FontRenderContextRenderingHints.java Source code

Java tutorial

Introduction

Here is the source code for FontRenderContextRenderingHints.java

Source

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontRenderContextRenderingHints extends JFrame {
    public FontRenderContextRenderingHints() {
        getContentPane().add(new DrawingCanvas());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String arg[]) {
        new FontRenderContextRenderingHints();
    }
}

class DrawingCanvas extends JPanel {
    Font font = new Font("Dialog", Font.BOLD, 40);
    FontMetrics fontMetrics;

    DrawingCanvas() {
        setSize(300, 300);
        setBackground(Color.white);
        fontMetrics = getFontMetrics(font);
    }

    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        int w = getSize().width;
        int h = getSize().height;

        RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2D.setRenderingHints(qualityHints);

        AffineTransform at = new AffineTransform();
        at.setToTranslation(-300, -400);
        at.shear(-0.5, 0.0);
        FontRenderContext frc = new FontRenderContext(at, false, false);
        TextLayout tl = new TextLayout("World!", font, frc);
        Shape outline = tl.getOutline(null);
        g2D.setColor(Color.blue);

        BasicStroke wideStroke = new BasicStroke(2.0f);
        g2D.setStroke(wideStroke);
        g2D.draw(outline);

    }
}