Example usage for java.awt AlphaComposite SrcOver

List of usage examples for java.awt AlphaComposite SrcOver

Introduction

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

Prototype

AlphaComposite SrcOver

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static void setAlpha(final Graphics graphics, final float alpha) {
    ((Graphics2D) graphics).setComposite(AlphaComposite.SrcOver.derive(alpha));
}

From source file:Main.java

/**
 * Clears the given area of the specified graphics object with the given
 * color or makes the region transparent.
 *///from  w ww .ja  v a  2 s . c  o m
public static void clearRect(Graphics2D g, Rectangle rect, Color background) {
    if (background != null) {
        g.setColor(background);
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    } else {
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
        g.setComposite(AlphaComposite.SrcOver);
    }
}

From source file:Main.java

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    if (!on) {//from  ww  w .j  a v a  2s .c o m
        g2d.setComposite(AlphaComposite.SrcOver.derive(0f));
    } else {
        g2d.setComposite(AlphaComposite.SrcOver.derive(1f));
    }
    super.paint(g2d);
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
    g2d.fillRect(10, 10, getWidth(), getHeight());
    g2d.dispose();/*from w ww.  j  av  a  2  s .  c  o  m*/
}

From source file:HighlightedButton.java

/**
 * Creates a new instance of HighlightedButton
 *//*from   w ww .  j  av  a2  s .  c  o m*/
public HighlightedButton(String label) {
    super(label);

    // Get the Graphics for the image
    Graphics2D g2d = highlight.createGraphics();

    // Erase the image with a transparent background
    g2d.setComposite(AlphaComposite.Clear);
    g2d.fillRect(0, 0, HIGHLIGHT_SIZE, HIGHLIGHT_SIZE);
    g2d.setComposite(AlphaComposite.SrcOver);

    // Draw the highlight
    Point2D center = new Point2D.Float((float) HIGHLIGHT_SIZE / 2.0f, (float) HIGHLIGHT_SIZE / 2.0f);
    float radius = (float) HIGHLIGHT_SIZE / 2.0f;
    float[] dist = { 0.0f, .85f };
    Color[] colors = { Color.white, new Color(255, 255, 255, 0) };
    RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
    g2d.setPaint(paint);
    g2d.fillOval(0, 0, HIGHLIGHT_SIZE, HIGHLIGHT_SIZE);
    g2d.dispose();
}

From source file:CompositeEffects.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;

    // fill the background
    g.setPaint(new Color(175, 175, 175));
    g.fillRect(0, 0, getWidth(), getHeight());

    // Set text attributes
    g.setColor(Color.black);//w  w w .  java2 s .c o  m
    g.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Draw the unmodified image
    g.translate(10, 10);
    g.drawImage(cover, 0, 0, this);
    g.drawString("SRC_OVER", 0, COVERHEIGHT + 15);

    // Draw the cover again, using AlphaComposite to make the opaque
    // colors of the image 50% translucent
    g.translate(COVERWIDTH + 10, 0);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g.drawImage(cover, 0, 0, this);

    // Restore the pre-defined default Composite for the screen, so
    // opaque colors stay opaque.
    g.setComposite(AlphaComposite.SrcOver);
    // Label the effect
    g.drawString("SRC_OVER, 50%", 0, COVERHEIGHT + 15);

    // Now get an offscreen image to work with. In order to achieve
    // certain compositing effects, the drawing surface must support
    // transparency. Onscreen drawing surfaces cannot, so we have to do the
    // compositing in an offscreen image that is specially created to have
    // an "alpha channel", then copy the final result to the screen.
    BufferedImage offscreen = new BufferedImage(COVERWIDTH, COVERHEIGHT, BufferedImage.TYPE_INT_ARGB);

    // First, fill the image with a color gradient background that varies
    // left-to-right from opaque to transparent yellow
    Graphics2D osg = offscreen.createGraphics();
    osg.setPaint(new GradientPaint(0, 0, Color.yellow, COVERWIDTH, 0, new Color(255, 255, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now copy the cover image on top of this, but use the DstOver rule
    // which draws it "underneath" the existing pixels, and allows the
    // image to show depending on the transparency of those pixels.
    osg.setComposite(AlphaComposite.DstOver);
    osg.drawImage(cover, 0, 0, this);

    // And display this composited image on the screen. Note that the
    // image is opaque and that none of the screen background shows through
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("DST_OVER", 0, COVERHEIGHT + 15);

    // Now start over and do a new effect with the off-screen image.
    // First, fill the offscreen image with a new color gradient. We
    // don't care about the colors themselves; we just want the
    // translucency of the background to vary. We use opaque black to
    // transparent black. Note that since we've already used this offscreen
    // image, we set the composite to Src, we can fill the image and
    // ignore anything that is already there.
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now set the compositing type to SrcIn, so colors come from the
    // source, but translucency comes from the destination
    osg.setComposite(AlphaComposite.SrcIn);

    // Draw our loaded image into the off-screen image, compositing it.
    osg.drawImage(cover, 0, 0, this);

    // And then copy our off-screen image to the screen. Note that the
    // image is translucent and some of the image shows through.
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_IN", 0, COVERHEIGHT + 15);

    // If we do the same thing but use SrcOut, then the resulting image
    // will have the inverted translucency values of the destination
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);
    osg.setComposite(AlphaComposite.SrcOut);
    osg.drawImage(cover, 0, 0, this);
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_OUT", 0, COVERHEIGHT + 15);

    // Here's a cool effect; it has nothing to do with compositing, but
    // uses an arbitrary shape to clip the image. It uses Area to combine
    // shapes into more complicated ones.
    g.translate(COVERWIDTH + 10, 0);
    Shape savedClip = g.getClip(); // Save current clipping region
    // Create a shape to use as the new clipping region.
    // Begin with an ellipse
    Area clip = new Area(new Ellipse2D.Float(0, 0, COVERWIDTH, COVERHEIGHT));
    // Intersect with a rectangle, truncating the ellipse.
    clip.intersect(new Area(new Rectangle(5, 5, COVERWIDTH - 10, COVERHEIGHT - 10)));
    // Then subtract an ellipse from the bottom of the truncated ellipse.
    clip.subtract(new Area(new Ellipse2D.Float(COVERWIDTH / 2 - 40, COVERHEIGHT - 20, 80, 40)));
    // Use the resulting shape as the new clipping region
    g.clip(clip);
    // Then draw the image through this clipping region
    g.drawImage(cover, 0, 0, this);
    // Restore the old clipping region so we can label the effect
    g.setClip(savedClip);
    g.drawString("Clipping", 0, COVERHEIGHT + 15);
}

From source file:org.tsho.dmc2.core.chart.CowebRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {

    state = STATE_RUNNING;//ww  w  .j a va 2s . c o  m

    if (plot.isAlpha()) {
        g2.setComposite(AlphaComposite.SrcOver);
    }

    Stepper.Point2D result = stepper.getCurrentPoint2D();

    int transX, transY;

    double start = (int) dataArea.getMinX();
    double end = (int) dataArea.getMaxX();

    double[] value = new double[1];

    int prevY = 0;
    boolean flagOld = false;
    boolean flagNew = false;

    label: for (double i = start; i <= end; i += 1) {
        value[0] = this.domainAxis.java2DToValue(i, dataArea, RectangleEdge.BOTTOM);

        stepper.setInitialValue(value);
        stepper.initialize();

        for (int j = 0; j < power; j++) {
            stepper.step();
        }

        result = stepper.getCurrentPoint2D();

        transX = (int) i;
        transY = (int) rangeAxis.valueToJava2D(result.getX(), dataArea, RectangleEdge.LEFT);
        flagNew = Double.isNaN(result.getX());

        if (bigDots) {
            g2.fillRect(transX - 1, transY - 1, 3, 3);
        } else {
            g2.fillRect(transX, transY, 1, 1);
        }

        if (connectWithLines) {
            if (i > start) {
                if (!flagOld && !flagNew)
                    g2.drawLine(transX, transY, transX - 1, prevY);
            }

            prevY = transY;
            flagOld = flagNew;
        }

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }

    }

    if (animate) {
        animateCowebPlot(g2, dataArea);
    }

    state = STATE_FINISHED;
}

From source file:org.tsho.dmc2.core.chart.CowebRenderer.java

private void animateCowebPlot(Graphics2D g2, Rectangle2D dataArea) {

    Graphics2D g2bisec = (Graphics2D) g2.create();
    g2bisec.setColor(Color.blue);

    Stroke stroke = new BasicStroke(3f);
    Stroke origStroke = g2.getStroke();
    Color color = Color.BLACK;
    g2blink = (Graphics2D) g2.create();
    g2blink.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    g2blink.setXORMode(color);//  ww  w  .j  a v  a  2s .  c o  m
    g2blink.setStroke(stroke);

    if (plot.isAlpha()) {
        g2bisec.setComposite(AlphaComposite.SrcOver);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, plot.getForegroundAlpha()));
    }

    /* transients */
    stepper.setInitialValue(initialValue);
    stepper.initialize();

    state = STATE_TRANSIENTS;

    for (int index = 0; index < transients; index++) {

        stepper.step();

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }
    }

    state = STATE_RUNNING;

    Range xDataRange = plot.getDataRange(domainAxis);
    int transX, transY, transX1, transY1;

    transX = (int) this.domainAxis.valueToJava2D(xDataRange.getLowerBound(), dataArea, RectangleEdge.BOTTOM);
    transY = (int) this.rangeAxis.valueToJava2D(xDataRange.getLowerBound(), dataArea, RectangleEdge.LEFT);
    transX1 = (int) this.domainAxis.valueToJava2D(xDataRange.getUpperBound(), dataArea, RectangleEdge.BOTTOM);
    transY1 = (int) this.rangeAxis.valueToJava2D(xDataRange.getUpperBound(), dataArea, RectangleEdge.LEFT);

    g2bisec.drawLine(transX, transY, transX1, transY1);

    //renderer.reset();

    recurseCoweb(g2, dataArea);
}

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   w  w  w . j  av a2s . c om
    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:DefaultGraphics2D.java

/**
 * Sets the paint mode of this graphics context to overwrite the destination
 * with this graphics context's current color. This sets the logical pixel
 * operation function to the paint or overwrite mode. All subsequent rendering
 * operations will overwrite the destination with the current color.
 *//*from w  w  w  . j a  v a  2  s . c  o  m*/
public void setPaintMode() {
    gc.setComposite(AlphaComposite.SrcOver);
}