Example usage for java.awt Graphics2D getRenderingHint

List of usage examples for java.awt Graphics2D getRenderingHint

Introduction

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

Prototype

public abstract Object getRenderingHint(Key hintKey);

Source Link

Document

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

Usage

From source file:Main.java

/**
 * Tell the G to use anti aliased drawing and text
 * @return old AA/*  w ww.ja  v  a  2  s  .c o m*/
 */
public static Object useAntiAliasing(Graphics2D g) {
    Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    return oldAA;
}

From source file:Main.java

public static void drawRoundBorder(Graphics g, Color c, int x, int y, int w, int h, int r) {
    Graphics2D g2D = (Graphics2D) g;
    Object savedRederingHint = g2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2D.setColor(c);/*from w w w  .  j  a  v  a2 s. c  o  m*/
    g2D.drawRoundRect(x, y, w - 1, h - 1, r, r);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint);
}

From source file:Main.java

/**
 * Fills a gradient using the start color and end color specified.
 *
 * @param g//  w w  w .  j  ava2 s . c  om
 *          the gradient
 * @param s
 *          the shape to fill
 * @param start
 *          the start color
 * @param end
 *          the end color
 * @param vertical
 *          true for a vertical gradient; false for horizontal
 */
public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) {
    Rectangle r = s.getBounds();
    Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end);
    Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setPaint(gp);
    g.fill(s);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o);
}

From source file:Main.java

/**
 * Returns map with rendering hints from a Graphics instance.
 *
 * @param g2d         Graphics instance/*from  w  w  w.ja  v a2 s  . c  o m*/
 * @param hintsToSave map of RenderingHint key-values
 * @param savedHints  map to save hints into
 * @return map with rendering hints from a Graphics instance
 */
private static Map getRenderingHints(final Graphics2D g2d, final Map hintsToSave, Map savedHints) {
    if (savedHints == null) {
        savedHints = new RenderingHints(null);
    } else {
        savedHints.clear();
    }
    if (hintsToSave == null || hintsToSave.size() == 0) {
        return savedHints;
    }
    final Set objects = hintsToSave.keySet();
    for (final Object o : objects) {
        final RenderingHints.Key key = (RenderingHints.Key) o;
        final Object value = g2d.getRenderingHint(key);
        if (value != null) {
            savedHints.put(key, value);
        }
    }
    return savedHints;
}

From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java

/**
 * Loads the given CLP or SVG file and creates a BufferedImage with the given dimensions. As CLP files contain Vector images,
 * they can be scaled to every size needed. The contents are scaled to the given width and height, <b>not</b> preserving any
 * ratio of the image./*from   ww w.ja  v  a 2 s . c  o m*/
 *
 * @param clpFile CLP or SVG file.
 * @param widthPixel The width, in pixels, the resulting image shall have.
 * @param heightPixel The height, in pixels, the resulting image shall have.
 *
 * @return An image displaying the contents of the loaded CLP file.
 *
 * @throws IOException If any I/O related problem occurs reading the file.
 */
public static BufferedImage loadClpFile(File clpFile, int widthPixel, int heightPixel) throws IOException {
    FileInputStream fis = new FileInputStream(clpFile);
    ClpInputStream cis = null;
    InputStream in = clpFile.getName().toLowerCase().endsWith(".clp") ? (cis = new ClpInputStream(fis)) : fis;

    UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
    BridgeContext bridgeContext = new BridgeContext(userAgentAdapter);

    SVGDocument svgDocument;
    GraphicsNode rootSvgNode;
    try {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        svgDocument = (SVGDocument) factory.createDocument(clpFile.toURI().toString(),
                new InputStreamReader(in, "ISO-8859-1"));
        rootSvgNode = getRootNode(svgDocument, bridgeContext);
    } finally {
        IOUtils.closeQuietly(cis);
        IOUtils.closeQuietly(fis);
    }

    float[] vb = ViewBox.parseViewBoxAttribute(svgDocument.getRootElement(),
            svgDocument.getRootElement().getAttribute("viewBox"), bridgeContext);

    AffineTransform usr2dev = ViewBox.getPreserveAspectRatioTransform(vb,
            SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true, widthPixel, heightPixel);

    BufferedImage img = new BufferedImage(widthPixel, heightPixel, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setColor(new Color(0.0f, 0.0f, 0.0f, 0.0f));
    g2d.fillRect(0, 0, widthPixel, heightPixel);
    g2d.transform(usr2dev);

    // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 1
    final Object oldBufferedImage = g2d.getRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE);
    g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference<BufferedImage>(img));
    rootSvgNode.paint(g2d);
    // fixes "Graphics2D from BufferedImage lacks BUFFERED_IMAGE hint" - part 2
    if (oldBufferedImage != null)
        g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, oldBufferedImage);
    else
        g2d.getRenderingHints().remove(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE);

    g2d.dispose();
    return img;
}

From source file:RenderingUtils.java

private static Map installDesktopHints(Graphics2D g2) {
    Map oldRenderingHints = null;

    Map desktopHints = desktopHints(g2);
    if (desktopHints != null && !desktopHints.isEmpty()) {
        oldRenderingHints = new HashMap(desktopHints.size());
        RenderingHints.Key key;
        for (Iterator i = desktopHints.keySet().iterator(); i.hasNext();) {
            key = (RenderingHints.Key) i.next();
            oldRenderingHints.put(key, g2.getRenderingHint(key));
        }/*from  w w  w.  java2  s  .  com*/
        g2.addRenderingHints(desktopHints);
    }

    return oldRenderingHints;
}

From source file:ala.soils2sat.DrawingUtils.java

public static void drawRoundedRect(Graphics g, int left, int top, int right, int bottom) {
    Graphics2D g2d = (Graphics2D) g;
    g.drawLine(left + cornerSize, top, right - cornerSize, top);
    g.drawLine(left + cornerSize, bottom, right - cornerSize, bottom);
    g.drawLine(left, top + cornerSize, left, bottom - cornerSize);
    g.drawLine(right, top + cornerSize, right, bottom - cornerSize);
    final Object previousAntiAliasingHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Stroke previousStroke = g2d.getStroke();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    try {/*  w  ww .j a v  a 2 s. co  m*/
        g.drawLine(left, top + cornerSize, left + cornerSize, top);
        g.drawLine(left, bottom - cornerSize, left + cornerSize, bottom);
        g.drawLine(right, top + cornerSize, right - cornerSize, top);
        g.drawLine(right, bottom - cornerSize, right - cornerSize, bottom);
    } finally {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, previousAntiAliasingHint);
        g2d.setStroke(previousStroke);
    }
}

From source file:com.igormaznitsa.jhexed.renders.svg.SVGImage.java

public void render(final Graphics2D g) throws IOException {
    final Object antialiasText = g.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
    final Object antialiasDraw = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Object antialiasAlpha = g.getRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION);
    try {//from ww w .  ja v a2  s.c  o m
        processAntialias(this.quality, g);
        this.svgGraphicsNode.primitivePaint(g);
    } finally {
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                antialiasText == null ? RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT : antialiasText);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                antialiasDraw == null ? RenderingHints.VALUE_ANTIALIAS_DEFAULT : antialiasDraw);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                antialiasAlpha == null ? RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT : antialiasAlpha);
    }
}

From source file:org.tsho.dmc2.core.chart.AbstractDmcPlot.java

public void drawPlot(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info) {
    Object originalAntialiasHint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    if (plotAntialias) {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } else {// w w w . ja  va  2s. c om
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }

    Composite originalComposite = g2.getComposite();
    if (alpha == true) {
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha()));
    }

    /* if automatic bounds... */
    if (!isNoData()) {
        if (this instanceof DmcRenderablePlot) {
            DmcPlotRenderer renderer;
            renderer = ((DmcRenderablePlot) this).getPlotRenderer();
            if (renderer != null) {
                renderer.initialize();
            }
        }
    }

    g2.setStroke(stroke);
    g2.setPaint(paint);
    render(g2, dataArea, info); /** This is where the computations method is called.*/
    g2.setComposite(originalComposite);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, originalAntialiasHint);
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

@Override
protected void paintLayer(Graphics2D g2, JXLayer<? extends V> layer) {
    super.paintLayer(g2, layer);

    if (this.mainTraceInfo.getPoint() == null) {
        return;// w  w  w . jav  a 2s .co  m
    }

    final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Color oldColor = g2.getColor();

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(COLOR_BLUE);
    final int BALL_RADIUS = 8;
    g2.fillOval((int) (this.mainTraceInfo.getPoint().getX() - (BALL_RADIUS >> 1) + 0.5),
            (int) (this.mainTraceInfo.getPoint().getY() - (BALL_RADIUS >> 1) + 0.5), BALL_RADIUS, BALL_RADIUS);

    for (TraceInfo indicatorTraceInfo : this.indicatorTraceInfos) {
        final Point2D point = indicatorTraceInfo.getPoint();
        if (null == point) {
            continue;
        }
        g2.fillOval((int) (point.getX() - (BALL_RADIUS >> 1) + 0.5),
                (int) (point.getY() - (BALL_RADIUS >> 1) + 0.5), BALL_RADIUS, BALL_RADIUS);
    }

    g2.setColor(oldColor);

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    this.drawInformationBox(g2, layer);
}