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


For an application, add an onPaint() method exactly as shown in Listing 10.1. Then compile the program and run it. You should see “Hello Visual J++ World!” in the window, as shown in Figure 10.1. For an applet, add a paint() method as shown in Listing 10.2. Compile the applet and then run it. You should see “Hello Visual J++ World!” in the applet window, as shown in Figure 10.2.

Listing 10.2 Using the paint() Method in an Application

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


Figure 10.2  This Visual J++ applet uses the paint() method to display text to the window.

The Graphics Coordinate System

To draw an object onscreen, you call one of the drawing methods available in the Graphics class. All the drawing methods have arguments representing endpoints, corners, or starting locations of the object whose values are in the program’s coordinate system. For example, a line might start at the point (10,10) and end at the point (20,20).

Java’s coordinate system has the origin (0,0) in the upper-left corner. Positive x values go to the right. Positive y values go down. All pixel values are integers. There are no partial or fractional pixels. Figure 10.3 shows how you might draw a rectangle using a coordinate system.


Figure 10.3  The Java graphics coordinate system.

Java’s coordinate system is different from many painting and layout programs that have their x and y intersection in the lower-left corner. If you’re not used to working with upside-down graphics systems, this system might take a while to get used to. Let’s experiment by going back to the applet or application you just wrote, in which you saw “Hello Visual J++ World!” onscreen. The second and third arguments of the drawString() method are the x and y coordinates. That’s the point at which the string “Hello Visual J++ World!” will be drawn. Go ahead and change those values, recompile, and run the program. See what effect that change has on the appearance of the application or applet. Your program will look different because you’re changing the points at which those strings will be drawn. Change the coordinates several times to experience the full effect.

The Font Class

There’s always a font set in the Graphics class. If there wasn’t, the drawString() method would not result in any text being drawn. Until now, we have drawn with the drawString() method using whatever font was currently selected in the Graphics class.

You can change the font that’s currently selected in the Graphics class by first creating a font and then selecting it into the Graphics class. After a font is selected into a Graphics class, all subsequent text-drawing operations will be done using that font.

The following line creates a font with a TimesRoman font base in a plain style with a point size of 25:

Font font = new Font( "TimesRoman", Font.PLAIN, 25 );

Various fonts are standard for Java applications and applets. You can usually count on Helvetica, TimesRoman, Courier, and Symbol. Later in this chapter, we’ll talk about how you can interrogate the system to find out which fonts are actually available. For now, we will assume that the standard and common fonts can be used.

When you create a new font, the first argument is the font typeface name. Some examples of this were mentioned in the preceding paragraph: Helvetica, TimesRoman, Courier, and Symbol. The second argument when you create a new font is the style. Table 10.1 shows the styles that are available when you create a font in Java.

Table 10.1 Font Styles

Name Description

Font.BOLD The bold style constant
Font.ITALIC The italicized style constant
Font.PLAIN The plain style constant

You can combine font styles. For instance, your style can be the italic style combined with the bold style, as this example shows:

Font font = new Font( "Courier", Font.BOLD + Font.ITALIC, 30 );

After you’ve created a font, you have to set it into a Graphics class before it can be used. The following example shows a newly created font being selected into the Graphics class:

Font font = new Font( "Helvetica", Font.BOLD, 50 );
g.setFont( font );


Note:  Many of the example applets and applications are simplified to zero in on the topic that’s being covered. These applets and applications will run just fine, but much of the startup code that’s created has been removed.

I’ve created a simple demonstration applet in the paint() method. Two fonts are created and used to draw strings to the applet window. Listing 10.3 shows this simple applet. See whether you can take this straightforward applet and enhance and embellish it to do other interesting things.

Listing 10.3 A Simple Applet for You to Try

1       import java.awt.*;
2       import java.applet.*;
3
4       public class Applet1 extends Applet
5       {
6           /**
7            * The entry point for the applet.
8            */
9           public void init()
10          {
11              initForm();
12          }
13
14          /**
15           * Initializes values for the applet and its components
16           */
17          void initForm()
18          {
19              this.setBackground(Color.lightGray);
20              this.setForeground(Color.black);
21           }
22
23          public void paint(Graphics g)
24          {
25              // Create the first font.
26              Font font1 = new Font( "TimesRoman",
27                  Font.PLAIN, 35 );
28              // Select the first font into the
29              // graphics class.
30              g.setFont( font1 );
31              // Draw the text to the Graphics
32              // class.
33              g.drawString( "This is the first line.", 5, 45 );
34
35              // Create the second font.
36              Font font2 = new Font( "Courier",
37                  Font.ITALIC, 45 );
38              // Select the second font into the
39              // graphics class.
40              g.setFont( font2 );
41              // Draw the text to the Graphics
42              // class.
43              g.drawString( "This is the second line.", 5, 100 );
44
45          }
46
47      }


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.