Tue Nov 11, 2008 6:35 pm
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
public class FontOutline extends JApplet {
public void initialize() {
FontPaint font1 = new FontPaint();
getContentPane().add(font1, BorderLayout.CENTER);
}
public static void main(String[] args) {
FontPaint font2 = new FontPaint();
JFrame frame = new JFrame("Outlining Font");
frame.getContentPane().add(font2, BorderLayout.CENTER);
frame.setSize(new Dimension(350, 200));
frame.setVisible(true);
}
}
class FontPaint extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);
int w = getSize().width;
int h = getSize().height;
Graphics2D g2d = (Graphics2D) g;
FontRenderContext fontRendContext = g2d.getFontRenderContext();
Font font = new Font("Monotype Corsiva", 2, 50);
String st = new String("Hello World.");
TextLayout text = new TextLayout(st, font, fontRendContext);
AffineTransform affineTransform = new AffineTransform();
Shape shape = text.getOutline(null);
Rectangle rect = shape.getBounds();
affineTransform = g2d.getTransform();
affineTransform.translate(w / 2 - (rect.width / 2), h / 2
+ (rect.height / 2));
g2d.transform(affineTransform);
g2d.setColor(Color.red);
g2d.draw(shape);
g2d.setClip(shape);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.