Search in sources :

Example 56 with Composite

use of java.awt.Composite in project josm by JOSM.

the class GpxDrawHelper method drawLinesAlpha.

/**
 * Draw GPX lines by using alpha blending
 * @param g               the common draw object to use
 * @param mv              the meta data to current displayed area
 * @param visibleSegments segments visible in the current scope of mv
 * @param layerAlpha      the color alpha value set for that operation
 */
private void drawLinesAlpha(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, float layerAlpha) {
    // 1st. backup the paint environment ----------------------------------
    Composite oldComposite = g.getComposite();
    Stroke oldStroke = g.getStroke();
    Paint oldPaint = g.getPaint();
    // 2nd. determine current scale factors -------------------------------
    // adjust global settings
    final int globalLineWidth = Utils.clamp(lineWidth, 1, 20);
    // cache scale of view
    final double zoomScale = mv.getDist100Pixel() / 50.0f;
    // 3rd. determine current paint parameters -----------------------------
    // alpha value is based on zoom and line with combined with global layer alpha
    float theLineAlpha = (float) Utils.clamp((0.50 / zoomScale) / (globalLineWidth + 1), 0.01, 0.50) * layerAlpha;
    final int theLineWith = (int) (lineWidth / zoomScale) + 1;
    // 4th setup virtual paint area ----------------------------------------
    // set line format and alpha channel for all overlays (more lines -> few overlap -> more transparency)
    g.setStroke(new BasicStroke(theLineWith, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g.setComposite(AlphaComposite.SrcOver.derive(theLineAlpha));
    // last used / calculated entries
    Point lastPaintPnt = null;
    // for all points
    for (WayPoint trkPnt : visibleSegments) {
        // transform coordinates
        final Point paintPnt = mv.getPoint(trkPnt);
        // skip single points
        if (lastPaintPnt != null && trkPnt.drawLine && !lastPaintPnt.equals(paintPnt)) {
            // set different color
            g.setColor(trkPnt.customColoring);
            // draw it
            g.drawLine(lastPaintPnt.x, lastPaintPnt.y, paintPnt.x, paintPnt.y);
        }
        lastPaintPnt = paintPnt;
    }
    // @last restore modified paint environment -----------------------------
    g.setPaint(oldPaint);
    g.setStroke(oldStroke);
    g.setComposite(oldComposite);
}
Also used : BasicStroke(java.awt.BasicStroke) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) WayPoint(org.openstreetmap.josm.data.gpx.WayPoint) MultipleGradientPaint(java.awt.MultipleGradientPaint) Paint(java.awt.Paint) LinearGradientPaint(java.awt.LinearGradientPaint) Point(java.awt.Point) WayPoint(org.openstreetmap.josm.data.gpx.WayPoint) Point(java.awt.Point) MultipleGradientPaint(java.awt.MultipleGradientPaint) WayPoint(org.openstreetmap.josm.data.gpx.WayPoint) Paint(java.awt.Paint) LinearGradientPaint(java.awt.LinearGradientPaint)

Example 57 with Composite

use of java.awt.Composite in project josm by JOSM.

the class OsmDataLayer method createHatchTexture.

/**
 * Initialize the hatch pattern used to paint the non-downloaded area
 */
public static void createHatchTexture() {
    BufferedImage bi = new BufferedImage(HATCHED_SIZE, HATCHED_SIZE, BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(getBackgroundColor());
    Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
    big.setComposite(comp);
    big.fillRect(0, 0, HATCHED_SIZE, HATCHED_SIZE);
    big.setColor(getOutsideColor());
    big.drawLine(-1, 6, 6, -1);
    big.drawLine(4, 16, 16, 4);
    hatched = bi;
}
Also used : AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 58 with Composite

use of java.awt.Composite in project josm by JOSM.

the class GeoImageLayer method paint.

@Override
public void paint(Graphics2D g, MapView mv, Bounds bounds) {
    int width = mv.getWidth();
    int height = mv.getHeight();
    Rectangle clip = g.getClipBounds();
    if (useThumbs) {
        if (!thumbsLoaded) {
            startLoadThumbs();
        }
        if (null == offscreenBuffer || // reuse the old buffer if possible
        offscreenBuffer.getWidth() != width || offscreenBuffer.getHeight() != height) {
            offscreenBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            updateOffscreenBuffer = true;
        }
        if (updateOffscreenBuffer) {
            Graphics2D tempG = offscreenBuffer.createGraphics();
            tempG.setColor(new Color(0, 0, 0, 0));
            Composite saveComp = tempG.getComposite();
            // remove the old images
            tempG.setComposite(AlphaComposite.Clear);
            tempG.fillRect(0, 0, width, height);
            tempG.setComposite(saveComp);
            for (ImageEntry e : data.searchImages(bounds)) {
                paintImage(e, mv, clip, tempG);
            }
            for (ImageEntry img : this.data.getSelectedImages()) {
                // Make sure the selected image is on top in case multiple images overlap.
                paintImage(img, mv, clip, tempG);
            }
            updateOffscreenBuffer = false;
        }
        g.drawImage(offscreenBuffer, 0, 0, null);
    } else {
        for (ImageEntry e : data.searchImages(bounds)) {
            if (e.getPos() == null) {
                continue;
            }
            Point p = mv.getPoint(e.getPos());
            icon.paintIcon(mv, g, p.x - icon.getIconWidth() / 2, p.y - icon.getIconHeight() / 2);
        }
    }
    for (ImageEntry e : data.getSelectedImages()) {
        if (e != null && e.getPos() != null) {
            Point p = mv.getPoint(e.getPos());
            Dimension imgDim = getImageDimension(e);
            if (e.getExifImgDir() != null) {
                Vector3D imgRotation = ImageViewerDialog.getInstance().getRotation(e);
                drawDirectionArrow(g, p, e.getExifImgDir() + (imgRotation != null ? Utils.toDegrees(imgRotation.getPolarAngle()) : 0d), imgDim);
            }
            if (useThumbs && e.hasThumbnail()) {
                g.setColor(new Color(128, 0, 0, 122));
                g.fillRect(p.x - imgDim.width / 2, p.y - imgDim.height / 2, imgDim.width, imgDim.height);
            } else {
                selectedIcon.paintIcon(mv, g, p.x - imgDim.width / 2, p.y - imgDim.height / 2);
            }
        }
    }
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) Vector3D(org.openstreetmap.josm.gui.util.imagery.Vector3D) GpxImageEntry(org.openstreetmap.josm.data.gpx.GpxImageEntry) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Dimension(java.awt.Dimension) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 59 with Composite

use of java.awt.Composite in project knime-base by knime.

the class DrawingUtils method drawBlock.

/**
 * Draws a filled shape without a border.
 * @param g2 the graphic object
 * @param shape the shape to fill
 * @param paint the filling color or TexturePaint
 * @param alpha the transparency
 */
public static void drawBlock(final Graphics2D g2, final Shape shape, final Paint paint, final float alpha) {
    if (shape == null) {
        return;
    }
    // save the original settings
    final Paint origPaint = g2.getPaint();
    final Composite originalComposite = g2.getComposite();
    // draw the color block
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    g2.setPaint(paint);
    g2.fill(shape);
    // set the old settings
    g2.setPaint(origPaint);
    g2.setComposite(originalComposite);
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) Paint(java.awt.Paint)

Example 60 with Composite

use of java.awt.Composite in project com.revolsys.open by revolsys.

the class Graphics2DTextStyleRenderer method drawText.

@Override
public void drawText(final String label, final Geometry geometry) {
    final Graphics2DViewRenderer view = this.view;
    double dx = this.dx;
    double dy = this.dy;
    if (Property.hasValue(label) && geometry != null) {
        final TextStyle style = this.style;
        final String textPlacementType = style.getTextPlacementType();
        final PointDoubleXYOrientation point = view.getPointWithOrientation(geometry, textPlacementType);
        if (point != null) {
            double orientation;
            final String orientationType = style.getTextOrientationType();
            if ("none".equals(orientationType)) {
                orientation = 0;
            } else {
                orientation = point.getOrientation();
                if (orientation > 270) {
                    orientation -= 360;
                }
            }
            orientation += style.getTextOrientation();
            final Graphics2D graphics = this.graphics;
            final Paint paint = graphics.getPaint();
            final Composite composite = graphics.getComposite();
            final AffineTransform savedTransform = graphics.getTransform();
            try {
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
                final double[] coordinates = this.coordinates;
                point.copyCoordinates(coordinates);
                view.toViewCoordinates(coordinates);
                style.setTextStyle(view, graphics);
                final FontMetrics fontMetrics = graphics.getFontMetrics();
                double maxWidth = 0;
                final String[] lines = label.split("[\\r\\n]");
                for (final String line : lines) {
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    maxWidth = Math.max(width, maxWidth);
                }
                final int descent = fontMetrics.getDescent();
                final int ascent = fontMetrics.getAscent();
                final int leading = fontMetrics.getLeading();
                final double maxHeight = lines.length * (ascent + descent) + (lines.length - 1) * leading;
                final String verticalAlignment = style.getTextVerticalAlignment();
                if ("top".equals(verticalAlignment)) {
                } else if ("middle".equals(verticalAlignment)) {
                    dy -= maxHeight / 2;
                } else {
                    dy -= maxHeight;
                }
                String horizontalAlignment = style.getTextHorizontalAlignment();
                double screenX = coordinates[0];
                double screenY = coordinates[1];
                final String textPlacement = textPlacementType;
                if ("auto".equals(textPlacement) && view != null) {
                    if (screenX < 0) {
                        screenX = 1;
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    final double viewWidth = view.getViewWidthPixels();
                    if (screenX + maxWidth > viewWidth) {
                        screenX = (int) (viewWidth - maxWidth - 1);
                        dx = 0;
                        horizontalAlignment = "left";
                    }
                    if (screenY < maxHeight) {
                        screenY = 1;
                        dy = 0;
                    }
                    final double viewHeight = view.getViewHeightPixels();
                    if (screenY > viewHeight) {
                        screenY = viewHeight - 1 - maxHeight;
                        dy = 0;
                    }
                }
                graphics.translate(screenX, screenY);
                if (orientation != 0) {
                    graphics.rotate(-Math.toRadians(orientation), 0, 0);
                }
                graphics.translate(dx, dy);
                for (final String line : lines) {
                    graphics.translate(0, ascent);
                    final AffineTransform lineTransform = graphics.getTransform();
                    final Rectangle2D bounds = fontMetrics.getStringBounds(line, graphics);
                    final double width = bounds.getWidth();
                    final double height = bounds.getHeight();
                    if ("right".equals(horizontalAlignment)) {
                        graphics.translate(-width, 0);
                    } else if ("center".equals(horizontalAlignment) || "auto".equals(horizontalAlignment)) {
                        graphics.translate(-width / 2, 0);
                    }
                    graphics.translate(dx, 0);
                    graphics.scale(1, 1);
                    if (Math.abs(orientation) > 90) {
                        graphics.rotate(Math.PI, maxWidth / 2, -height / 4);
                    }
                    final int textBoxOpacity = style.getTextBoxOpacity();
                    final Color textBoxColor = style.getTextBoxColor();
                    if (textBoxOpacity > 0 && textBoxColor != null) {
                        graphics.setPaint(textBoxColor);
                        final double cornerSize = Math.max(height / 2, 5);
                        final RoundRectangle2D.Double box = new RoundRectangle2D.Double(bounds.getX() - 3, bounds.getY() - 1, width + 6, height + 2, cornerSize, cornerSize);
                        graphics.fill(box);
                    }
                    final double radius = style.getTextHaloRadius();
                    final Unit<Length> unit = style.getTextSizeUnit();
                    final double textHaloRadius = view.toDisplayValue(Quantities.getQuantity(radius, unit));
                    if (textHaloRadius > 0) {
                        final Stroke savedStroke = graphics.getStroke();
                        final Stroke outlineStroke = new BasicStroke((float) (textHaloRadius + 1), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
                        graphics.setColor(style.getTextHaloFill());
                        graphics.setStroke(outlineStroke);
                        final Font font = graphics.getFont();
                        final FontRenderContext fontRenderContext = graphics.getFontRenderContext();
                        final TextLayout textLayout = new TextLayout(line, font, fontRenderContext);
                        final Shape outlineShape = textLayout.getOutline(Graphics2DViewRenderer.IDENTITY_TRANSFORM);
                        graphics.draw(outlineShape);
                        graphics.setStroke(savedStroke);
                    }
                    graphics.setColor(style.getTextFill());
                    if (textBoxOpacity > 0 && textBoxOpacity < 255) {
                        graphics.setComposite(AlphaComposite.SrcOut);
                        graphics.drawString(line, (float) 0, (float) 0);
                        graphics.setComposite(AlphaComposite.DstOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    } else {
                        graphics.setComposite(AlphaComposite.SrcOver);
                        graphics.drawString(line, (float) 0, (float) 0);
                    }
                    graphics.setTransform(lineTransform);
                    graphics.translate(0, leading + descent);
                }
            } finally {
                graphics.setTransform(savedTransform);
                graphics.setPaint(paint);
                graphics.setComposite(composite);
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Font(java.awt.Font) PointDoubleXYOrientation(com.revolsys.geometry.model.impl.PointDoubleXYOrientation) TextStyle(com.revolsys.swing.map.layer.record.style.TextStyle) FontMetrics(java.awt.FontMetrics) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Paint(java.awt.Paint) Paint(java.awt.Paint) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout) Length(javax.measure.quantity.Length) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext)

Aggregations

Composite (java.awt.Composite)289 AlphaComposite (java.awt.AlphaComposite)274 Graphics2D (java.awt.Graphics2D)133 Paint (java.awt.Paint)103 Rectangle2D (java.awt.geom.Rectangle2D)103 Color (java.awt.Color)79 Shape (java.awt.Shape)77 BasicStroke (java.awt.BasicStroke)54 Stroke (java.awt.Stroke)52 BufferedImage (java.awt.image.BufferedImage)48 Line2D (java.awt.geom.Line2D)38 AffineTransform (java.awt.geom.AffineTransform)37 Point (java.awt.Point)36 Rectangle (java.awt.Rectangle)35 Font (java.awt.Font)33 Point2D (java.awt.geom.Point2D)31 GradientPaint (java.awt.GradientPaint)27 Iterator (java.util.Iterator)26 PlotOrientation (org.jfree.chart.plot.PlotOrientation)25 GeneralPath (java.awt.geom.GeneralPath)23