Recipe 13.6 Drawing Text with 2D


Problem

You want fancier drawing abilities.

Solution

Use a Graphics2D object.

Discussion

The 2D graphics added in JDK 1.2 could be the subject of an entire book, and in fact, it is. Java 2D Graphics by Jonathan Knudsen (O'Reilly) covers every imaginable aspect of this comprehensive graphics package. Here I'll just show one example: drawing text with a textured background.

The Graphics2D class is a direct subclass of the original Java Graphics object. In fact, your paint( ) method is always called with an instance of Graphics2D. So begin your paint method by casting appropriately:

public void paint(Graphics g) {     Graphics2D g2 = (Graphics2D) g;

You can then use any Graphics2D methods or any regular Graphics methods, getting to them with the object reference g2. One of the additional methods in Graphics2D is setPaint( ), which can take the place of setColor( ) to draw with a solid color. However, it can also be called with several other types, and in this case we pass in an object called a TexturePaint, which refers to a pattern. Our pattern is a simple set of diagonal lines, but any pattern or even a bitmap from a file (see Recipe 13.8) can be used. Figure 13-4 shows the resulting screen (it looks even better in color); the program itself is shown in Example 13-2 .

Figure 13-4. TexturedText in action
figs/jcb2_1304.gif


Example 13-2. TexturedText.java
import java.awt.*;  import java.awt.event.*;  import java.awt.image.*;    /** Text with a Texture   */  public class TexturedText extends Component {      /** The image we draw in the texture */      protected BufferedImage bim;       /** The texture for painting. */      TexturePaint tp;      /** The string to draw. */      String mesg = "Stripey";      /** The font */      Font myFont = new Font("Lucida Regular", Font.BOLD, 72);        /** "main program" method - construct and show */      public static void main(String av[]) {          // create a TexturedText object, tell it to show up          final Frame f = new Frame("TexturedText");          TexturedText comp = new TexturedText( );          f.add(comp);          f.addWindowListener(new WindowAdapter( ) {              public void windowClosing(WindowEvent e) {                  f.setVisible(false);                  f.dispose( );                  System.exit(0);              }          });          f.pack( );          f.setLocation(200, 200);          f.setVisible(true);      }        protected static Color[] colors = {          Color.green, Color.red, Color.blue, Color.yellow,      };        /** Construct the object */      public TexturedText( ) {          super( );          setBackground(Color.white);          int width = 8, height = 8;          bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);          Graphics2D g2 = bim.createGraphics( );          for (int i=0; i<width; i++) {              g2.setPaint(colors[(i/2)%colors.length]);              g2.drawLine(0, i, i, 0);              g2.drawLine(width-i, height, width, height-i);          }          Rectangle r = new Rectangle(0, 0, bim.getWidth( ), bim.getHeight( ));          tp = new TexturePaint(bim, r);      }        public void paint(Graphics g) {          Graphics2D g2 = (Graphics2D)g;          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,              RenderingHints.VALUE_ANTIALIAS_ON);          g2.setPaint(tp);          g2.setFont(myFont);          g2.drawString(mesg, 20, 100);      }        public Dimension getMinimumSize( ) {           return new Dimension(250, 100);      }        public Dimension getPreferredSize( ) {           return new Dimension(320, 150);      }  }

See Also

I have not discussed how to scale, rotate, or otherwise transmogrify an image using the AffineTransform class in Java 2D graphics, as such topics are beyond the scope of this book. Consult the previously mentioned Java 2D Graphics.



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

Similar book on Amazon

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