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



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net