Example usage for java.awt Graphics2D setRenderingHint

List of usage examples for java.awt Graphics2D setRenderingHint

Introduction

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

Prototype

public abstract void setRenderingHint(Key hintKey, Object hintValue);

Source Link

Document

Sets the value of a single preference for the rendering algorithms.

Usage

From source file:freemind.controller.Controller.java

public void setTextRenderingHint(Graphics2D g) {
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            (getAntialiasAll()) ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
                    : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            (getAntialiasAll()) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
}

From source file:com.moviejukebox.plugin.DefaultImagePlugin.java

/**
 * Draw a frame around the image; color depends on resolution if wanted
 *
 * @param movie//from   w ww .  ja  v a  2s.co m
 * @param bi
 * @return
 */
private BufferedImage drawFrame(Movie movie, BufferedImage bi) {
    BufferedImage newImg = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D newGraphics = newImg.createGraphics();
    newGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int cornerRadius2 = 0;

    if (!movie.isHD()) {
        String[] colorSD = frameColorSD.split("/");
        int[] lengthSD = new int[colorSD.length];
        for (int i = 0; i < colorSD.length; i++) {
            lengthSD[i] = Integer.parseInt(colorSD[i]);
        }
        newGraphics.setPaint(new Color(lengthSD[0], lengthSD[1], lengthSD[2]));
    } else if (highdefDiff) {
        if (movie.isHD()) {
            // Otherwise use the 720p
            String[] color720 = frameColor720.split("/");
            int[] length720 = new int[color720.length];
            for (int i = 0; i < color720.length; i++) {
                length720[i] = Integer.parseInt(color720[i]);
            }
            newGraphics.setPaint(new Color(length720[0], length720[1], length720[2]));
        }

        if (movie.isHD1080()) {
            String[] color1080 = frameColor1080.split("/");
            int[] length1080 = new int[color1080.length];
            for (int i = 0; i < color1080.length; i++) {
                length1080[i] = Integer.parseInt(color1080[i]);
            }
            newGraphics.setPaint(new Color(length1080[0], length1080[1], length1080[2]));
        }
    } else {
        // We don't care, so use the default HD logo.
        String[] colorHD = frameColorHD.split("/");
        int[] lengthHD = new int[colorHD.length];
        for (int i = 0; i < colorHD.length; i++) {
            lengthHD[i] = Integer.parseInt(colorHD[i]);
        }
        newGraphics.setPaint(new Color(lengthHD[0], lengthHD[1], lengthHD[2]));
    }

    if (roundCorners) {
        cornerRadius2 = cornerRadius;
    }

    RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, bi.getWidth(), bi.getHeight(),
            rcqFactor * cornerRadius2, rcqFactor * cornerRadius2);
    newGraphics.setClip(rect);

    // image fitted into border
    newGraphics.drawImage(bi, (int) (rcqFactor * frameSize - 1), (int) (rcqFactor * frameSize - 1),
            (int) (bi.getWidth() - (rcqFactor * frameSize * 2) + 2),
            (int) (bi.getHeight() - (rcqFactor * frameSize * 2) + 2), null);

    BasicStroke s4 = new BasicStroke(rcqFactor * frameSize * 2);

    newGraphics.setStroke(s4);
    newGraphics.draw(rect);
    newGraphics.dispose();

    return newImg;
}

From source file:MyJava3D.java

public Graphics2D createGraphics2D(int width, int height, BufferedImage bi, Graphics g) {

    Graphics2D g2 = null;

    if (bi != null) {
        g2 = bi.createGraphics();/*  w ww  . j  a  va 2 s.  c o  m*/
    } else {
        g2 = (Graphics2D) g;
    }

    g2.setBackground(getBackground());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, Rendering);

    if (clearSurface || clearOnce) {
        g2.clearRect(0, 0, width, height);
        clearOnce = false;
    }

    if (texture != null) {
        // set composite to opaque for texture fills
        g2.setComposite(AlphaComposite.SrcOver);
        g2.setPaint(texture);
        g2.fillRect(0, 0, width, height);
    }

    if (composite != null) {
        g2.setComposite(composite);
    }

    return g2;
}

From source file:freemind.controller.Controller.java

public Object setEdgesRenderingHint(Graphics2D g) {
    Object renderingHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            (getAntialiasEdges()) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    return renderingHint;
}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

/**
 *
 *///from   w ww .  j a v  a2s .  com
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.GRAY);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    if (bufferedImage != null) {
        scaleGraphicsContext(g2);
        g2.drawImage(bufferedImage, 0, 0, null);
        drawTextBoxes(g2);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.graphics.internal.LogicalPageDrawable.java

protected void processOtherNode(final RenderNode node) {
    if (node.isNodeVisible(drawArea) == false) {
        return;//  w  w w .  ja  v  a  2 s .c om
    }

    final int type = node.getNodeType();
    if (isTextLineOverflow()) {
        if (node.isVirtualNode()) {
            if (ellipseDrawn == false) {
                if (isClipOnWordBoundary() == false && type == LayoutNodeTypes.TYPE_NODE_TEXT) {
                    final RenderableText text = (RenderableText) node;
                    final long ellipseSize = extractEllipseSize(node);
                    final long x1 = text.getX();
                    final long effectiveAreaX2 = (contentAreaX2 - ellipseSize);

                    if (x1 < contentAreaX2) {
                        // The text node that is printed will overlap with the ellipse we need to print.
                        drawText(text, effectiveAreaX2);
                    }
                } else if (isClipOnWordBoundary() == false && type == LayoutNodeTypes.TYPE_NODE_COMPLEX_TEXT) {
                    final RenderableComplexText text = (RenderableComplexText) node;
                    // final long ellipseSize = extractEllipseSize(node);
                    final long x1 = text.getX();
                    // final long effectiveAreaX2 = (contentAreaX2 - ellipseSize);

                    if (x1 < contentAreaX2) {
                        // The text node that is printed will overlap with the ellipse we need to print.
                        final Graphics2D g2;
                        if (getTextSpec() == null) {
                            g2 = (Graphics2D) getGraphics().create();
                            final StyleSheet layoutContext = text.getStyleSheet();
                            configureGraphics(layoutContext, g2);
                            g2.setStroke(LogicalPageDrawable.DEFAULT_STROKE);

                            if (RenderUtility.isFontSmooth(layoutContext, metaData)) {
                                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                            } else {
                                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                                        RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
                            }
                        } else {
                            g2 = getTextSpec().getGraphics();
                        }

                        drawComplexText(text, g2);
                    }
                }

                ellipseDrawn = true;

                final RenderBox parent = node.getParent();
                if (parent != null) {
                    final RenderBox textEllipseBox = parent.getTextEllipseBox();
                    if (textEllipseBox != null) {
                        processBoxChilds(textEllipseBox);
                    }
                }
                return;
            }
        }
    }

    if (type == LayoutNodeTypes.TYPE_NODE_TEXT) {
        final RenderableText text = (RenderableText) node;
        if (underline != null) {
            final ExtendedBaselineInfo baselineInfo = text.getBaselineInfo();
            final long underlinePos = text.getY() + baselineInfo.getUnderlinePosition();
            underline.updateVerticalPosition(StrictGeomUtility.toExternalValue(underlinePos));
            underline.updateStart(StrictGeomUtility.toExternalValue(text.getX()));
            underline.updateEnd(StrictGeomUtility.toExternalValue(text.getX() + text.getWidth()));
        }

        if (strikeThrough != null) {
            final ExtendedBaselineInfo baselineInfo = text.getBaselineInfo();
            final long strikethroughPos = text.getY() + baselineInfo.getStrikethroughPosition();
            strikeThrough.updateVerticalPosition(StrictGeomUtility.toExternalValue(strikethroughPos));
            strikeThrough.updateStart(StrictGeomUtility.toExternalValue(text.getX()));
            strikeThrough.updateEnd(StrictGeomUtility.toExternalValue(text.getX() + text.getWidth()));
        }

        if (isTextLineOverflow()) {
            final long ellipseSize = extractEllipseSize(node);
            final long x1 = text.getX();
            final long x2 = x1 + text.getWidth();
            final long effectiveAreaX2 = (contentAreaX2 - ellipseSize);
            if (x2 <= effectiveAreaX2) {
                // the text will be fully visible.
                drawText(text);
            } else {
                if (x1 < contentAreaX2) {
                    // The text node that is printed will overlap with the ellipse we need to print.
                    drawText(text, effectiveAreaX2);
                }

                final RenderBox parent = node.getParent();
                if (parent != null) {
                    final RenderBox textEllipseBox = parent.getTextEllipseBox();
                    if (textEllipseBox != null) {
                        processBoxChilds(textEllipseBox);
                    }
                }

                ellipseDrawn = true;
            }
        } else {
            drawText(text);
        }
    } else if (type == LayoutNodeTypes.TYPE_NODE_COMPLEX_TEXT) {
        final RenderableComplexText text = (RenderableComplexText) node;
        final long x1 = text.getX();

        if (x1 < contentAreaX2) {
            // The text node that is printed will overlap with the ellipse we need to print.
            final Graphics2D g2;
            if (getTextSpec() == null) {
                g2 = (Graphics2D) getGraphics().create();
                final StyleSheet layoutContext = text.getStyleSheet();
                configureGraphics(layoutContext, g2);
                g2.setStroke(LogicalPageDrawable.DEFAULT_STROKE);

                if (RenderUtility.isFontSmooth(layoutContext, metaData)) {
                    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                } else {
                    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                            RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
                }
            } else {
                g2 = getTextSpec().getGraphics();
            }

            drawComplexText(text, g2);
        }
    }
}

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

public void resizeImage(int width, int height, int interpolation) throws ExpressionException {
    Object ip;/*from w  w w. j a  va2  s . c  o  m*/
    if (interpolation == IPC_NEAREST)
        ip = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
    else if (interpolation == IPC_BICUBIC)
        ip = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
    else if (interpolation == IPC_BILINEAR)
        ip = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
    else
        throw new ExpressionException("invalid interpoltion definition");

    BufferedImage dst = new BufferedImage(width, height, image().getType());
    Graphics2D graphics = dst.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, ip);
    graphics.drawImage(image(), 0, 0, width, height, null);
    graphics.dispose();
    image(dst);

}

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

public void setAntiAliasing(boolean antiAlias) throws ExpressionException {
    this.antiAlias = antiAlias ? ANTI_ALIAS_ON : ANTI_ALIAS_OFF;
    Graphics2D graphics = getGraphics();
    if (antiAlias) {
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {/*  www.ja v  a2 s. c o m*/
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }
}

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

/**
  * Convenience method that returns a scaled instance of the
  * provided {@code BufferedImage}.//w  ww. ja v a2s  . c o m
  *
  * @param img the original image to be scaled
  * @param targetWidth the desired width of the scaled instance,
  *    in pixels
  * @param targetHeight the desired height of the scaled instance,
  *    in pixels
  * @param hint one of the rendering hints that corresponds to
  *    {@code RenderingHints.KEY_INTERPOLATION} (e.g.
  *    {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
  *    {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
  *    {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
  * @param higherQuality if true, this method will use a multi-step
  *    scaling technique that provides higher quality than the usual
  *    one-step technique (only useful in downscaling cases, where
  *    {@code targetWidth} or {@code targetHeight} is
  *    smaller than the original dimensions, and generally only when
  *    the {@code BILINEAR} hint is specified)
  * @return a scaled version of the original {@code BufferedImage}
  */
private BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    // functionality not supported in java 1.4
    int transparency = Transparency.OPAQUE;
    try {
        transparency = img.getTransparency();
    } catch (Throwable t) {
    }
    int type = (transparency == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

    BufferedImage ret = img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:edu.ku.brc.specify.tasks.subpane.qb.QueryFieldPanel.java

@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);

    if (selected) {
        //this block was copied from RolloverCommand.paintComp()

        g.setColor(RolloverCommand.getActiveColor());
        Insets insets = getInsets();
        insets.set(1, 1, 1, 1);/*from w ww  .j a  va2  s  .  co m*/
        Dimension size = getSize();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rr = new RoundRectangle2D.Double(insets.left, insets.top,
                size.width - insets.right - insets.left, size.height - insets.bottom - insets.top, 10, 10);
        g2d.draw(rr);
        rr = new RoundRectangle2D.Double(insets.left + 1, insets.top + 1,
                size.width - insets.right - insets.left - 2, size.height - insets.bottom - insets.top - 2, 10,
                10);
        g2d.draw(rr);
    }
}