Example usage for java.awt GraphicsConfiguration createCompatibleImage

List of usage examples for java.awt GraphicsConfiguration createCompatibleImage

Introduction

In this page you can find the example usage for java.awt GraphicsConfiguration createCompatibleImage.

Prototype

public BufferedImage createCompatibleImage(int width, int height, int transparency) 

Source Link

Document

Returns a BufferedImage that supports the specified transparency and has a data layout and color model compatible with this GraphicsConfiguration .

Usage

From source file:com.josue.tileset.editor.Editor.java

private BufferedImage createTileImage() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    BufferedImage tileImage = gc.createCompatibleImage(TILE_SIZE, TILE_SIZE, Transparency.TRANSLUCENT);
    //        return new BufferedImage(Tile.TILE_SIZE, Tile.TILE_SIZE, BufferedImage.TYPE_INT_ARGB);
    return tileImage;
}

From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java

@Override
protected void paintComponent(Graphics graphics) {
    Graphics2D g = (Graphics2D) graphics;

    Rectangle rect = getInternalBounds();
    int width = rect.width;
    int height = rect.height;

    if (useBGImage) {
        // Create a translucent intermediate image in which we can perform
        // the soft clipping
        GraphicsConfiguration gc = g.getDeviceConfiguration();
        if (img == null || img.getWidth() != width || img.getHeight() != height) {
            img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        }// ww  w. ja  va 2  s  .com
        Graphics2D g2 = img.createGraphics();

        // Clear the image so all pixels have zero alpha
        g2.setComposite(AlphaComposite.Clear);
        g2.fillRect(0, 0, width, height);

        g2.setComposite(AlphaComposite.Src);
        g2.setColor(new Color(0, 0, 0, 85));
        g2.fillRect(0, 0, width, height);

        if (delegateRenderer != null) {
            delegateRenderer.render(g, g2, img);
        }

        g2.dispose();

        // Copy our intermediate image to the screen
        g.drawImage(img, rect.x, rect.y, null);
    }

    super.paintComponent(graphics);

    if (StringUtils.isNotEmpty(text)) {
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(fillColor);
        g2.fillRect(margin.left, margin.top, rect.width, rect.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(text);
        int th = fm.getHeight();
        int tx = (rect.width - tw) / 2;
        int ty = (rect.height - th) / 2;

        if (yPos != null) {
            ty = yPos;
        }

        int expand = 20;
        int arc = expand * 2;

        g2.setColor(new Color(0, 0, 0, 50));

        int x = margin.left + tx - (expand / 2);
        int y = margin.top + ty - fm.getAscent() - (expand / 2);

        drawBGContainer(g2, true, x + 4, y + 6, tw + expand, th + expand, arc, arc);

        g2.setColor(new Color(255, 255, 255, 220));
        drawBGContainer(g2, true, x, y, tw + expand, th + expand, arc, arc);

        g2.setColor(Color.DARK_GRAY);
        drawBGContainer(g2, false, x, y, tw + expand, th + expand, arc, arc);

        g2.setColor(textColor == null ? Color.BLACK : textColor);
        g2.drawString(text, tx, ty);
    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.link_and_brush.LinkAndBrushChartPanel.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (getChart() == null) {
        return;//from  w  ww.j a  va 2 s .c  om
    }
    Graphics2D g2 = (Graphics2D) g.create();

    // first determine the size of the chart rendering area...
    Dimension size = getSize();
    Insets insets = getInsets();
    Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
            size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    setChartFieldValue(getChartFieldByName("scaleX"), 1.0);
    // this.scaleX = 1.0;
    setChartFieldValue(getChartFieldByName("scaleY"), 1.0);
    // this.scaleY = 1.0;

    if (drawWidth < getMinimumDrawWidth()) {
        setChartFieldValue(getChartFieldByName("scaleX"), drawWidth / getMinimumDrawWidth());
        // this.scaleX = drawWidth / getMinimumDrawWidth();
        drawWidth = getMinimumDrawWidth();
        scale = true;
    } else if (drawWidth > getMaximumDrawWidth()) {
        setChartFieldValue(getChartFieldByName("scaleX"), drawWidth / getMaximumDrawWidth());
        // this.scaleX = drawWidth / getMaximumDrawWidth();
        drawWidth = getMaximumDrawWidth();
        scale = true;
    }

    if (drawHeight < getMinimumDrawHeight()) {
        setChartFieldValue(getChartFieldByName("scaleY"), drawHeight / getMinimumDrawHeight());
        // this.scaleY = drawHeight / getMinimumDrawHeight();
        drawHeight = getMinimumDrawHeight();
        scale = true;
    } else if (drawHeight > getMaximumDrawHeight()) {
        setChartFieldValue(getChartFieldByName("scaleY"), drawHeight / getMaximumDrawHeight());
        // this.scaleY = drawHeight / getMaximumDrawHeight();
        drawHeight = getMaximumDrawHeight();
        scale = true;
    }

    Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight);

    // are we using the chart buffer?
    if ((Boolean) getChartFieldValueByName("useBuffer")) {

        // do we need to resize the buffer?
        if ((getChartFieldValueByName("chartBuffer") == null)
                || ((Integer) getChartFieldValueByName("chartBufferWidth") != available.getWidth())
                || ((Integer) getChartFieldValueByName("chartBufferHeight") != available.getHeight())) {
            setChartFieldValue(getChartFieldByName("chartBufferWidth"), (int) available.getWidth());
            // this.chartBufferWidth = (int) available.getWidth();
            setChartFieldValue(getChartFieldByName("chartBufferHeight"), (int) available.getHeight());
            // this.chartBufferHeight = (int) available.getHeight();
            GraphicsConfiguration gc = g2.getDeviceConfiguration();
            setChartFieldValue(getChartFieldByName("chartBuffer"),
                    gc.createCompatibleImage((Integer) getChartFieldValueByName("chartBufferWidth"),
                            (Integer) getChartFieldValueByName("chartBufferHeight"), Transparency.TRANSLUCENT));
            // this.chartBuffer = gc.createCompatibleImage(this.chartBufferWidth,
            // this.chartBufferHeight, Transparency.TRANSLUCENT);
            setRefreshBuffer(true);
        }

        // do we need to redraw the buffer?
        if (getRefreshBuffer()) {

            setRefreshBuffer(false); // clear the flag

            Rectangle2D bufferArea = new Rectangle2D.Double(0, 0,
                    (Integer) getChartFieldValueByName("chartBufferWidth"),
                    (Integer) getChartFieldValueByName("chartBufferHeight"));

            Graphics2D bufferG2 = (Graphics2D) ((Image) getChartFieldValueByName("chartBuffer")).getGraphics();
            Rectangle r = new Rectangle(0, 0, (Integer) getChartFieldValueByName("chartBufferWidth"),
                    (Integer) getChartFieldValueByName("chartBufferHeight"));
            bufferG2.setPaint(getBackground());
            bufferG2.fill(r);
            if (scale) {
                AffineTransform saved = bufferG2.getTransform();
                AffineTransform st = AffineTransform.getScaleInstance(
                        (Double) getChartFieldValueByName("scaleX"),
                        (Double) getChartFieldValueByName("scaleY"));
                bufferG2.transform(st);
                getChart().draw(bufferG2, chartArea, getAnchor(), getChartRenderingInfo());
                bufferG2.setTransform(saved);
            } else {
                getChart().draw(bufferG2, bufferArea, getAnchor(), getChartRenderingInfo());
            }

        }

        // zap the buffer onto the panel...
        g2.drawImage((Image) getChartFieldValueByName("chartBuffer"), insets.left, insets.top, this);

    }

    // or redrawing the chart every time...
    else {

        AffineTransform saved = g2.getTransform();
        g2.translate(insets.left, insets.top);
        if (scale) {
            AffineTransform st = AffineTransform.getScaleInstance((Double) getChartFieldValueByName("scaleX"),
                    (Double) getChartFieldValueByName("scaleY"));
            g2.transform(st);
        }
        getChart().draw(g2, chartArea, getAnchor(), getChartRenderingInfo());
        g2.setTransform(saved);

    }

    Iterator iterator = ((List) getChartFieldValueByName("overlays")).iterator();
    while (iterator.hasNext()) {
        Overlay overlay = (Overlay) iterator.next();
        overlay.paintOverlay(g2, this);
    }

    // redraw the zoom rectangle (if present) - if useBuffer is false,
    // we use XOR so we can XOR the rectangle away again without redrawing
    // the chart
    drawZoomRectangle(g2, !(Boolean) getChartFieldValueByName("useBuffer"));

    g2.dispose();

    setAnchor(null);
    setVerticalTraceLine(null);
    setHorizontalTraceLine(null);
}

From source file:lucee.runtime.img.Image.java

public static BufferedImage toBufferedImage(java.awt.Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//  ww w  . j  a v  a2s.c om

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:net.sf.maltcms.chromaui.charts.FastHeatMapPlot.java

/**
 *
 * @param width//from  w  w  w  .j a  v a2s  .  com
 * @param height
 * @param transparency
 * @return
 */
public BufferedImage createCompatibleImage(int width, int height, int transparency) {
    GraphicsConfiguration gc = getGraphicsConfiguration();
    gc.getImageCapabilities().isAccelerated();
    return gc.createCompatibleImage(width, height, transparency);

}

From source file:Filter3dTest.java

/**
 * Creates an image compatible with the current display.
 */// www . ja v a 2  s.  c om
public BufferedImage createCompatibleImage(int w, int h, int transparancy) {
    Window window = device.getFullScreenWindow();
    if (window != null) {
        GraphicsConfiguration gc = window.getGraphicsConfiguration();
        return gc.createCompatibleImage(w, h, transparancy);
    }
    return null;
}

From source file:org.yccheok.jstock.gui.Utils.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from  www. jav a2 s  .  c om*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}