home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Day 10
Fonts and Text

Drawing text to a Java window is different from putting text into a Java control. When you draw text to a Java window, you’re actually using graphics commands instead of just inserting text into a control. Today you’ll learn how to draw to a Java window, how to alter the fonts so that you can draw with different font styles and colors, and even how to change font styles and colors in controls. In this chapter, we’ll cover the following:

  The Graphics class
  Graphics screen coordinates
  The Font class
  Drawing text
  The FontMetrics class
  The Color class
  Finding the available system fonts

After you’re familiar with the ins and outs of Java text and fonts, you can create applets and applications that are far more useful. That’s because drawing with text makes your presentation richer and allows you a greater degree of flexibility in your display.

The Graphics Class

Before we talk about drawing text to a Java window, we need to talk about the Graphics class. The reason we have to start off by talking about the Graphics class is that drawing text to a Java window is a graphics operation. The Graphics class is what you use to perform all graphics operations. The Graphics class is a descriptor of the window to which you’ll be drawing. It’s similar to the device context in Visual C++; for any of you who have developed with Visual C++, you know that you must have a device context with which to draw. It’s the same in Java; you must have a Graphics class.

With Java’s graphics capabilities, you can draw lines, shapes, characters, and images to the screen inside of your applet or application. Most of the graphics operations in Java are methods defined in the Graphics class. You don’t have to create an instance of the Graphics class in order to draw something in your applet. When you override your applet’s paint method, or your application’s onPaint method, you’re given a Graphics class. By drawing to that class, you draw to your program’s window, and the results appear onscreen.

Methods to Override for Painting

You can override two different methods if you want to paint every time your window is redrawn. For an application, you will want to override the onPaint() method. For an applet, you will want to override the paint() method. After these methods are overridden, every time the window is redrawn in response to a window event or a mouse movement, or something similar, either the onPaint() method for an application or the paint()method for an applet will be called. This will give you the opportunity to draw in your window.

This is what the onPaint() method in an application looks like:

protected void onPaint(PaintEvent pe)
{
    pe.graphics.drawString( "Hello Visual J++ World.", 10, 10 );
}

Notice that the PaintEvent class, named pe in this case, has a member class called graphics. This member class is the Graphics class we mentioned in the preceding section. One of its member methods is drawString(). That’s the method we use to draw text to the Graphics class.

Next, you need to learn how to draw to an applet window. The method you will override for an applet is simply named paint(). It gets, as an argument, a Graphics class.

The following example shows you what a paint() method looks like for an applet. It also uses the drawString() member method to display “Hello Visual J++ World!” in the applet window.

protected void onPaint(PaintEvent pe)
{
g.drawString( "Hello Visual J++ World!", 100, 100 );
}

The repaint() and update() Methods

The repaint() and update() methods can be overridden in Java applets. They are members of the Component class. The Java system, or your program, might call the repaint() method to request that the window be refreshed. Typically, you call it if you have new things to display.


Caution:  Never override this method! It calls the update() method to clear and repaint the screen.

The update() method belongs to the Component class. The default implementation clears the window area and then calls the paint() method. You should not directly call the update() method. The update() method is called by the repaint() method. You might want to override the update() method to perform additional functions. I often use it when I don’t want the screen cleared before the paint() method is called.

If you clear the window for every screen refresh, you get a lot of screen flicker. Two things can remedy this problem: first, override the update() method, and second, don’t clear the background of the screen each time a window refresh is called for. The window refresh will be smoother and your applet might look better.

Try It Yourself!

Before we get too much further, you need to create an application or an applet in which you draw a text string. Run Visual J++. If you’re writing an application, open the form source code; if you’re writing an applet, open the applet source code. Listing 10.1 shows how to draw text to an application window. The results are shown in Figure 10.1.

Listing 10.1 Using the onPaint Method in an Application

1   protected void onPaint(PaintEvent pe)
2   {
3       pe.graphics.drawString( "Hello Visual J++ World.", 10, 10 );
4   }


Figure 10.1  This Visual J++ application uses the onPaint() method to display text to the window.

Adding the onPaint() method overrides the superclass onPaint() method. If you’ve forgotten, refer to the discussion of the terms superclass and override in Chapter 5.

There’s another alternative to adding an onPaint() method. With Form1 in the Properties window, click on the Events button in the Properties window. Scroll down until you find the paint event. Double-click on the event and a Form1_paint() method will be added for you. This method gets a PaintEvent method just like the onPaint() method.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.