Example usage for java.awt RenderingHints KEY_TEXT_ANTIALIASING

List of usage examples for java.awt RenderingHints KEY_TEXT_ANTIALIASING

Introduction

In this page you can find the example usage for java.awt RenderingHints KEY_TEXT_ANTIALIASING.

Prototype

Key KEY_TEXT_ANTIALIASING

To view the source code for java.awt RenderingHints KEY_TEXT_ANTIALIASING.

Click Source Link

Document

Text antialiasing hint key.

Usage

From source file:com.rcp.wbw.demo.ChartComposite.java

/**
 * Paints the control./*from www. ja v a 2  s.c  o  m*/
 * 
 * @param e
 *            the paint event.
 */
public void paintControl(PaintEvent e) {
    // first determine the size of the chart rendering area...
    // TODO workout insets for SWT
    Rectangle available = getBounds();
    // skip if chart is null
    if (this.chart == null) {
        this.canvas.drawBackground(e.gc, available.x, available.y, available.width, available.height);
        return;
    }
    SWTGraphics2D sg2 = new SWTGraphics2D(e.gc);

    // work out if scaling is required...
    boolean scale = false;
    int drawWidth = available.width;
    int drawHeight = available.height;
    if (drawWidth == 0.0 || drawHeight == 0.0)
        return;
    this.scaleX = 1.0;
    this.scaleY = 1.0;
    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = (double) drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = (double) drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = (double) drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = (double) drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }
    // are we using the chart buffer?
    if (this.useBuffer) {
        // SwtGraphics2D sg2 = new SwtGraphics2D(e.gc);
        this.chartBuffer = (org.eclipse.swt.graphics.Image) this.canvas.getData("double-buffer-image");
        // do we need to fill the buffer?
        if (this.chartBuffer == null || this.chartBufferWidth != available.width
                || this.chartBufferHeight != available.height) {
            this.chartBufferWidth = available.width;
            this.chartBufferHeight = available.height;
            if (this.chartBuffer != null) {
                this.chartBuffer.dispose();
            }
            this.chartBuffer = new org.eclipse.swt.graphics.Image(getDisplay(), this.chartBufferWidth,
                    this.chartBufferHeight);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            // Performs the actual drawing here ...
            GC gci = new GC(this.chartBuffer);
            // anti-aliasing
            if (this.chart.getAntiAlias()) {
                gci.setAntialias(SWT.ON);
            }
            if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
                gci.setTextAntialias(SWT.ON);
            }
            SWTGraphics2D sg2d = new SWTGraphics2D(gci);
            if (scale) {
                sg2d.scale(this.scaleX, this.scaleY);
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            } else {
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            }
            this.canvas.setData("double-buffer-image", this.chartBuffer);
            sg2d.dispose();
            gci.dispose();
            this.refreshBuffer = false;
        }

        // zap the buffer onto the canvas...
        sg2.drawImage(this.chartBuffer, 0, 0);
    }
    // or redrawing the chart every time...
    else {
        if (this.chart.getAntiAlias()) {
            e.gc.setAntialias(SWT.ON);
        }
        if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
            e.gc.setTextAntialias(SWT.ON);
        }
        this.chart.draw(sg2, new Rectangle2D.Double(0, 0, getBounds().width, getBounds().height), getAnchor(),
                this.info);
    }
    Rectangle area = getScreenDataArea();
    // TODO see if we need to apply some line color and style to the
    // axis traces
    if (this.horizontalAxisTrace && area.x < this.verticalTraceLineX
            && area.x + area.width > this.verticalTraceLineX) {
        e.gc.drawLine(this.verticalTraceLineX, area.y, this.verticalTraceLineX, area.y + area.height);
    }
    if (this.verticalAxisTrace && area.y < this.horizontalTraceLineY
            && area.y + area.height > this.horizontalTraceLineY) {
        e.gc.drawLine(area.x, this.horizontalTraceLineY, area.x + area.width, this.horizontalTraceLineY);
    }
    this.verticalTraceLineX = 0;
    this.horizontalTraceLineY = 0;
    if (this.zoomRectangle != null) {
        e.gc.drawRectangle(this.zoomRectangle);
    }
    sg2.dispose();
}

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:edu.pitt.dbmi.odie.ui.jfreechart.EnhancedChartComposite.java

public void paintControl(PaintEvent e) {
    // first determine the size of the chart rendering area...
    // TODO workout insets for SWT
    Rectangle available = getBounds();
    // Rectangle available = new
    // Rectangle(0,0,maximumDrawWidth,maximumDrawHeight);

    // skip if chart is null
    if (this.chart == null) {
        this.canvas.drawBackground(e.gc, available.x, available.y, available.width, available.height);
        return;//from   w  w w .  j a  va2s .c om
    }
    SWTGraphics2D sg2 = new SWTGraphics2D(e.gc);

    // work out if scaling is required...

    boolean scale = false;
    int drawWidth = available.width;
    int drawHeight = available.height;
    if (drawWidth == 0.0 || drawHeight == 0.0)
        return;
    this.scaleX = 1.0;
    this.scaleY = 1.0;

    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = (double) drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = (double) drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = (double) drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = (double) drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }

    // logger.debug(drawWidth + "," + drawHeight + "," + scaleX + "," +
    // scaleY);
    // logger.debug(minimumDrawWidth +"," + minimumDrawHeight + "," +
    // maximumDrawWidth + "," + maximumDrawHeight);

    // are we using the chart buffer?
    if (this.useBuffer) {
        // SwtGraphics2D sg2 = new SwtGraphics2D(e.gc);
        this.chartBuffer = (org.eclipse.swt.graphics.Image) this.canvas.getData("double-buffer-image");
        // do we need to fill the buffer?
        if (this.chartBuffer == null || this.chartBufferWidth != available.width
                || this.chartBufferHeight != available.height) {
            this.chartBufferWidth = available.width;
            this.chartBufferHeight = available.height;
            if (this.chartBuffer != null) {
                this.chartBuffer.dispose();
            }
            this.chartBuffer = new org.eclipse.swt.graphics.Image(getDisplay(), this.chartBufferWidth,
                    this.chartBufferHeight);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            // Performs the actual drawing here ...
            GC gci = new GC(this.chartBuffer);
            // anti-aliasing
            if (this.chart.getAntiAlias()) {
                gci.setAntialias(SWT.ON);
            }
            if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
                gci.setTextAntialias(SWT.ON);
            }
            SWTGraphics2D sg2d = new SWTGraphics2D(gci);
            if (scale) {
                sg2d.scale(this.scaleX, this.scaleY);
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            } else {
                this.chart.draw(sg2d, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), getAnchor(),
                        this.info);
            }
            this.canvas.setData("double-buffer-image", this.chartBuffer);
            sg2d.dispose();
            gci.dispose();
            this.refreshBuffer = false;
        }

        // zap the buffer onto the canvas...
        sg2.drawImage(this.chartBuffer, 0, 0);
    }
    // or redrawing the chart every time...
    else {
        if (this.chart.getAntiAlias()) {
            e.gc.setAntialias(SWT.ON);
        }
        if (this.chart.getTextAntiAlias() == RenderingHints.KEY_TEXT_ANTIALIASING) {
            e.gc.setTextAntialias(SWT.ON);
        }
        this.chart.draw(sg2, new Rectangle2D.Double(0, 0, getBounds().width, getBounds().height), getAnchor(),
                this.info);
    }
    Rectangle area = getScreenDataArea();
    // TODO see if we need to apply some line color and style to the
    // axis traces
    if (this.horizontalAxisTrace && area.x < this.verticalTraceLineX
            && area.x + area.width > this.verticalTraceLineX) {
        e.gc.drawLine(this.verticalTraceLineX, area.y, this.verticalTraceLineX, area.y + area.height);
    }
    if (this.verticalAxisTrace && area.y < this.horizontalTraceLineY
            && area.y + area.height > this.horizontalTraceLineY) {
        e.gc.drawLine(area.x, this.horizontalTraceLineY, area.x + area.width, this.horizontalTraceLineY);
    }
    this.verticalTraceLineX = 0;
    this.horizontalTraceLineY = 0;

    // Draw the zoom rectangle and selection rectangle
    if (this.zoomRectangle != null) {

        e.gc.drawRectangle(this.zoomRectangle);
    } else if (this.selectionRectangle != null) {
        boundSelectionRectangle();
        Rectangle rect = translateJava2DToScreen(selectionRectangle);
        e.gc.setBackground(selectionBackground);
        e.gc.setAlpha(75);
        e.gc.fillRectangle(rect);
    }
    sg2.dispose();
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Creates rendering hints for Text.//w w  w  .jav  a2 s. c  o  m
 */
public static RenderingHints createTextRenderingHints() {
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    Object value = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
    try {
        Field declaredField = RenderingHints.class.getDeclaredField("VALUE_TEXT_ANTIALIAS_LCD_HRGB");
        value = declaredField.get(null);

    } catch (Exception e) {
        // do nothing
    }
    renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, value);
    return renderingHints;
}

From source file:DefaultGraphics2D.java

/**
 * Get the rendering context of the <code>Font</code> within this
 * <code>Graphics2D</code> context. The {@link FontRenderContext}
 * encapsulates application hints such as anti-aliasing and fractional
 * metrics, as well as target device specific information such as
 * dots-per-inch. This information should be provided by the application when
 * using objects that perform typographical formatting, such as
 * <code>Font</code> and <code>TextLayout</code>. This information should
 * also be provided by applications that perform their own layout and need
 * accurate measurements of various characteristics of glyphs such as advance
 * and line height when various rendering hints have been applied to the text
 * rendering.//from w w  w .  j  a v  a 2s . c om
 * 
 * @return a reference to an instance of FontRenderContext.
 * @see java.awt.font.FontRenderContext
 * @see java.awt.Font#createGlyphVector(FontRenderContext,char[])
 * @see java.awt.font.TextLayout
 * @since JDK1.2
 */
public FontRenderContext getFontRenderContext() {
    //
    // Find if antialiasing should be used.
    //
    Object antialiasingHint = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
    boolean isAntialiased = true;
    if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            && antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT) {

        // If antialias was not turned off, then use the general rendering
        // hint.
        if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) {
            antialiasingHint = hints.get(RenderingHints.KEY_ANTIALIASING);

            // Test general hint
            if (antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON
                    && antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT) {
                // Antialiasing was not requested. However, if it was not turned
                // off explicitly, use it.
                if (antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF)
                    isAntialiased = false;
            }
        } else
            isAntialiased = false;

    }

    //
    // Find out whether fractional metrics should be used.
    //
    boolean useFractionalMetrics = true;
    if (hints.get(RenderingHints.KEY_FRACTIONALMETRICS) == RenderingHints.VALUE_FRACTIONALMETRICS_OFF)
        useFractionalMetrics = false;

    FontRenderContext frc = new FontRenderContext(defaultTransform, isAntialiased, useFractionalMetrics);
    return frc;
}

From source file:com.flexoodb.common.FlexUtils.java

static public void createTextBanner(String text, int fontsize, int width, int height, int x, int y,
        String outputfile) throws Exception {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();//from  w  ww  .j a v  a  2s . c o  m
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Arial", Font.BOLD, fontsize);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(Color.BLUE.darker());
    g.drawString(text, x, y);
    ImageIO.write(img, "png", new File(outputfile));
}

From source file:com.flexoodb.common.FlexUtils.java

static public BufferedImage createCaptcha(String text, Color background, Color fontcolor, int fontsize,
        int width, int height, int x, int y) throws Exception {
    BufferedImage img = new BufferedImage(width + 10, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();/*from w w  w.j av a 2s  .com*/
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(background);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Monospaced", Font.BOLD + Font.ITALIC, fontsize);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(fontcolor);

    char[] c = text.toCharArray();

    java.util.Random random = new java.util.Random();

    for (int i = 0; i < c.length; i++) {
        int j = random.nextInt(9);
        if (j > 5) {
            j = j - 5;
        } else {
            j = -1 * (j - 2);
        }

        char[] c1 = new char[1];
        c1[0] = c[i];
        g.drawChars(c1, 0, 1, x + (i * 16), y + j);
    }

    for (int i = -10; i < 20; i++) {
        int j = random.nextInt(8);
        g.drawOval((i + j) - 20, ((i * 8) - (j > 3 ? 4 : -4)), width + 20, height);
    }
    //ImageIO.write(img,"png",new File(outputfile));
    return img;

}

From source file:org.forester.archaeopteryx.TreePanel.java

final void setTextAntialias() {
    if ((_phylogeny != null) && !_phylogeny.isEmpty()) {
        if (_phylogeny.getNumberOfExternalNodes() <= LIMIT_FOR_HQ_RENDERING) {
            _rendering_hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        } else {//from   ww w. ja v a 2s .c om
            _rendering_hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
        }
    }
    if (getMainPanel().getOptions().isAntialiasScreen()) {
        if (getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR) {
            _rendering_hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        } else {
            _rendering_hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        }
        try {
            _rendering_hints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        } catch (final Throwable e) {
            _rendering_hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        }
    } else {
        _rendering_hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        _rendering_hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }
}

From source file:org.apache.batchee.tools.maven.DiagramMojo.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) throws MojoExecutionException {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);/*  w w  w  .ja va 2 s. c  o  m*/
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        getLog().info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        getLog().info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new MojoExecutionException("can't save picture " + name + "." + format);
        }
        getLog().info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new MojoExecutionException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}

From source file:org.apache.batchee.tools.maven.doc.DiagramGenerator.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);//from  w ww  . j  a v a 2s  .com
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new IllegalStateException("can't save picture " + name + "." + format);
        }
        info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new IllegalStateException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}