I want to convert some arbitrary text to a Shape (java.awt.Shape
) and then stroke/fill the Shape to draw it. How can I do this?
Asked
Active
Viewed 1.4k times
11

Jason S
- 184,598
- 164
- 608
- 970
-
Not sure what you mean here. Can you elaborate? Do you want the text to become the shapes of the characters in the text? – Holograham Mar 17 '10 at 22:08
3 Answers
14
Hm I did not know the answer to this but after a bit tweaking and poking around in with Eclipse content assist i found this which seems to be what you need:
EDIT: i changed to code to change the way the string is displayed which is the reason you asked what you asked :) Try it. It renders the string with red color and a dashed outline
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel{
private Shape s;
public Test() {
Font f = getFont().deriveFont(Font.BOLD, 70);
GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), "Hello");
s = v.getOutline();
setPreferredSize(new Dimension(300,300));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(100, 150);
g2.rotate(0.4);
g2.setPaint(Color.red);
g2.fill(s);
g2.setPaint(Color.black);
g2.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, new float[]{1,0.4f,1.5f}, 0));
g2.draw(s);
}
public static void main(String[] args) {
JFrame f = new JFrame("Test");
Component c = new Test();
f.getContentPane().add(c);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
Also note that you can get the individual characters from the string by calling:
getGlyphOutline(glyphIndex)

Savvas Dalkitsis
- 11,476
- 16
- 65
- 104
-
1Great -- I'm accepting this because it's a complete & understandable example. I'm puzzled because it looks like there are two ways to do this. The GlyphVector approach that you used, and the TextLayout approach that @objects answered with. Not sure what the advantages/disadvantages are of each.... in addition to a font and a text string, GlyphVector() seems to just take a FontRenderContext, but TextLayout takes a FontRenderContext and an AffineTransform. – Jason S Mar 18 '10 at 14:02
-
indeed... It would be interesting to hear from a Sun employee (or from someone experienced in the matter) on the differences. It appears that TextLayout is used for many text glyph related operations such as caret shapes, switching characters... I initially thought that one of the two must be a new addition to the API but from what i can see both classes existed in the 1.4 API (for me and many that's the first version worthy enough to be called Java :P ). – Savvas Dalkitsis Mar 18 '10 at 14:10
5
Use the TextLayout class (see the getOutline() method). Theres an example here

objects
- 8,637
- 4
- 30
- 38
-
this example is a little more compact, much neater than the accepted answer too. http://www.java2s.com/Code/Java/2D-Graphics-GUI/OutlineFontpaint.htm – planty182 Aug 27 '15 at 13:34
0
If I understood you correctly, this is not to address your exact answer, but it's a start...
//Rough pseudo code
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
//Paint with texturing brush
Rectangle2D rect2D = new Rectangle2D.Double(0, 0, width, height);
graphics.setPaint(new TexturePaint(image, rect2D));
graphics.fill(rect2D);
//Draw text
graphics.drawString("my text goes here", xPos, yPos);
In Summary,
- Create a
BufferedImage
object ofwidth
andheight
andImageType
. - Get the image's
Graphics
object. - Paint that graphics like you please (i.e. create a rectangle, circle, text, etc.)
- Write that image to a stream (file, ServletRequest, etc.)

Buhake Sindi
- 87,898
- 29
- 167
- 228
-
-
I suggest that you become more discreet with your question then @Jason S. We are trying to help, but we cannot read what's in your mind. – Buhake Sindi Mar 18 '10 at 23:09
-
?? I appreciate the help, but my question specifically asked how to convert text to a Shape, as in `java.awt.Shape`. – Jason S Mar 19 '10 at 13:54
-
ok, I just saw the edited question now. It wasn't clear earlier. Look also @Holograham's comment below your question. Initially we were unclear about your question. – Buhake Sindi Mar 19 '10 at 14:30