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.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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


FIGURE 39.4  The left rectangle was drawn with drawRect(), and the one on the right was drawn with fillRect().

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 rectangle’s 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 rectangle’s 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);


FIGURE 39.5  Rectangles with rounded corners were drawn using the drawRoundRect() and fillRoundRect() methods.

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.


NOTE:  The three-dimensional rectangles discussed here do not actually exist as three-dimensional objects. A shadow effect is used to give the illusion that they are three dimensional. This effect consists of a relatively dark color along two adjacent sides of a rectangle and a light color along the opposite two sides. Java assumes the light source to be from the upper-left corner of the screen.
ON THE WEB
http://java.sun.com/products/java-media/3D/ Sun is developing a set of classes for writing true 3D objects. Learn more on this Web page.

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.


Three-dimensional rectangles typically look best when their color matches the background color. Three-dimensional effects depend on several aspects of your system, including your computer graphics and color capabilities and the browser you use. If your three-dimensional effects are less than satisfactory, try using different colors. Color manipulation using Java is covered in detail later in this chapter in the section titled “Displaying Colors.”


FIGURE 39.6  The draw3DRect() and fill3DRect() methods use shading to produce a three-dimensional effect.

Drawing Ovals

The 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)


NOTE:  A circle is an oval with its width equal to its 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.


FIGURE 39.7  The same oval is inside its bounding rectangle on the left and by itself on the right.

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);
   }
}


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.