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 11
Drawing

It’s easy to get an application or an applet up and running with Visual J++. All you have to do is create a startup program and put some controls in the Java window, and you can have a program that performs useful functions.

But before long you’ll outgrow this capability. You’ll want to make your programs fancy; you’ll want to make them look better; you’ll want them to communicate more than what your text controls can. Today you’ll learn how to use the Java drawing commands to make your programs look great.

In this chapter, you will do the following:

  Learn how to draw these items:
Lines
Rectangles
Ovals
Arcs
Polygons
  See a demonstration program that uses these graphics techniques
  Learn how to copy areas in Java windows
  Learn how to clear your program’s window

After you’ve mastered all these techniques, your programs will take on a new dimension. They’ll start to look professional, unlike some of the amateur and shareware programs you currently see out there. With the drawing commands you’ll learn today, your programs will sparkle.

The Graphics Class

In the preceding chapter, we covered drawing fonts to Java windows. We used the Graphics class and two of its member functions to perform this task. The two methods we used were drawString() and drawChars().

We got the Graphics class in an applet’s paint() method or an application’s onPaint() method. If you’re writing an applet, it’s possible, though, to get a Graphics class for a component in another method besides the paint() method. You can use the getGraphics() method to obtain a Graphics class with which you can do all of your drawing.

The following line of source code shows you how to get a Graphics class in an applet:

Graphics g = getGraphics();


Note:  When you draw in the paint() method, you’ll already have a Graphics class and won’t have to use the getGraphics() method. When you draw elsewhere, you’ll have to get a Graphics class with the getGraphics() method.

So now, if you’re in a method that doesn’t have anything to do with a paint() method, you can go ahead and get a Graphics class and draw to a Java window. The only problem is that when the applet redraws or refreshes in response to some sort of a window move or window message, whatever you drew to the window that isn’t in the paint() method will not be redrawn.

In what situation would you want to get a Graphics class outside of the paint() method and paint to the window? One example might be when you want to temporarily draw a small figure to a large window. You’d draw the figure to the program window, and then when you wanted it to go away, you could call repaint().

Lines

You can draw a line with the Graphics class drawLine() method. This method takes four arguments. The first and second arguments are the x and y coordinates of the starting point of your line; the third and fourth arguments are the x and y coordinates of the ending point of your line.

The following example draws a line in an applet’s paint() method:

      public void paint(Graphics g)
           {
        // Draw from (25, 30) to (160, 170)
        g.drawLine( 25, 30, 160, 270 );
      }

When you draw a line in an application’s onPaint() method, your source code will look slightly different from that in the preceding example. That’s because you don’t get a Graphics class in an onPaint() method; you get a PaintEvent class. Of course, the PaintEvent class has a Graphics class as one of its member variables.

The following example shows the source code for drawing a simple line in a Visual J++ application’s onPaint() method. Figure 11.1 shows what this application looks like when it runs.

       protected void onPaint(PaintEvent pe)
        {
     pe.graphics.drawLine( 25, 30, 160, 270 );
   }


Figure 11.1  This simple Java application draws a line in the onPaint() method.

One of the problems with the drawLine() method is that it draws a line that’s only a single pixel wide. Many times when you’re drawing, you want wide lines—lines that have widths of three, four, five, six, and more pixels. One of the ways I’ve solved this dilemma is to write special functions that draw wide lines. Diagonal lines are slightly more difficult. The best way to accomplish this job is by drawing a polygon that simulates a thick line—I’ll show you how after talking about polygons later. As an example, I have two methods to show you. One is called drawHorizontalLine(); the other is called drawVerticalLine(). They draw horizontal and vertical lines of any thickness. The source code is shown in Listing 11.1.

Listing 11.1 Methods to Draw Thick Horizontal and Vertical Lines

1       public void drawHorizontalLine( Graphics g, int x1, int y1,
2           int x2, int y2, int nThickness )
3       {
4           for( int i=0; i<nThickness; i++ )
5               g.drawLine( x1, y1 + i, x2, y2 + i );
6       }
7
8       public void drawVerticalLine( Graphics g, int x1, int y1,
9           int x2, int y2, int nThickness )
10      {
11          for( int i=0; i<nThickness; i++ )
12              g.drawLine( x1 + i, y1, x2 + i, y2 );
13      }

Before you draw lines, you’ll also probably want to set the color. You do this the same way as you did when you drew text to the Java window. The first thing you do is either create a color object of your own or use one of the predefined Color classes, such as Color.white or Color.black. You then select your color object into the Graphics class. It’s just like drawing with text: from that point on, anytime you draw to the Graphics class, the draw operation will be in that color.


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.