Java examples for 2D Graphics:Text
Display strings in different fonts and colors.
import javax.swing.JFrame; import java.awt.Font; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; class FontJPanel extends JPanel { // display Strings in different fonts and colors @Override//from w w w . j a v a 2 s .co m public void paintComponent(Graphics g) { super.paintComponent(g); // set font to Serif (Times), bold, 12pt and draw a string g.setFont(new Font("Serif", Font.BOLD, 12)); g.drawString("Serif 12 point bold.", 20, 30); // set font to Monospaced (Courier), italic, 24pt and draw a string g.setFont(new Font("Monospaced", Font.ITALIC, 24)); g.drawString("Monospaced 24 point italic.", 20, 50); // set font to SansSerif (Helvetica), plain, 14pt and draw a string g.setFont(new Font("SansSerif", Font.PLAIN, 14)); g.drawString("SansSerif 14 point plain.", 20, 70); // set font to Serif (Times), bold/italic, 18pt and draw a string g.setColor(Color.RED); g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18)); g.drawString(g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 90); } } public class Main { // execute application public static void main(String[] args) { // create frame for FontJPanel JFrame frame = new JFrame("Using fonts"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FontJPanel fontJPanel = new FontJPanel(); frame.add(fontJPanel); frame.setSize(420, 150); frame.setVisible(true); } }