Example usage for java.awt Graphics2D setComposite

List of usage examples for java.awt Graphics2D setComposite

Introduction

In this page you can find the example usage for java.awt Graphics2D setComposite.

Prototype

public abstract void setComposite(Composite comp);

Source Link

Document

Sets the Composite for the Graphics2D context.

Usage

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

@Override
protected void paintComponent(Graphics g) {
    if (portrait == null) {
        super.paintComponent(g);
        return;//from w  w  w .jav a  2s  .  c  o m
    }
    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);
    }
}

From source file:WeatherWizard.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Dimension size = getSize();//from ww  w  .j  av a2 s .  c o m
    Composite origComposite;

    setupWeatherReport();

    origComposite = g2.getComposite();
    if (alpha0 != null)
        g2.setComposite(alpha0);
    g2.drawImage(img0, 0, 0, size.width, size.height, 0, 0, img0.getWidth(null), img0.getHeight(null), null);
    if (img1 != null) {
        if (alpha1 != null)
            g2.setComposite(alpha1);
        g2.drawImage(img1, 0, 0, size.width, size.height, 0, 0, img1.getWidth(null), img1.getHeight(null),
                null);
    }
    g2.setComposite(origComposite);

    // Freezing, Cold, Cool, Warm, Hot,
    // Blue, Green, Yellow, Orange, Red
    Font font = new Font("Serif", Font.PLAIN, 36);
    g.setFont(font);

    String tempString = feels + " " + temperature + "F";
    FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
    Rectangle2D boundsTemp = font.getStringBounds(tempString, frc);
    Rectangle2D boundsCond = font.getStringBounds(condStr, frc);
    int wText = Math.max((int) boundsTemp.getWidth(), (int) boundsCond.getWidth());
    int hText = (int) boundsTemp.getHeight() + (int) boundsCond.getHeight();
    int rX = (size.width - wText) / 2;
    int rY = (size.height - hText) / 2;

    g.setColor(Color.LIGHT_GRAY);
    g2.fillRect(rX, rY, wText, hText);

    g.setColor(textColor);
    int xTextTemp = rX - (int) boundsTemp.getX();
    int yTextTemp = rY - (int) boundsTemp.getY();
    g.drawString(tempString, xTextTemp, yTextTemp);

    int xTextCond = rX - (int) boundsCond.getX();
    int yTextCond = rY - (int) boundsCond.getY() + (int) boundsTemp.getHeight();
    g.drawString(condStr, xTextCond, yTextCond);

}

From source file:net.technicpack.ui.lang.ResourceLoader.java

public BufferedImage colorImage(BufferedImage loadImg, Color color) {
    BufferedImage img = new BufferedImage(loadImg.getWidth(), loadImg.getHeight(), BufferedImage.TRANSLUCENT);
    Graphics2D graphics = img.createGraphics();

    graphics.setColor(color);/*from w w w.  ja v a 2s.  com*/
    graphics.drawImage(loadImg, null, 0, 0);
    graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f));
    graphics.fillRect(0, 0, loadImg.getWidth(), loadImg.getHeight());

    graphics.dispose();
    return img;
}

From source file:TextureWithBufferedImage.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Rectangle2D rec1, rec2, rec3, rec4, rec5;
    rec1 = new Rectangle2D.Float(25, 25, 75, 150);
    rec2 = new Rectangle2D.Float(125, 25, 10, 75);
    rec3 = new Rectangle2D.Float(75, 125, 125, 75);
    rec4 = new Rectangle2D.Float(25, 15, 12, 75);
    rec5 = new Rectangle2D.Float(15, 50, 15, 15);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1);
    g2D.setComposite(ac);

    g2D.setStroke(new BasicStroke(5.0f));
    g2D.draw(rec1);//w  ww  .  jav a 2s  .co m
    GradientPaint gp = new GradientPaint(125f, 25f, Color.yellow, 225f, 100f, Color.blue);
    g2D.setPaint(gp);
    g2D.fill(rec2);
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.magenta);
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.black);
    big.drawLine(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    TexturePaint tp = new TexturePaint(bi, r);

    g2D.setPaint(tp);
    g2D.fill(rec3);
    g2D.setColor(Color.green);
    g2D.fill(rec4);
    g2D.setColor(Color.red);
    g2D.fill(rec5);
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java

private void createImageStream(PDDocument doc, BufferedImage bi) throws IOException {
    BufferedImage alphaImage = null;
    BufferedImage rgbImage = null;
    int width = bi.getWidth();
    int height = bi.getHeight();
    if (bi.getColorModel().hasAlpha()) {
        // extract the alpha information
        WritableRaster alphaRaster = bi.getAlphaRaster();
        ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false,
                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        alphaImage = new BufferedImage(cm, alphaRaster, false, null);
        // create a RGB image without alpha
        rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = rgbImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(bi, 0, 0, null);//from ww w  . ja  v a  2s. com
    } else {
        rgbImage = bi;
    }
    java.io.OutputStream os = null;
    try {
        int numberOfComponents = rgbImage.getColorModel().getNumComponents();
        if (numberOfComponents == 3) {
            setColorSpace(PDDeviceRGB.INSTANCE);
        } else {
            if (numberOfComponents == 1) {
                setColorSpace(new PDDeviceGray());
            } else {
                throw new IllegalStateException();
            }
        }
        byte[] outData = new byte[width * height * numberOfComponents];
        rgbImage.getData().getDataElements(0, 0, width, height, outData);
        // add FlateDecode compression
        getPDStream().addCompression();
        os = getCOSStream().createUnfilteredStream();
        os.write(outData);

        COSDictionary dic = getCOSStream();
        dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);
        dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
        dic.setItem(COSName.TYPE, COSName.XOBJECT);
        if (alphaImage != null) {
            PDPixelMap smask = new PDPixelMap(doc, alphaImage);
            dic.setItem(COSName.SMASK, smask);
        }
        setBitsPerComponent(8);
        setHeight(height);
        setWidth(width);
    } finally {
        os.close();
    }
}

From source file:Composite.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    Dimension d = getSize();/*  www .j  a  v  a  2s .  c  o  m*/
    int w = d.width;
    int h = d.height;

    BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    g2.setColor(Color.white);
    g2.fillRect(0, 0, d.width, d.height);

    int rectx = w / 4;
    int recty = h / 4;

    gbi.setColor(new Color(0.0f, 0.0f, 1.0f, 1.0f));
    gbi.fill(new Rectangle2D.Double(rectx, recty, 150, 100));
    gbi.setColor(new Color(1.0f, 0.0f, 0.0f, 1.0f));
    gbi.setComposite(ac);
    gbi.fill(new Ellipse2D.Double(rectx + rectx / 2, recty + recty / 2, 150, 100));

    g2.drawImage(buffImg, null, 0, 0);
}

From source file:com.appnativa.rare.ui.chart.jfreechart.XYPlotEx.java

@Override
protected void fillBackground(Graphics2D g2, Rectangle2D area, PlotOrientation orientation) {
    Paint p = this.getBackgroundPaint();

    if (p == null) {
        return;/*from w ww  . j  av  a 2  s  .c  om*/
    }

    iPainter painter = ChartHelper.getPainter(p);

    if (painter == null) {
        super.fillBackground(g2, area, orientation);

        return;
    }

    Composite originalComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getBackgroundAlpha()));
    graphics = SwingGraphics.fromGraphics(g2, null, graphics);

    float x = UIScreen.snapToPosition(area.getX());
    float y = UIScreen.snapToPosition(area.getY());
    float w = UIScreen.snapToSize(area.getWidth());
    float h = UIScreen.snapToSize(area.getHeight());

    painter.paint(graphics, x, y, w, h, iPainter.UNKNOWN);
    g2.setComposite(originalComposite);
}

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);
        }/*from   w  w w. j a  va2 s .com*/
    }
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
}

From source file:dbseer.gui.panel.DBSeerExplainChartPanel.java

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

    selectRectangle = new Rectangle2D.Double(x, y, width, height);
    g2d.setPaint(Color.black);//from  w w  w  .  j  ava2 s  .  c  o  m
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2d.setComposite(c);
    g2d.fill(selectRectangle);
}

From source file:de.dakror.villagedefense.game.entity.Entity.java

public void drawEntity(Graphics2D g) {
    if (alpha == 0)
        return;//ww  w .  j  a va2  s  . c o m

    drawBump(g, false);
    Composite c = g.getComposite();
    if (this instanceof Struct)
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    draw(g);
    g.setComposite(c);

    if (isHungry()) {
        g.drawImage(Game.getImage("icon/hunger.png"), (int) x,
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    } else if (this instanceof Struct && !((Struct) this).isWorking()) {
        g.drawImage(Game.getImage("icon/sleep.png"), (int) (x + width * 0.75f),
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    }

    drawBump(g, true);
}