|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
As you can see, the fillRect() method takes the same parameters as the drawRect() method. Figure 39.4 shows the result of using the drawRect() and fillRect() methods. The rectangle at the left of the figure is drawn with the drawRect() method, and the one at the right is drawn with the fillRect() method. The body of this applet contains the lines g.drawRect(20, 20, 200, 100); g.fillRect(240, 20, 200, 100); The Java Graphics class also provides two methods for drawing rectangles with rounded corners. The drawRoundRect() and fillRoundRect() methods are similar to the drawRect() and fillRect() methods except that they take two extra parameters: the arcWidth and arcHeight parameters. Their complete definitions are
public abstract void drawRoundRect(int x, int y, int width, ⇒int height, int arcWidth, int arcHeight) public abstract void fillRoundRect(int x, int y, int width, ⇒int height, int arcWidth, int arcHeight) The arcWidth and arcHeight parameters determine how the corners will be rounded. Using an arcWidth of 10 results in including the leftmost 5 pixels and the rightmost 5 pixels of each horizontal side of a rectangle in the rectangles rounded corners. Similarly, using an arcHeight of 8 includes the topmost 4 pixels and the bottommost 4 pixels of each vertical side of a rectangle in the rectangles rounded corners. Figure 39.5 shows rectangles with rounded corners constructed using these parameter values. The body of this applet contains the lines g.drawRoundRect(20, 20, 200, 100, 40, 20); g.fillRoundRect(240, 20, 200, 100, 40, 20);
In addition to the regular rectangles and those with rounded corners, the Graphics class provides two methods for drawing three-dimensional rectangles: the draw3DRect() and fill3DRect() methods. The complete definitions of the three-dimensional rectangle methods are: public void draw3DRect(int x, int y, int width, ⇒int height, boolean raised) public void fill3DRect(int x, int y, int width, ⇒int height, boolean raised) The syntax for the draw3DRect() and fill3DRect() methods is similar to the drawRect() and fillRect() methods except that they have an extra parameter added to the end of their parameter lists. It is a boolean parameter that results in a raised rectangle effect when set to true. If it is set to false, the face of the rectangle shows a sunken effect. The applet in Listing 39.3 draws raised and lowered, filled and unfilled rectangles.
Listing 39.3 Rect3D.java3-D Rectangles Have a Shadow that Assumes Light Comes from the Upper-Left Corner import java.awt.Graphics; // This applet draws four varieties of 3D rectangles. // It sets the drawing color to the same color as the // background. public class Rect3D extends java.applet.Applet { public void paint(Graphics g) { // Make the drawing color the same as the background g.setColor(getBackground()); // Draw a raised 3D rectangle in the upper-left g.draw3DRect(20, 20, 200, 100, true); // Draw a lowered 3-D rectangle in the upper-right g.draw3DRect(240, 20, 200, 100, false); // Fill a raised 3D rectangle in the lower-left g.fill3DRect(20, 140, 200, 100, true); // Fill a lowered 3D rectangle in the lower-right g.fill3DRect(240, 140, 200, 100, false); } } Figure 39.6 shows the output from the Rect3D applet. The raised rectangles appear the same when filled or unfilled because the drawing color is the same color as the background. If a different drawing color were used, the filled rectangle would be filled with the drawing color, whereas the unfilled rectangle would still show the background color.
Drawing OvalsThe Java Graphics class provides two methods for drawing ovals or circles: the drawOval() and fillOval() methods. The full definitions of these methods are as follows: public abstract void drawOval(int x, int y, int width, int height) public abstract void fillOval(int x, int y, int width, int height)
To draw an oval, imagine surrounding the oval with a rectangle that just touches the oval at its widest and highest points as illustrated in Figure 39.7. Listing 39.4 shows the code.
Listing 39.4 OvalDemo.javaThink of Ovals as Lying Inside a Bounding Rectangle import java.awt.Graphics; import java.awt.Color; public class OvalDemo extends java.applet.Applet { public void paint(Graphics g) { // Draw a gray rectangle 200 pixels wide and 100 pixels // high with its upper left corner at (20, 20). g.setColor(Color.gray); g.drawRect(20, 20, 200, 100); // Draw an oval inside the rectangle created above. g.setColor(Color.black); g.drawOval(20, 20, 200, 100); // Draw the oval again but to the right of the oval above. g.drawOval(240, 20, 200, 100); } }
|
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. |