|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Its 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 }
ArcsArcs 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.
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 rectanglethe 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.
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. Lets 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 ); }
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.
To get the last two arguments, think in degrees around the circle. You find 0 degrees at 3 oclock; 90 degrees at 12 oclock; 180 degrees at 9 oclock; and 270 degrees at 6 oclock. 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. Its not the ending degree, as you might think. In this case, because youre going halfway around the circle, youre sweeping 180 degrees. Therefore, 180 degrees is the last argument youll give to the drawArc() method. The important part is that youre sweeping 180 degrees counterclockwise, which is in the positive direction in Java. If youre drawing an upside-down U, youre sweeping 180 degrees clockwise, which is in the negative direction, and the last argument is negative 180 degrees. Heres 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 ); }
The copyArea() and clearRect() MethodsThe copyArea() method copies an area of the components 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 components 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 thats 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 programs 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 );
|
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. |