Example usage for java.awt AlphaComposite getInstance

List of usage examples for java.awt AlphaComposite getInstance

Introduction

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

Prototype

public static AlphaComposite getInstance(int rule, float alpha) 

Source Link

Document

Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source.

Usage

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);
        }/* ww  w  .  ja  v  a  2 s. c o m*/
    }
    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);//from   w  ww. java  2  s  . c  o  m

    g2.dispose();
}

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  ww w .j av  a  2s. co 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: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)/*w  w w  .j  av 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: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);/*ww w . j ava  2 s .  c o m*/
        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/*from   w  ww .  j a v a  2s  . c o  m*/
 */
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  .j  av  a 2s .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());/*from   ww w.jav  a2  s.  c o 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);
}

From source file:edu.ku.brc.ui.GradiantButton.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int w = this.getWidth();
    int h = this.getHeight();

    drawButtonBody(g2, w, h, getForeground());

    if (pressed) {
        g2.translate(1, 1);// w  ww  . ja va2  s  .co m
    }

    String text = getText();
    if (isNotEmpty(text)) {
        drawText(g2, w, h, getText());
    }

    Icon roIcon = getRolloverIcon();
    Icon paintedIcon = isHover && roIcon != null ? roIcon : icon;
    if (paintedIcon != null) {
        //Graphics2D g2 = (Graphics2D) g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, iconAlpha));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        paintedIcon.paintIcon(this, g2, (w - icon.getIconWidth()) / 2, (h - icon.getIconHeight()) / 2);
    }
}

From source file:painting.IconDisplayer.java

protected void paintComponent(Graphics g) {
    if (isOpaque()) { //paint background
        g.setColor(getBackground());/*  ww w.  j  ava 2  s  .c om*/
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    if (icon != null) {
        //Draw the icon over and over, right aligned.
        Insets insets = getInsets();
        int iconWidth = icon.getIconWidth();
        int iconX = getWidth() - insets.right - iconWidth;
        int iconY = insets.top;
        boolean faded = false;

        //Copy the Graphics object, which is actually
        //a Graphics2D object.  Cast it so we can
        //set alpha composite.
        Graphics2D g2d = (Graphics2D) g.create();

        //Draw the icons, starting from the right.
        //After the first one, the rest are faded.
        //We won't bother painting icons that are clipped.
        g.getClipBounds(clipRect);
        while (iconX >= insets.left) {
            iconRect.setBounds(iconX, iconY, iconWidth, icon.getIconHeight());
            if (iconRect.intersects(clipRect)) {
                icon.paintIcon(this, g2d, iconX, iconY);
            }
            iconX -= (iconWidth + pad);
            if (!faded) {
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
                faded = true;
            }
        }

        g2d.dispose(); //clean up
    }
}