Example usage for java.awt AlphaComposite SRC_OVER

List of usage examples for java.awt AlphaComposite SRC_OVER

Introduction

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

Prototype

int SRC_OVER

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

Click Source Link

Document

The source is composited over the destination (Porter-Duff Source Over Destination rule).

Usage

From source file:components.SizeDisplayer.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create(); //copy g
    Dimension minSize = getMinimumSize();
    Dimension prefSize = getPreferredSize();
    Dimension size = getSize();//from   w ww. j a  v a  2  s . co m
    int prefX = 0, prefY = 0;

    //Set hints so text looks nice.
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    //Draw the maximum size rectangle if we're opaque.
    if (isOpaque()) {
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, size.width, size.height);
    }

    //Draw the icon.
    if (icon != null) {
        Composite oldComposite = g2d.getComposite();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
        icon.paintIcon(this, g2d, (size.width - icon.getIconWidth()) / 2,
                (size.height - icon.getIconHeight()) / 2);
        g2d.setComposite(oldComposite);
    }

    //Draw the preferred size rectangle.
    prefX = (size.width - prefSize.width) / 2;
    prefY = (size.height - prefSize.height) / 2;
    g2d.setColor(Color.RED);
    g2d.drawRect(prefX, prefY, prefSize.width - 1, prefSize.height - 1);

    //Draw the minimum size rectangle.
    if (minSize.width != prefSize.width || minSize.height != prefSize.height) {
        int minX = (size.width - minSize.width) / 2;
        int minY = (size.height - minSize.height) / 2;
        g2d.setColor(Color.CYAN);
        g2d.drawRect(minX, minY, minSize.width - 1, minSize.height - 1);
    }

    //Draw the text.
    if (text != null) {
        Dimension textSize = getTextSize(g2d);
        g2d.setColor(getForeground());
        g2d.drawString(text, (size.width - textSize.width) / 2,
                (size.height - textSize.height) / 2 + g2d.getFontMetrics().getAscent());
    }
    g2d.dispose();
}

From source file:ShowOff.java

protected void drawImageMosaic(Graphics2D g2) {
    int side = 36;
    int width = mImage.getWidth();
    int height = mImage.getHeight();
    for (int y = 0; y < height; y += side) {
        for (int x = 0; x < width; x += side) {
            float xBias = (float) x / (float) width;
            float yBias = (float) y / (float) height;
            float alpha = 1.0f - Math.abs(xBias - yBias);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            int w = Math.min(side, width - x);
            int h = Math.min(side, height - y);
            BufferedImage tile = mImage.getSubimage(x, y, w, h);
            g2.drawImage(tile, x, y, null);
        }//w w  w . ja v  a 2s  .c om
    }
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
}

From source file:Wallpaper.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    Graphics2D g2 = (Graphics2D) g.create();

    int w = c.getWidth();
    int h = c.getHeight();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
    g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red));
    g2.fillRect(0, 0, w, h);/* ww  w . j ava 2 s.com*/

    g2.dispose();
}

From source file:Composite.java

public int getRule(int rule) {
    int alphaComp = 0;
    switch (rule) {
    case 0://from   w w  w .  ja  v  a 2s .  c  o  m
        alphaComp = AlphaComposite.SRC;
        break;
    case 1:
        alphaComp = AlphaComposite.DST_IN;
        break;
    case 2:
        alphaComp = AlphaComposite.DST_OUT;
        break;
    case 3:
        alphaComp = AlphaComposite.DST_OVER;
        break;
    case 4:
        alphaComp = AlphaComposite.SRC_IN;
        break;
    case 5:
        alphaComp = AlphaComposite.SRC_OVER;
        break;
    case 6:
        alphaComp = AlphaComposite.SRC_OUT;
        break;
    case 7:
        alphaComp = AlphaComposite.CLEAR;
        break;
    }
    return alphaComp;
}

From source file:org.messic.server.facade.controllers.pages.CaptchaController.java

@ApiMethod(path = "/captcha", verb = ApiVerb.GET, description = "Get a captcha", produces = {
        MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ApiErrors(apierrors = { @ApiError(code = UnknownMessicRESTException.VALUE, description = "Unknown error") })
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)//from  w ww . j a v a  2  s  .c o m
@ResponseBody
@ApiResponseObject
public Captcha getCaptcha() throws UnknownMessicRESTException {

    try {
        UUID idOne = UUID.randomUUID();
        // create the image with the text
        BufferedImage bi = service.getImageChallengeForID(idOne.toString());
        BufferedImage bi2 = Util.ImagedeepCopy(bi);
        Graphics2D g2d = (Graphics2D) bi.getGraphics();
        float alpha = 0.25f;
        int type = AlphaComposite.SRC_OVER;
        AlphaComposite composite = AlphaComposite.getInstance(type, alpha);
        g2d.setComposite(composite);

        final int Min = 5;
        final int Max = 30;
        int random1 = Min + (int) (Math.random() * ((Max - Min) + 1));
        int random2 = Min + (int) (Math.random() * ((Max - Min) + 1));

        g2d.drawImage(bi2, random1, random2, null);

        alpha = 0.80f;
        type = AlphaComposite.SRC_OVER;
        composite = AlphaComposite.getInstance(type, alpha);
        g2d.setComposite(composite);

        int MinX = 0;
        int MaxX = bi.getWidth();
        int MinY = 0;
        int MaxY = bi.getHeight();
        g2d.setColor(Color.black);
        for (int i = 0; i < random2; i++) {
            int random3 = MinX + (int) (Math.random() * ((MaxX - MinX) + 1));
            int random4 = MinX + (int) (Math.random() * ((MaxX - MinX) + 1));
            int random5 = MinY + (int) (Math.random() * ((MaxY - MinY) + 1));
            int random6 = MinY + (int) (Math.random() * ((MaxY - MinY) + 1));
            g2d.drawLine(random3, random5, random4, random6);
        }

        Captcha result = new Captcha();
        result.id = idOne.toString();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", baos);
        byte[] resultb64 = Base64.encode(baos.toByteArray());
        result.captchaImage = new String(resultb64);
        return result;
    } catch (Exception e) {
        throw new UnknownMessicRESTException(e);
    }
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static final void yellowHighlight(Image image, Collection<Rectangle> boxes, float outsetFactor) {
    if (CollectionUtils.isEmpty(boxes))
        return;//from  w w  w  . jav  a  2s  . c o m
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setPaint(Color.YELLOW);
    Composite originalComposite = g2d.getComposite();
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
    g2d.setComposite(ac);
    for (Rectangle box : boxes) {
        if (outsetFactor != 1.0F) {
            box = new Rectangle(box);
            box.grow((int) (box.getHeight() * outsetFactor), (int) (box.getWidth() * outsetFactor));
        }
        g2d.fill(box);
    }
    g2d.setComposite(originalComposite);
}

From source file:Diva.java

@Override
public void paint(Graphics g, JComponent c) {
    Graphics2D g2 = (Graphics2D) g.create();

    // Paint the view.
    super.paint(g2, c);

    if (mActive) {
        // Create a radial gradient, transparent in the middle.
        java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(mX, mY);
        float radius = 72;
        float[] dist = { 0.0f, 1.0f };
        Color[] colors = { new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK };
        RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors);
        g2.setPaint(p);/*from  w w w .  j  a  v a  2s .  c om*/
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f));
        g2.fillRect(0, 0, c.getWidth(), c.getHeight());
    }

    g2.dispose();
}

From source file:savant.view.swing.Ruler.java

/**
 * Render the background of this graphpane
 * @param g The graphics object to use/*  w  ww.j ava2 s .c om*/
 */
private void renderBackground(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    try {

        Image image = javax.imageio.ImageIO
                .read(getClass().getResource("/savant/images/bar_selected_glossy.png"));
        Composite originalComposite = ((Graphics2D) g).getComposite();
        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.85F));
        g.drawImage(image, -getWidth(), 0, getWidth() * 3, getHeight(), this);
        ((Graphics2D) g).setComposite(originalComposite);
    } catch (Exception e) {
        LOG.error("Error drawing image background");
    }

    Range r = locationController.getRange();

    // At early points in the GUI initialisation, the range has not yet been set.
    if (r == null) {
        return;
    }

    int[] tickPositions = MiscUtils.getTickPositions(r);
    int separation = tickPositions.length > 1 ? tickPositions[1] - tickPositions[0] : 0;
    int xEnd = Integer.MIN_VALUE;
    FontMetrics fm = g2.getFontMetrics();

    for (int p : tickPositions) {

        int x = MiscUtils.transformPositionToPixel(p, getWidth(), r);
        if (x > xEnd + 10) {
            g2.setColor(new Color(50, 50, 50, 50)); //BrowserDefaults.colorAxisGrid);
            g2.drawLine(x, 0, x, getHeight());

            String numStr = MiscUtils.posToShortStringWithSeparation(p, separation);
            g2.setColor(Color.black);
            g2.drawString(numStr, x + 3, getHeight() / 2 + 3);
            xEnd = x + fm.stringWidth(numStr) + 3;
        }
    }

    if (r.getLength() >= locationController.getRangeStart()) {
        try {
            Image image_left_cap = javax.imageio.ImageIO
                    .read(getClass().getResource("/savant/images/round_cap_left_bordered.png"));
            int pos = getLeftCapPos();
            g.drawImage(image_left_cap, pos, 0, CAP_WIDTH, CAP_HEIGHT, this);
            g.setColor(Savant.getInstance().getBackground());
            g.fillRect(pos, 0, -getWidth(), getHeight());
        } catch (IOException ex) {
            LOG.error("Drawing failed.", ex);
        }
    }

    if (r.getLength() >= locationController.getMaxRangeEnd() - locationController.getRangeEnd()) {
        try {
            Image image_right_cap = javax.imageio.ImageIO
                    .read(getClass().getResource("/savant/images/round_cap_right_bordered.png"));
            int pos = MiscUtils.transformPositionToPixel(locationController.getMaxRangeEnd(), getWidth(),
                    locationController.getRange());
            g.drawImage(image_right_cap, pos - CAP_WIDTH, 0, CAP_WIDTH, CAP_HEIGHT, this);
            g.setColor(Savant.getInstance().getBackground());
            g.fillRect(pos, 0, this.getWidth(), this.getHeight());

        } catch (IOException ex) {
            LOG.error("Drawing failed.", ex);
        }
    }
}

From source file:TapTapTap.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    // Paint the view.
    super.paint(g, c);

    if (!mIsRunning) {
        return;/*from  w ww  . ja va2s  .  c o m*/
    }

    Graphics2D g2 = (Graphics2D) g.create();

    float fade = (float) mFadeCount / (float) mFadeLimit;
    // Gray it out.
    Composite urComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f * fade));
    g2.fillRect(0, 0, w, h);
    g2.setComposite(urComposite);

    // Paint the wait indicator.
    int s = Math.min(w, h) / 5;
    int cx = w / 2;
    int cy = h / 2;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g2.setPaint(Color.white);
    g2.rotate(Math.PI * mAngle / 180, cx, cy);
    for (int i = 0; i < 12; i++) {
        float scale = (11.0f - (float) i) / 11.0f;
        g2.drawLine(cx + s, cy, cx + s * 2, cy);
        g2.rotate(-Math.PI / 6, cx, cy);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * fade));
    }

    g2.dispose();
}

From source file:edu.cuny.jfree.chart.annotations.CategoryIntervalAnnotation.java

public void draw(final Graphics2D g2, final CategoryPlot plot, final Rectangle2D dataArea,
        final CategoryAxis domainAxis, final ValueAxis rangeAxis) {

    final AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            plot.getForegroundAlpha());/*  ww w.j  a va 2  s.co m*/
    final Composite oldComposite = g2.getComposite();
    g2.setComposite(alphaComposite);

    final CategoryDataset dataset = plot.getDataset();
    final int catIndex = dataset.getColumnIndex(category);
    final int catCount = dataset.getColumnCount();
    double lineX1 = 0.0D;
    double lineY = 0.0D;
    double lineX2 = 0.0D;
    final PlotOrientation orientation = plot.getOrientation();
    final RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
    final RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);
    if (orientation == PlotOrientation.HORIZONTAL) {
        lineY = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX1 = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX2 = rangeAxis.valueToJava2D(value2, dataArea, rangeEdge);
    } else if (orientation == PlotOrientation.VERTICAL) {
        lineY = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX1 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX2 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
    }
    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.drawLine((int) lineX1, (int) lineY, (int) lineX2, (int) lineY);

    g2.setComposite(oldComposite);
}