Example usage for java.awt Graphics2D getDeviceConfiguration

List of usage examples for java.awt Graphics2D getDeviceConfiguration

Introduction

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

Prototype

public abstract GraphicsConfiguration getDeviceConfiguration();

Source Link

Document

Returns the device configuration associated with this Graphics2D .

Usage

From source file:RenderingUtils.java

private static Map desktopHints(Graphics2D g2) {
    if (isPrinting(g2)) {
        return null;
    }/*from  www. j  av  a 2s. co  m*/
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    GraphicsDevice device = g2.getDeviceConfiguration().getDevice();
    Map desktopHints = (Map) toolkit.getDesktopProperty(PROP_DESKTOPHINTS + '.' + device.getIDstring());
    if (desktopHints == null) {
        desktopHints = (Map) toolkit.getDesktopProperty(PROP_DESKTOPHINTS);
    }
    // It is possible to get a non-empty map but with disabled AA.
    if (desktopHints != null) {
        Object aaHint = desktopHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
        if ((aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)
                || (aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) {
            desktopHints = null;
        }
    }
    return desktopHints;
}

From source file:GraphicsInfo.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    GraphicsConfiguration gc = g2d.getDeviceConfiguration();
    printModelType(gc.getColorModel());/*from ww  w .ja v a2 s .com*/
    BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D g2d2 = bi.createGraphics();
    GraphicsConfiguration gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
    bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
    g2d2 = bi.createGraphics();
    gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
    bi = new BufferedImage(20, 20, BufferedImage.TYPE_USHORT_565_RGB);
    g2d2 = bi.createGraphics();
    gc2 = g2d2.getDeviceConfiguration();
    printModelType(gc2.getColorModel());
}

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);
        }/*from  w  w w .  j av a2 s  .co m*/
        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;//  ww w.  j av  a  2 s. c  o  m
    }
    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:org.bitstrings.maven.plugins.splasher.DrawingUtil.java

public static Rectangle getDrawingBounds(Graphics2D g) {
    return ObjectUtils.defaultIfNull(g.getClipBounds(), g.getDeviceConfiguration().getBounds());
}

From source file:org.squidy.designer.util.DrawableString.java

/**
 * @param g//w w w. j  a v  a 2s .co m
 */
private void update(Graphics2D g, double viewScale) {
    Cache.Item item;

    // use screen if no bounds set
    if (bounds == null) {
        bounds = g.getDeviceConfiguration().getBounds();
    }

    // get item extents from cache or calculate
    if (cache == null || dirty) {
        cache = new Cache();
        item = null;
    } else {
        item = cache.get(viewScale);
    }

    if (item != null) {
        drawValue = item.drawValue;
        positionX = item.positionX;
    } else {
        FontMetrics fm;
        int width, height;

        // font width does not scale linear to view scale
        fm = g.getFontMetrics();
        drawValue = FontUtils.createCroppedLabelIfNecessary(fm, value, bounds.width - 50);

        positionX = bounds.x;
        if (alignmentH == AlignmentH.CENTER) {
            width = FontUtils.getWidthOfText(fm, drawValue);
            positionX += (bounds.width - width) / 2;
        } else if (alignmentH == AlignmentH.RIGHT) {
            width = FontUtils.getWidthOfText(fm, drawValue);
            positionX += (bounds.width - width);
        }

        // font height keeps the same all the time
        if (dirty) {
            positionY = bounds.y;
            if (alignmentV == AlignmentV.CENTER) {
                height = (int) fm.getLineMetrics(drawValue, g).getHeight();
                positionY += (bounds.height - height) / 2;
            } else if (alignmentV == AlignmentV.BOTTOM) {
                height = (int) fm.getLineMetrics(drawValue, g).getHeight();
                positionY += (bounds.height - height);
            }
        }

        // store extents in cache
        cache.put(drawValue, positionX, viewScale);
    }

    dirty = false;
}