Example usage for java.awt RenderingHints VALUE_FRACTIONALMETRICS_OFF

List of usage examples for java.awt RenderingHints VALUE_FRACTIONALMETRICS_OFF

Introduction

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

Prototype

Object VALUE_FRACTIONALMETRICS_OFF

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

Click Source Link

Document

Font fractional metrics hint value -- character glyphs are positioned with advance widths rounded to pixel boundaries.

Usage

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text/*from w  w  w . ja v  a2  s .  c  om*/
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:piramide.interaction.reasoner.FuzzyReasonerWizardFacade.java

private BufferedImage createErrorMessagesImage(String text) {
    final Font font = TextTitle.DEFAULT_FONT;
    final Font bigBold = new Font(font.getName(), Font.BOLD, 24);
    final FontRenderContext frc = new FontRenderContext(null, true, false);
    final TextLayout layout = new TextLayout(text, bigBold, frc);
    final Rectangle2D bounds = layout.getBounds();
    final int w = (int) Math.ceil(bounds.getWidth());
    final int h = (int) Math.ceil(bounds.getHeight());
    final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);//from   w  w w. j  a  v a  2  s .c o m
    g.fillRect(0, 0, w, h);
    g.setColor(Color.RED);
    g.setFont(bigBold);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();
    return image;
}

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 va  2  s . com
 * 
 * @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:org.squidy.designer.shape.VisualShape.java

@Override
protected final void paint(PPaintContext paintContext) {
    super.paint(paintContext);

    Graphics2D g = paintContext.getGraphics();

    // Set default font.
    g.setFont(internalFont);/*from   w  w w .j  a v  a  2 s .c  o  m*/

    //      if (!renderingHintsSet) {
    if (isRenderPrimitive()) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
        g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    } else {
        // Use anti aliasing -> May slow down performance.
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
    //         renderingHintsSet = true;
    //      }

    // Paint the shapes visual representation.
    paintShape(paintContext);

    // Allows visual debugging if enabled.
    if (DebugConstants.ENABLED) {
        paintDebug(paintContext);
    }
}