Drawing a String using Java 2D Graphics API


This example shows how to draw a String on a JFrame using the Java 2D Graphics API.
We cast the graphics object that is sent to the paint method as an argument to an Graphics 2D object, and then call the drawString() method on it.
The first parameter is the string to be drawn, the second is the X coordinate, and the third parameter is the Y coordinate.
Both coordinates has the top left corner as origin, so according to the code below the line will be drawn 100 pixels down and 20 pixels in from the left margin.


import java.awt.Graphics;
import java.awt.Graphics2D;
/**
 * Displays a JFrame and draws text on it using the Java 2D Graphics API
 * @author www.javadb.com
 */

public class Java2DFrame extends javax.swing.JFrame {
    
    /**
     * Creates a new instance of Java2DFrame
     */

    public Java2DFrame() {
        initComponents();
    }
    
    /**
     * This is the method where the String is drawn.
     *
     * @param g The graphics object
     */

    public void paint(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        graphics.drawString("Using Java 2D API to draw a String", 20, 100);
    }
    
}

This is the result of drawing a string with the Java 2D Graphics API and the code above:


Drawing a string with the Java 2D API


In the example above only parts of the class is presented to make it easier to view.
Here is the complete code of the frame class that uses the Java 2D API to draw a string:


import java.awt.Graphics;
import java.awt.Graphics2D;
/**
 * Displays a JFrame and draws text on it using the Java 2D Graphics API
 * @author www.javadb.com
 */

public class Java2DFrame extends javax.swing.JFrame {
    
    /**
     * Creates a new instance of Java2DFrame
     */

    public Java2DFrame() {
        initComponents();
    }
    
    /**
     * This is the method where the String is drawn.
     *
     * @param g The graphics object
     */

    public void paint(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        graphics.drawString("Using Java 2D API to draw a String", 20, 100);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>
    
    /**
     * Starts the program
     * @param args the command line arguments
     */

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java2DFrame().setVisible(true);
            }
        });
    }
    
}