Example usage for java.awt RenderingHints VALUE_TEXT_ANTIALIAS_OFF

List of usage examples for java.awt RenderingHints VALUE_TEXT_ANTIALIAS_OFF

Introduction

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

Prototype

Object VALUE_TEXT_ANTIALIAS_OFF

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

Click Source Link

Document

Text antialiasing hint value -- text rendering is done without any form of antialiasing.

Usage

From source file:TextQualityDemo.java

public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF));
    frame.pack();/*from   w w w . j  av  a  2 s .  c o m*/
    frame.setVisible(true);
}

From source file:Main.java

/**
 * //from   ww  w .  ja v a  2s.c o  m
 * @param g
 * @param antiAlias
 * @param textAntiAlias
 */
public static void setAntiAlias(Graphics2D g, boolean antiAlias, boolean textAntiAlias) {
    g.setRenderingHint(RenderingHints.KEY_RENDERING,
            (antiAlias) ? RenderingHints.VALUE_RENDER_QUALITY : RenderingHints.VALUE_RENDER_SPEED);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            (antiAlias) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            (textAntiAlias) ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}

From source file:GuiUtil.java

public static final void setTextAntiAliasing(Graphics g, boolean yesno) {
    Object obj = yesno ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;

    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, obj);
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text/*  w w  w . java 2  s  .  c  o m*/
 * @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:electroStaticUI.Save.java

/**
  * Save chart as SVG file.// w w  w  . j a  v a2s . com
  * Required libs: Apache Batik (batik-svggen.jar, batik-dom.jar, dom.jar).
  *
  * @param chart JFreeChart to save.
  * @param fileName Name of file to save chart in.
  * @param width Width of chart graphic.
  * @param height Height of chart graphic.
  * @return Final file name used.
  * @throws IOException if failed.
  * 
  * To Do: Add a save/open method for saving a file native to this application. It should save the users current charge distribution 
  * to be re-loaded at a later time.
  */

static public final String saveChartToSVG(final JFreeChart chart, String fileName, final int width,
        final int height) throws IOException {
    String result = null;

    if (chart != null) {
        if (fileName == null) {
            final String chartTitle = chart.getTitle().getText();
            if (chartTitle != null) {
                fileName = chartTitle;
            } else {
                fileName = "chart";
            }
        }
        result = fileName + ".svg";

        final DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        final Document document = domImpl.createDocument(null, "svg", null);
        final SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

        //            svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true); //embed fonts

        //set anti-aliasing bug fix for SVG generator:
        //setting rendering hints of svgGenerator doesn't help as it seems to use the rendering hints from the chart
        final boolean antiAlias = chart.getAntiAlias();
        final RenderingHints rh = chart.getRenderingHints();

        if (antiAlias) {
            rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); //fix
        } else {
            rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //fix
        }

        //            svgGenerator.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //            svgGenerator.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        //            svgGenerator.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        //            svgGenerator.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        //            svgGenerator.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        //            svgGenerator.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

        chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), new ChartRenderingInfo());

        //undo anti-aliasing bugfix settings for chart to use correct settings:
        if (antiAlias) {
            rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        } else {
            rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        }

        final boolean useCSS = true;
        Writer out = null;
        try {
            out = new OutputStreamWriter(
                    new BufferedOutputStream(new FileOutputStream(new File(result), false)), "iso-8859-1"); //UTF-8
            svgGenerator.stream(out, useCSS);
        } finally {
            svgGenerator.dispose();
            IOUtils.closeQuietly(out);
        }
    } //else: input unavailable

    return result;
}

From source file:RenderingUtils.java

private static Map desktopHints(Graphics2D g2) {
    if (isPrinting(g2)) {
        return null;
    }/*from www  .j  a  va  2s.  c  o  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:com.igormaznitsa.jhexed.renders.svg.SVGImage.java

private static void processAntialias(final boolean flag, final Graphics2D g) {
    if (flag) {/*  www .j  a va 2  s  .co m*/
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    } else {
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
    }
}

From source file:com.github.lucapino.sheetmaker.renderer.JavaTemplateRenderer.java

private void processTextElement(Graphics2D g2, Element textElement) {

    int x = Integer.valueOf(textElement.getAttributeValue("X"));
    int y = Integer.valueOf(textElement.getAttributeValue("Y"));
    int width = Integer.valueOf(textElement.getAttributeValue("Width"));
    int height = Integer.valueOf(textElement.getAttributeValue("Height"));
    String alignment = textElement.getAttributeValue("TextAlignment");
    boolean multiline = Boolean.valueOf(textElement.getAttributeValue("Multiline").toLowerCase());
    boolean antiAlias = textElement.getAttributeValue("TextQuality").equalsIgnoreCase("antialias");

    Font font = parseFont(textElement.getAttributeValue("Font"));

    logger.info("Using font " + font);
    // now get the textim4java performance
    String text = textElement.getAttributeValue("Text");
    // if text matches pattern of %VARIABLE%{MODIFIER}
    logger.info("parsing token {}", text);
    Matcher matcher = pattern.matcher(text);
    int start = 0;
    while (matcher.find(start)) {
        // apply modification
        text = text.replace(matcher.group(), applyModifier(matcher.group()));
        start = matcher.end();//from  w  w  w  .  java  2s.  c  o m
    }
    BufferedImage tmpImage;
    if (width > 0 && height > 0) {
        // create a transparent tmpImage
        tmpImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    } else {
        FontMetrics fm = g2.getFontMetrics(font);
        Rectangle outlineBounds = fm.getStringBounds(text, g2).getBounds();
        //         we need to create a transparent image to paint
        tmpImage = new BufferedImage(outlineBounds.width, outlineBounds.height, BufferedImage.TYPE_INT_ARGB);
    }
    Graphics2D g2d = tmpImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    //        }
    g2d.setFont(font);
    Color textColor = new Color(Integer.valueOf(textElement.getAttributeValue("ForeColor")));
    g2d.setColor(textColor);
    Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f);
    g2d.setComposite(comp);
    drawString(g2d, text, new Rectangle(0, 0, width, height), Align.valueOf(alignment), 0, multiline);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    tmpImage = processActions(textElement, tmpImage);

    ////        Graphics2D g2d = tmpImage.createGraphics();
    //        // set current font
    //        g2.setFont(font);
    ////        g2d.setComposite(AlphaComposite.Clear);
    ////        g2d.fillRect(0, 0, width, height);
    ////        g2d.setComposite(AlphaComposite.Src);
    //        // TODO: we have to parse it
    //        int strokeWidth = Integer.valueOf(textElement.getAttributeValue("StrokeWidth"));
    //        // the color of the outline
    //        if (strokeWidth > 0) {
    ////            Color strokeColor = new Color(Integer.valueOf(textElement.getAttributeValue("StrokeColor")));
    ////            AffineTransform affineTransform;
    ////            affineTransform = g2d.getTransform();
    ////            affineTransform.translate(width / 2 - (outlineBounds.width / 2), height / 2
    ////                    + (outlineBounds.height / 2));
    ////            g2d.transform(affineTransform);
    ////            // backup stroke width and color
    ////            Stroke originalStroke = g2d.getStroke();
    ////            Color originalColor = g2d.getColor();
    ////            g2d.setColor(strokeColor);
    ////            g2d.setStroke(new BasicStroke(strokeWidth));
    ////            g2d.draw(shape);
    ////            g2d.setClip(shape);
    ////            // restore stroke width and color
    ////            g2d.setStroke(originalStroke);
    ////            g2d.setColor(originalColor);
    //        }
    ////        // get the text color
    //        Color textColor = new Color(Integer.valueOf(textElement.getAttributeValue("ForeColor")));
    //        g2.setColor(textColor);
    ////        g2d.setBackground(Color.BLACK);
    ////        g2d.setStroke(new BasicStroke(2));
    ////        g2d.setColor(Color.WHITE);
    //        // draw the text
    //
    //        drawString(g2, text, new Rectangle(x, y, width, height), Align.valueOf(alignment), 0, multiline);
    //        g2.drawString(text, x, y);
    //        Rectangle rect = new Rectangle(x, y, width, height); // defines the desired size and position
    //        FontMetrics fm = g2.getFontMetrics();
    //        FontRenderContext frc = g2.getFontRenderContext();
    //        TextLayout tl = new TextLayout(text, g2.getFont(), frc);
    //        AffineTransform transform = new AffineTransform();
    //        transform.setToTranslation(rect.getX(), rect.getY());
    //        if (Boolean.valueOf(textElement.getAttributeValue("AutoSize").toLowerCase())) {
    //            double scaleY
    //                    = rect.getHeight() / (double) (tl.getOutline(null).getBounds().getMaxY()
    //                    - tl.getOutline(null).getBounds().getMinY());
    //            transform.scale(rect.getWidth() / (double) fm.stringWidth(text), scaleY);
    //        }
    //        Shape shape = tl.getOutline(transform);
    //        g2.setClip(shape);
    //        g2.fill(shape.getBounds());
    //        if (antiAlias) {
    // we need to restore antialias to none
    //            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    //        }
    //        g2.drawString(text, x, y);
    // alway resize
    //        BicubicScaleFilter scaleFilter = new BicubicScaleFilter(width, height);
    //        tmpImage = scaleFilter.filter(tmpImage, null);
    // draw the image to the source
    g2.drawImage(tmpImage, x, y, width, height, null);
    try {
        ScreenImage.writeImage(tmpImage, "/tmp/images/" + textElement.getAttributeValue("Name") + ".png");
    } catch (IOException ex) {

    }

}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public BufferedImage getImage() {
    // Create an image that supports transparent pixels
    Dimension d = getPreferredSize();
    BufferedImage img = GraphicUtils.createCompatibleImage(d.width, d.height, false);

    // prepare graphics context
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g2d.setBackground(new Color(255, 255, 255, 0));

    // paint/*from ww w  .j  a v a2s  . c om*/
    is_printing = true;
    this.paint(g2d);
    is_printing = false;

    return img;
}