Java Swing Tutorial - Java Graphics2D.drawString(String str, int x, int y)








Syntax

Graphics2D.drawString(String str, int x, int y) has the following syntax.

public abstract void drawString(String str,  int x,  int y)

Example

In the following code shows how to use Graphics2D.drawString(String str, int x, int y) method.

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
// ww w .j a v a 2s.  c  om
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);

    g2.drawString("java2s.com", 40, 120); 
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20,20, 500,500);
    frame.setVisible(true);
  }
}