Skip to Main Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced content levelIntermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

Drawing Text

Problem

You need to draw text in a component.

Solution

Simply call the drawString ( ) method in the Graphics class:

// graphics/DrawStringDemo.java 
import java.awt.*; 
 
public class DrawStringDemo extends Component { 
    int textX = 10, textY = 20; 
    public void paint(Graphics g) { 
        g.drawString("Hello Java", textX, textY); 
    } 
    public Dimension getPreferredSize(  ) { 
        return new Dimension(100, 100); 
    } 
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham
Distributed Computing in Java 9

Distributed Computing in Java 9

Raja Malleswara Rao Malleswara Rao Pattamsetti

Publisher Resources

ISBN: 0596001703Supplemental ContentCatalog PageErrata