Example usage for java.awt AlphaComposite SrcAtop

List of usage examples for java.awt AlphaComposite SrcAtop

Introduction

In this page you can find the example usage for java.awt AlphaComposite SrcAtop.

Prototype

AlphaComposite SrcAtop

To view the source code for java.awt AlphaComposite SrcAtop.

Click Source Link

Document

AlphaComposite object that implements the opaque SRC_ATOP rule with an alpha of 1.0f.

Usage

From source file:Main.java

private static BufferedImage dye(BufferedImage image, Color color) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = dyed.createGraphics();
    g.drawImage(image, 0, 0, null);/*  w w w . j a v a2 s  .  com*/
    g.setComposite(AlphaComposite.SrcAtop);
    g.setColor(color);
    g.fillRect(0, 0, w, h);
    g.dispose();
    return dyed;
}

From source file:Main.java

/**
 * Fills the given {@link BufferedImage} with a {@link Paint}, preserving its alpha channel.
 *
 * @param source The source image./*  w  w w.jav a  2 s . co  m*/
 * @param paint  The paint to fill with.
 * @return A new, painted/filled image.
 */
public static BufferedImage filledImage(BufferedImage source, Paint paint) {
    BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, 0, 0, null);
    g.setComposite(AlphaComposite.SrcAtop);
    g.setPaint(paint);
    g.fillRect(0, 0, source.getWidth(), source.getHeight());
    return newImage;
}

From source file:com.zacwolf.commons.email.Email.java

public static BufferedImage makeRoundedBanner(BufferedImage image, int cornerRadius) {
    int w = image.getWidth();
    int h = image.getHeight() + 10;
    BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);/*from   www .  ja va2  s. c o  m*/
    g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
    g2.setComposite(AlphaComposite.SrcAtop);
    g2.drawImage(image, 0, 0, null);
    g2.setComposite(AlphaComposite.SrcOver);
    //               g2.setColor(new Color(153,153,153));//slight grey border
    //               g2.drawRoundRect(0, 0, w-1, h, cornerRadius, cornerRadius);
    g2.dispose();
    return output.getSubimage(0, 0, image.getWidth(), image.getHeight());
}

From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphNode.java

/**
 * Draw a GraphNode as a rectangle with a name
 *
 * @param g   The Java2D Graphics/*  www .  jav  a2  s  .com*/
 * @param col The color to draw
 */
public void drawNode(final Graphics2D g, final Color col) {
    final int x = displayPosition.x;
    final int y = displayPosition.y;

    g.setFont(g.getFont().deriveFont(Font.BOLD, 11));
    final FontMetrics metrics = g.getFontMetrics();
    final String name = node.getId();
    final Rectangle2D rect = metrics.getStringBounds(name, g);
    final int stringWidth = (int) rect.getWidth();
    setSize(Math.max(stringWidth, 50) + 10, 25);

    int step = 4;
    int alpha = 96;
    for (int i = 0; i < step; ++i) {
        g.setColor(new Color(0, 0, 0, alpha - (32 * i)));
        g.drawLine(x + i + 1, y + nodeHeight + i, x + nodeWidth + i - 1, y + nodeHeight + i);
        g.drawLine(x + nodeWidth + i, y + i, x + nodeWidth + i, y + nodeHeight + i);
    }

    Shape clipShape = new Rectangle(x, y, nodeWidth, nodeHeight);

    g.setComposite(AlphaComposite.SrcAtop);
    g.setPaint(new GradientPaint(x, y, col, x + nodeWidth, y + nodeHeight, col.darker()));
    g.fill(clipShape);

    g.setColor(Color.blue);
    g.draw3DRect(x, y, nodeWidth - 1, nodeHeight - 1, true);

    g.setColor(Color.BLACK);
    g.drawString(name, x + (nodeWidth - stringWidth) / 2, y + 15);
}

From source file:org.jas.gui.ImagePanel.java

@Override
protected void paintComponent(Graphics g) {
    if (portrait == null) {
        super.paintComponent(g);
        return;/*  ww w .j  a  va2s  .  com*/
    }
    try {
        // Create a translucent intermediate image in which we can perform the soft clipping
        GraphicsConfiguration gc = ((Graphics2D) g).getDeviceConfiguration();
        BufferedImage intermediateBufferedImage = gc.createCompatibleImage(getWidth(), getHeight(),
                Transparency.TRANSLUCENT);
        Graphics2D bufferGraphics = intermediateBufferedImage.createGraphics();

        // Clear the image so all pixels have zero alpha
        bufferGraphics.setComposite(AlphaComposite.Clear);
        bufferGraphics.fillRect(0, 0, getWidth(), getHeight());

        // Render our clip shape into the image. Shape on where to paint
        bufferGraphics.setComposite(AlphaComposite.Src);
        bufferGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        bufferGraphics.setColor(Color.WHITE);
        bufferGraphics.fillRoundRect(0, 0, getWidth(), getHeight(), (int) (getWidth() * arcWidth),
                (int) (getHeight() * arcHeight));

        // SrcAtop uses the alpha value as a coverage value for each pixel stored in the
        // destination shape. For the areas outside our clip shape, the destination
        // alpha will be zero, so nothing is rendered in those areas. For
        // the areas inside our clip shape, the destination alpha will be fully
        // opaque.
        bufferGraphics.setComposite(AlphaComposite.SrcAtop);
        bufferGraphics.drawImage(portrait, 0, 0, getWidth(), getHeight(), null);
        bufferGraphics.dispose();

        // Copy our intermediate image to the screen
        g.drawImage(intermediateBufferedImage, 0, 0, null);

    } catch (Exception e) {
        log.warn("Error: Creating Renderings", e);
    }
}