Home » Desktop Java » awt » Draw text example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Draw text example

With this example we are going to see how to draw text in a Java Desktop Application. This is very useful when you have lots of graphics components in your application and you want to display some text in it.
 

In order to draw text in your Java Desktop Application you should:

  • Create a new Frame.
  • Add to the frame a new CustomPaintComponent().
  • Create a new class that extends Component and override the paint method.
  • Use Graphics2D.drawString to draw a string in the screen.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class DrawText {

  public static void main(String[] args) {

// Create a frame

Frame frame = new Frame();

// Add a component with a custom paint method

frame.add(new CustomPaintComponent());

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

  }

  /**
    * To draw on the screen, it is first necessary to subclass a Component and
    * override its paint() method. The paint() method is automatically called
    * by the windowing system whenever component's area needs to be repainted.
    */
  static class CustomPaintComponent extends Component {

public void paint(Graphics g) {

	// Retrieve the graphics context; this object is used to paint shapes

  Graphics2D g2d = (Graphics2D) g;

/**
  * The coordinate system of a graphics context is such that the
  * origin is at the northwest corner and x-axis increases toward the
  * right while the y-axis increases toward the bottom.
  */

  int x = 0;

  int y = 10;

  // Set the desired font if different from default font

  Font font = new Font("Serif", Font.PLAIN, 12);

  g2d.setFont(font);

  // Draw a string such that its base line is at x, y

  g2d.drawString("This is a test string", x, y);

  FontMetrics fontMetrics = g2d.getFontMetrics();

  // Draw a string such that the top-left corner is at x, y

  g2d.drawString("This is another test string", x, y+fontMetrics.getAscent());

  // Draw string rotated clockwise 90 degrees

  AffineTransform affineTransform = new AffineTransform();

  affineTransform.setToRotation(Math.PI/2.0);

  g2d.setTransform(affineTransform);

  g2d.drawString("This is a vertical test string", x, y+2*fontMetrics.getAscent());

  // Draw string rotated counter-clockwise 90 degrees

  affineTransform = new AffineTransform();

  affineTransform.setToRotation(-Math.PI/2.0);

  g2d.setTransform(affineTransform);

  g2d.drawString("This is a another vertical test string", x + fontMetrics.getAscent(), y + 2*fontMetrics.getAscent() + fontMetrics.stringWidth("This is a another vertical test string"));

}

  }

}

 
This was an example on how to draw text in a Java Desktop Application.

(+1 rating, 1 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of