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);
            }
        });
    }
    
}


Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: