Click Here!
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


It’s a lot of fun to experiment with ovals. You can create programs that produce interesting results. The following example applet, given in Listing 11.2 and shown in Figure 11.8, creates random filled and outlined ellipses in the Java window.

Listing 11.2 The Ovals Applet

1   // Applet1.java
2
3   import java.awt.*;
4   import java.applet.*;
5
6   /**
7    * This class reads PARAM tags from its HTML host page and sets
8    * the color and label properties of the applet. Program execution
9    * begins with the init() method.
10   */
11  public class Applet1 extends Applet
12  {
13      /**
14       * The entry point for the applet.
15       */
16      public void init()
17      {
18          initForm();
19
20      }
21
22      /**
23       * Initializes values for the applet and its components
24       */
25      void initForm()
26  {
27          this.setBackground(Color.lightGray);
28          this.setForeground(Color.black);
29      }
30
31      public void paint(Graphics g)
32      {
33
34          for( int i=0; i<10; i++)
35          {
36              int x, y, w, h;
37              x = (int)( Math.random() * 300 ) + 20;
38              y = (int)( Math.random() * 200 ) + 20;
39              w = (int)( Math.random() * 150 ) + 30;
40              h = (int)( Math.random() * 100 ) + 30;
41              if( Math.random() > .5 )
42                  g.drawOval( x, y, w, h );
43              else
44                  g.fillOval( x, y, w, h );
45          }
46
47      }
48  }


Figure 11.8  The program shown in Listing 11.1 draws random, filled, and outlined ellipses in the Java window.

Arcs

Arcs are probably the most complex shapes to draw in Java. An arc is nothing more than part of an oval. Figure 11.9 shows an application with some arcs drawn in it.


Figure 11.9  Arcs drawn in a Java window.

There are two ways of drawing arcs. The drawArc() method draws an arc without filling it. The fillArc() method draws an arc and fills its interior. Both take six arguments. The first two arguments are the x and y coordinates of the upper-left corner of the bounding rectangle—the same as for the oval. The next two arguments are the width and height of the bounding rectangle. Once again, the first four arguments of the arc methods are exactly the same as those of the oval methods. The fifth and sixth arguments to the arc methods specify the starting and stopping angles for the arcs. Filled arcs are drawn as if they were sections of a pie instead of being joined across the two endpoints. Both endpoints are joined to the center of the circle.


Tip:  The important thing to understand about arcs is that you’re actually formulating the arc as an oval and then drawing only some of that oval. The starting corner and the width and height are not the starting point and width and height of the actual arc that is drawn onscreen; they’re the width and height of the full ellipse of which the arc is a part.

The first two arguments determine the size and shape of the arc. The last two arguments, the starting and ending degrees, determine the starting and ending points. Let’s draw a simple arc, an example that will be a U-shape. Figure 11.10 shows this arc drawn in an applet window. The source code for the paint() method of the program follows:

       public void paint( Graphics g )
        {
           g.drawArc( 10, 50, 200, 150, 180, 180 );
   }


Figure 11.10  An arc in the shape of a U.

The U-shaped arc is actually part of a complete circle. To construct the method to draw this arc, the first thing you do is think of the arc as a complete circle. Then, you find the x and y coordinates and the height and width of the circle. Those four values are the first four arguments of the drawArc() or fillArc() methods. Figure 11.11 shows you how to get those values from the arc.


Figure 11.11  Constructing a circular arc.

To get the last two arguments, think in degrees around the circle. You find 0 degrees at 3 o’clock; 90 degrees at 12 o’clock; 180 degrees at 9 o’clock; and 270 degrees at 6 o’clock. In this example, the starting point is to the left of the circle where the U begins at 180 degrees. That will be the fifth argument.

The sixth and final argument is another degree value indicating how far around the circle to sweep, and the direction to go in. It’s not the ending degree, as you might think. In this case, because you’re going halfway around the circle, you’re sweeping 180 degrees. Therefore, 180 degrees is the last argument you’ll give to the drawArc() method. The important part is that you’re sweeping 180 degrees counterclockwise, which is in the positive direction in Java. If you’re drawing an upside-down U, you’re sweeping 180 degrees clockwise, which is in the negative direction, and the last argument is negative 180 degrees.

Here’s the code for the example of drawing a U. A filled U is below it, as shown in Figure 11.12.

       public void paint( Graphics g )
        {
      g.drawArc( 10, 10, 200, 150, 180, 180 );
      g.fillArc( 10, 170, 200, 150, 180, 180 );
    }


Figure 11.12  Two semicircular arcs.

The copyArea() and clearRect() Methods

The copyArea() method copies an area of the component’s window by a distance specified by two arguments. It also needs a coordinate for the upper-left corner of the source rectangle. This method copies downward and to the right. To copy an area of the component to the left or upward, specify a negative value for the distance value. If a portion of the source rectangle lies outside the bounds of the component or is obscured by another window or component, copyArea() will be unable to copy the associated pixels. The area that is omitted can be refreshed via a call to the component’s paint() method.

The following source code shows you how to copy the rectangle at (10,25) with a width of 100 and a height of 80 to a rectangle that’s 250 pixels to the right and 280 pixels down:

g.copyArea( 10, 25, 100, 80, 250, 280 );

The clearRect() method clears a rectangular region of your program’s window. It takes four arguments: the first two are the upper-left corner of the rectangle to clear; the next two, the width and height of the rectangle to clear. The following source code shows how to use clearRect():

int x = 50, y = 65;
int nWidth = 100, nHeight = 150;
g.clearRect( x, y, nWidth, nHeight );


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.