Java Reference
In-Depth Information
The
paintComponent
method in the
SplatPanel
class draws the panel by call-
ing the
draw
method of each circle. Essentially, the
SplatPanel
class asks each
circle to draw itself.
The
Circle
class is shown in Listing 4.9. It defines instance data to store the
size of the circle, its (
x
,
y
) location, and its color. These values are set using the
constructor, and the class contains all the appropriate accessor and mutator meth-
ods. The
draw
method of the
Circle
class simply draws the circle based on the
values of its instance data (its current state).
LISTING 4.9
//********************************************************************
// Circle.java Author: Lewis/Loftus
//
// Represents a circle with a particular position, size, and color.
//********************************************************************
import
java.awt.*;
public class
Circle
{
private int
diameter, x, y;
private
Color color;
//-----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
//-----------------------------------------------------------------
public
Circle (
int
size, Color shade,
int
upperX,
int
upperY)
{
diameter = size;
color = shade;
x = upperX;
y = upperY;
}
//-----------------------------------------------------------------
// Draws this circle in the specified graphics context.
//-----------------------------------------------------------------
public void
draw (Graphics page)
{
page.setColor (color);
page.fillOval (x, y, diameter, diameter);
}
Search WWH ::
Custom Search