drawString(): specify the position of the text on the window area
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Text extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
Font font = new Font("New Roma", Font.BOLD, 40);
g2d.setFont(font);
g2d.drawString("text", 20, 30);
}
public static void main(String[] args) {
Text text = new Text();
JFrame frame = new JFrame("Sonnet 55");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(text);
frame.setSize(500, 470);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Related examples in the same category