Java Example Program / Sample Source Code
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
public class ShapeFromOutline {
public static void main(String[] args) {
Frame frame = new Frame("ShapeFromOutline");
frame.setSize(400, 400);
frame.add(new CanvasToDisplay());
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class CanvasToDisplay extends Component {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Font plainFont = new Font("Times New Roman", Font.BOLD, 24);
Shape shape = getTextShape(g2D, "JavaTips.net", plainFont);
g2D.draw(shape);
}
Shape getTextShape(Graphics2D g2D, String str, Font font) {
FontRenderContext frc = g2D.getFontRenderContext();
TextLayout textLayout = new TextLayout(str, font, frc);
return textLayout.getOutline(null);
}
} |
|
|