Search in sources :

Example 66 with Composite

use of java.awt.Composite in project pivot by apache.

the class ImageViewSkin method paint.

@Override
public void paint(final Graphics2D graphics) {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    int width = getWidth();
    int height = getHeight();
    if (backgroundColor != null) {
        graphics.setPaint(backgroundColor);
        graphics.fillRect(0, 0, width, height);
    }
    if (image != null) {
        Graphics2D imageGraphics = (Graphics2D) graphics.create();
        imageGraphics.translate(imageX, imageY);
        imageGraphics.scale(scaleX, scaleY);
        // Apply an alpha composite if the opacity value is less than the current alpha
        float alpha = 1.0f;
        Composite composite = imageGraphics.getComposite();
        if (composite instanceof AlphaComposite) {
            AlphaComposite alphaComposite = (AlphaComposite) composite;
            alpha = alphaComposite.getAlpha();
        }
        if (opacity < alpha) {
            imageGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
        }
        image.paint(imageGraphics);
        imageGraphics.dispose();
    }
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) AlphaComposite(java.awt.AlphaComposite) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image) Graphics2D(java.awt.Graphics2D)

Example 67 with Composite

use of java.awt.Composite in project TrakEM2 by trakem2.

the class Tree method paint.

public final void paint(final Graphics2D g, final Rectangle srcRect, final double magnification, final boolean active, final int channels, final Layer active_layer, final List<Layer> layers, final boolean with_arrows, final boolean with_tags) {
    if (null == root) {
        setupForDisplay();
        if (null == root)
            return;
    }
    Composite original_composite = null;
    AffineTransform gt = null;
    Stroke stroke = null;
    final Color below, above;
    if (layer_set.use_color_cue_colors) {
        below = Color.red;
        above = Color.blue;
    } else {
        below = this.color;
        above = this.color;
    }
    synchronized (node_layer_map) {
        // Determine which layers to paint
        final Set<Node<T>> nodes = getNodesToPaint(active_layer, layers);
        if (null != nodes) {
            // so avoid filtering for them:
            if (srcRect.x > 0 && srcRect.y > 0 && srcRect.width < (int) layer_set.getLayerWidth() && srcRect.height < (int) layer_set.getLayerHeight()) {
                try {
                    final Rectangle localRect = this.at.createInverse().createTransformedShape(srcRect).getBounds();
                    for (final Iterator<Node<T>> it = nodes.iterator(); it.hasNext(); ) {
                        final Node<T> nd = it.next();
                        if (nd.isRoughlyInside(localRect))
                            continue;
                        it.remove();
                    }
                } catch (final NoninvertibleTransformException nite) {
                    IJError.print(nite);
                }
            }
            // Arrange transparency
            if (alpha != 1.0f) {
                original_composite = g.getComposite();
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            }
            // Clear transform and stroke
            gt = g.getTransform();
            g.setTransform(DisplayCanvas.DEFAULT_AFFINE);
            stroke = g.getStroke();
            g.setStroke(DisplayCanvas.DEFAULT_STROKE);
            final AffineTransform to_screen = new AffineTransform();
            to_screen.scale(magnification, magnification);
            to_screen.translate(-srcRect.x, -srcRect.y);
            to_screen.concatenate(this.at);
            final Node<T>[] handles = active ? new Node[nodes.size()] : null;
            int next = 0;
            final ArrayList<Runnable> tags_tasks = new ArrayList<Runnable>();
            for (final Node<T> nd : nodes) {
                final Runnable task = nd.paint(g, active_layer, active, srcRect, magnification, nodes, this, to_screen, with_arrows, with_tags, layer_set.paint_edge_confidence_boxes, true, above, below);
                if (null != task)
                    tags_tasks.add(task);
                if (nd == marked) {
                    if (null == MARKED_CHILD)
                        createMarks();
                    final Composite c = g.getComposite();
                    g.setXORMode(Color.green);
                    final float[] fps = new float[] { nd.x, nd.y };
                    this.at.transform(fps, 0, fps, 0, 1);
                    final AffineTransform aff = new AffineTransform();
                    aff.translate((fps[0] - srcRect.x) * magnification, (fps[1] - srcRect.y) * magnification);
                    g.fill(aff.createTransformedShape(active ? MARKED_PARENT : MARKED_CHILD));
                    g.setComposite(c);
                }
                if (active && active_layer == nd.la)
                    handles[next++] = nd;
            }
            for (final Runnable task : tags_tasks) task.run();
            if (active) {
                for (int i = 0; i < next; i++) {
                    handles[i].paintHandle(g, srcRect, magnification, this);
                }
            }
        }
    }
    // restore
    if (null != gt) {
        g.setTransform(gt);
        g.setStroke(stroke);
    }
    // Transparency: fix alpha composite back to original.
    if (null != original_composite) {
        g.setComposite(original_composite);
    }
}
Also used : KeyStroke(javax.swing.KeyStroke) Stroke(java.awt.Stroke) Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Point(java.awt.Point) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) AffineTransform(java.awt.geom.AffineTransform)

Example 68 with Composite

use of java.awt.Composite in project TrakEM2 by trakem2.

the class LayerSet method paint.

public void paint(Graphics2D g, Rectangle srcRect, double magnification, boolean active, int channels, Layer active_layer) {
    // arrange transparency
    Composite original_composite = null;
    if (alpha != 1.0f) {
        original_composite = g.getComposite();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    }
    // Translate graphics context accordingly
    AffineTransform gt = g.getTransform();
    AffineTransform aff = new AffineTransform(this.at);
    aff.preConcatenate(gt);
    g.setTransform(aff);
    // set color
    g.setColor(this.color);
    // fill a background box
    g.fillRect(0, 0, (int) (this.width), (int) (this.height));
    // the "opposite", but brighter, so it won't fail to generate contrast if the color is 127 in all channels
    g.setColor(new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()).brighter());
    int x = (int) (this.width / 5);
    int y = (int) (this.height / 5);
    int width = (int) (this.width / 5);
    int height = (int) (this.height / 5 * 3);
    g.fillRect(x, y, width, height);
    x = (int) (this.width / 5 * 2);
    y = (int) (this.height / 5 * 3);
    width = (int) (this.width / 5 * 2);
    height = (int) (this.height / 5);
    g.fillRect(x, y, width, height);
    // Transparency: fix composite back to original.
    if (alpha != 1.0f) {
        g.setComposite(original_composite);
    }
    g.setTransform(gt);
}
Also used : AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) AffineTransform(java.awt.geom.AffineTransform)

Example 69 with Composite

use of java.awt.Composite in project TrakEM2 by trakem2.

the class Pipe method paint.

// synchronizing to protect n_points ... need to wrap it in a lock
@Override
public void paint(final Graphics2D g, final Rectangle srcRect, final double magnification, final boolean active, final int channels, final Layer active_layer, final List<Layer> layers) {
    if (0 == n_points)
        return;
    if (-1 == n_points) {
        // load points from the database
        setupForDisplay();
    }
    // arrange transparency
    Composite original_composite = null;
    if (alpha != 1.0f) {
        original_composite = g.getComposite();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    }
    // local pointers, since they may be transformed
    int n_points = this.n_points;
    double[][] p = this.p;
    double[][] p_r = this.p_r;
    double[][] p_l = this.p_l;
    double[][] p_i = this.p_i;
    // double[] p_width = this.p_width;
    double[] p_width_i = this.p_width_i;
    if (!this.at.isIdentity()) {
        final Object[] ob = getTransformedData();
        p = (double[][]) ob[0];
        n_points = p[0].length;
        p_l = (double[][]) ob[1];
        p_r = (double[][]) ob[2];
        p_i = (double[][]) ob[3];
        // p_width = (double[])ob[4];
        p_width_i = (double[]) ob[5];
    }
    final boolean no_color_cues = !layer_set.color_cues;
    final Color below, above;
    if (layer_set.use_color_cue_colors) {
        below = Color.red;
        above = Color.blue;
    } else {
        below = this.color;
        above = this.color;
    }
    final long layer_id = active_layer.getId();
    if (active) {
        // draw/fill points
        final int oval_radius = (int) Math.ceil(4 / magnification);
        final int oval_corr = (int) Math.ceil(3 / magnification);
        for (int j = 0; j < n_points; j++) {
            // TODO there is room for optimization, operations are being done twice or 3 times; BUT is the creation of new variables as costly as the calculations? I have no idea.
            if (layer_id != p_layer[j])
                continue;
            // draw big ovals at backbone points
            DisplayCanvas.drawHandle(g, (int) p[0][j], (int) p[1][j], magnification);
            g.setColor(color);
            // fill small ovals at control points
            g.fillOval((int) p_l[0][j] - oval_corr, (int) p_l[1][j] - oval_corr, oval_radius, oval_radius);
            g.fillOval((int) p_r[0][j] - oval_corr, (int) p_r[1][j] - oval_corr, oval_radius, oval_radius);
            // draw lines between backbone and control points
            g.drawLine((int) p[0][j], (int) p[1][j], (int) p_l[0][j], (int) p_l[1][j]);
            g.drawLine((int) p[0][j], (int) p[1][j], (int) p_r[0][j], (int) p_r[1][j]);
            // label the first point distinctively:
            if (0 == j) {
                final Composite comp = g.getComposite();
                g.setColor(Color.white);
                g.setXORMode(Color.green);
                // displaced 4 screen pixels to the right
                g.drawString("1", (int) (p[0][0] + (4.0 / magnification)), (int) p[1][0]);
                g.setComposite(comp);
            }
        }
    }
    // paint the tube in 2D:
    if (n_points > 1 && p_i[0].length > 1) {
        // need the second check for repaints that happen before generating the interpolated points.
        double angle = 0;
        // double a0 = Math.toRadians(0);
        final double a90 = Math.toRadians(90);
        // double a180 = Math.toRadians(180);
        // double a270 = Math.toRadians(270);
        final int n = p_i[0].length;
        final double[] r_side_x = new double[n];
        final double[] r_side_y = new double[n];
        final double[] l_side_x = new double[n];
        final double[] l_side_y = new double[n];
        final int m = n - 1;
        for (int i = 0; i < n - 1; i++) {
            angle = Math.atan2(p_i[0][i + 1] - p_i[0][i], p_i[1][i + 1] - p_i[1][i]);
            // sin and cos are inverted, but works better like this. WHY ??
            r_side_x[i] = p_i[0][i] + Math.sin(angle + a90) * p_width_i[i];
            r_side_y[i] = p_i[1][i] + Math.cos(angle + a90) * p_width_i[i];
            l_side_x[i] = p_i[0][i] + Math.sin(angle - a90) * p_width_i[i];
            l_side_y[i] = p_i[1][i] + Math.cos(angle - a90) * p_width_i[i];
        }
        angle = Math.atan2(p_i[0][m] - p_i[0][m - 1], p_i[1][m] - p_i[1][m - 1]);
        r_side_x[m] = p_i[0][m] + Math.sin(angle + a90) * p_width_i[m];
        r_side_y[m] = p_i[1][m] + Math.cos(angle + a90) * p_width_i[m];
        l_side_x[m] = p_i[0][m] + Math.sin(angle - a90) * p_width_i[m];
        l_side_y[m] = p_i[1][m] + Math.cos(angle - a90) * p_width_i[m];
        final double z_current = active_layer.getZ();
        if (no_color_cues) {
            // paint a tiny bit where it should!
            g.setColor(this.color);
        }
        for (int j = 0; j < n_points; j++) {
            // at least looping through 2 points, as guaranteed by the preconditions checking
            if (no_color_cues) {
                if (layer_id == p_layer[j]) {
                // paint normally
                } else {
                    // else if crossed the current layer, paint segment as well
                    if (0 == j)
                        continue;
                    final double z1 = layer_set.getLayer(p_layer[j - 1]).getZ();
                    final double z2 = layer_set.getLayer(p_layer[j]).getZ();
                    if ((z1 < z_current && z_current < z2) || (z2 < z_current && z_current < z1)) {
                    // paint normally, in this pipe's color
                    } else {
                        continue;
                    }
                }
            } else {
                final double z = layer_set.getLayer(p_layer[j]).getZ();
                if (z < z_current)
                    g.setColor(below);
                else if (z == z_current)
                    g.setColor(this.color);
                else
                    g.setColor(above);
            }
            int fi = 0;
            int la = j * 20 - 1;
            // 10 is half a segment
            if (0 != j)
                fi = (j * 20) - 10;
            // same //= j * 20 + 9;
            if (n_points - 1 != j)
                la += 10;
            // quick fix. -2 so that the k+1 below can work
            if (la >= r_side_x.length)
                la = r_side_x.length - 2;
            if (fi > la)
                fi = la;
            try {
                for (int k = fi; k <= la; k++) {
                    g.drawLine((int) r_side_x[k], (int) r_side_y[k], (int) r_side_x[k + 1], (int) r_side_y[k + 1]);
                    g.drawLine((int) l_side_x[k], (int) l_side_y[k], (int) l_side_x[k + 1], (int) l_side_y[k + 1]);
                }
            } catch (final Exception ee) {
                Utils.log2("Pipe paint failed with: fi=" + fi + " la=" + la + " n_points=" + n_points + " r_side_x.length=" + r_side_x.length);
            // WARNING still something is wrong with the synchronization over the arrays ... despite this error being patched with the line above:
            // if (la >= r_side_x.length) r_side_x.length-2; // quick fix
            // 
            // ADDING the synchronized keyword to many methods involving n_points did not fix it; neither to crop arrays and get the new n_points from the getTransformedData() returned p array.
            }
        }
    }
    // Transparency: fix alpha composite back to original.
    if (null != original_composite) {
        g.setComposite(original_composite);
    }
}
Also used : AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color)

Example 70 with Composite

use of java.awt.Composite in project TrakEM2 by trakem2.

the class Polyline method paint.

@Override
public void paint(final Graphics2D g, final Rectangle srcRect, final double magnification, final boolean active, final int channels, final Layer active_layer, final List<Layer> layers) {
    if (0 == n_points)
        return;
    if (-1 == n_points) {
        // load points from the database
        setupForDisplay();
    }
    // arrange transparency
    Composite original_composite = null;
    if (alpha != 1.0f) {
        original_composite = g.getComposite();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    }
    // local pointers, since they may be transformed
    int n_points = this.n_points;
    double[][] p = this.p;
    if (!this.at.isIdentity()) {
        final Object[] ob = getTransformedData();
        p = (double[][]) ob[0];
        n_points = p[0].length;
    }
    final boolean no_color_cues = !layer_set.color_cues;
    final Color below, above;
    if (layer_set.use_color_cue_colors) {
        below = Color.red;
        above = Color.blue;
    } else {
        below = this.color;
        above = this.color;
    }
    final long layer_id = active_layer.getId();
    final double z_current = active_layer.getZ();
    // Different approach: a point paints itself and towards the next, except the last point.
    // First point:
    double z = layer_set.getLayer(p_layer[0]).getZ();
    boolean paint = true;
    if (z < z_current) {
        if (no_color_cues)
            paint = false;
        else
            g.setColor(below);
    } else if (z == z_current)
        g.setColor(this.color);
    else if (no_color_cues)
        paint = false;
    else
        g.setColor(above);
    // Paint half line:
    if (paint && n_points > 1) {
        g.drawLine((int) p[0][0], (int) p[1][0], (int) ((p[0][0] + p[0][1]) / 2), (int) ((p[1][0] + p[1][1]) / 2));
    }
    // Paint handle if active and in the current layer
    if (active && layer_id == p_layer[0]) {
        g.setColor(this.color);
        DisplayCanvas.drawHandle(g, p[0][0], p[1][0], srcRect, magnification);
        // Label the first point distinctively
        final Composite comp = g.getComposite();
        final AffineTransform aff = g.getTransform();
        g.setTransform(new AffineTransform());
        g.setColor(Color.white);
        g.setXORMode(Color.green);
        g.drawString("1", (int) ((p[0][0] - srcRect.x) * magnification + (4.0 / magnification)), // displaced 4 screen pixels to the right
        (int) ((p[1][0] - srcRect.y) * magnification));
        g.setComposite(comp);
        g.setTransform(aff);
    }
    for (int i = 1; i < n_points; i++) {
        // Determine color
        z = layer_set.getLayer(p_layer[i]).getZ();
        paint = true;
        if (z < z_current) {
            if (no_color_cues)
                paint = false;
            else
                g.setColor(below);
        } else if (z == z_current)
            g.setColor(this.color);
        else if (no_color_cues)
            paint = false;
        else
            g.setColor(above);
        if (!paint)
            continue;
        // paint half line towards previous point:
        g.drawLine((int) p[0][i], (int) p[1][i], (int) ((p[0][i] + p[0][i - 1]) / 2), (int) ((p[1][i] + p[1][i - 1]) / 2));
        // paint half line towards next point:
        if (i < n_points - 1) {
            g.drawLine((int) p[0][i], (int) p[1][i], (int) ((p[0][i] + p[0][i + 1]) / 2), (int) ((p[1][i] + p[1][i + 1]) / 2));
        }
        // Paint handle if active and in the current layer
        if (active && layer_id == p_layer[i]) {
            g.setColor(this.color);
            DisplayCanvas.drawHandle(g, p[0][i], p[1][i], srcRect, magnification);
        }
    }
    // Transparency: fix alpha composite back to original.
    if (null != original_composite) {
        g.setComposite(original_composite);
    }
}
Also used : AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Color(java.awt.Color) AffineTransform(java.awt.geom.AffineTransform)

Aggregations

Composite (java.awt.Composite)248 AlphaComposite (java.awt.AlphaComposite)236 Graphics2D (java.awt.Graphics2D)120 Paint (java.awt.Paint)80 Rectangle2D (java.awt.geom.Rectangle2D)80 Color (java.awt.Color)76 Shape (java.awt.Shape)64 BasicStroke (java.awt.BasicStroke)47 Stroke (java.awt.Stroke)44 BufferedImage (java.awt.image.BufferedImage)42 AffineTransform (java.awt.geom.AffineTransform)33 Point (java.awt.Point)32 Line2D (java.awt.geom.Line2D)30 Font (java.awt.Font)28 Point2D (java.awt.geom.Point2D)24 Rectangle (java.awt.Rectangle)23 GradientPaint (java.awt.GradientPaint)22 GeneralPath (java.awt.geom.GeneralPath)20 RectangleInsets (org.jfree.ui.RectangleInsets)20 FontMetrics (java.awt.FontMetrics)19