I am writing a game in which I need to draw text onto the screen. As I understand it there are two main ways to do this using Graphics2D - using GlyphVector and using drawString(). Of the two I prefer the previous because it allows me to define text as a Shape object by using GlyphVector's getOutline() method.

However, GlyphVector is giving me very poor quality output. I am not sure what I am doing wrong, but the text is severely jagged and aliased, especially at small font sizes.

Here is an applet to quickly show what I am trying to do.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import javax.swing.JApplet;

public class Test extends JApplet
	public void paint(Graphics gr)
		Graphics2D g = (Graphics2D) gr.create();
		AffineTransform trans = AffineTransform.getTranslateInstance(100, 100);
		Font f = new Font("Serif", Font.PLAIN, 15);
		GlyphVector v = f.createGlyphVector(g.getFontRenderContext(), "Hello");
                Shape shape = v.getOutline();

If there are any other suggestions for drawing text I would love to hear them. However, I do need the final result to be a Shape.

I suggest you use drawString(); (my personal opinon).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.