Java Reference
In-Depth Information
The reason is that the Double class is actually defined inside the Ellipse class, where more
information about static nested classes is provided in the next chapter.
The Ellipse2D class has two subclasses: Ellipse2D.Double and Ellipse2D.Float .
The constructor of the first class takes, as parameters, doubles, while the constructor of the
second class takes, as parameters, floats. The four parameters of the constructor are the
coordinates of the top left corner of the surrounding rectangle followed by the width and
height of the surround rectangle.
The astute reader may be wondering why the constructors of the classes
Ellipse2D.Double and Ellipse2D.Float take as input numbers of type double and float ,
respectively, when the computer can only set the color of pixels that have integer coordi-
nates. After all, it is impossible to set the color of a pixel with coordinates (2.4, 3.1) because
such a pixel simply does not exist. The answer is that although the pixel does not exist, the
color of this pixel can be set or at least appear as though it is set. The color of a virtual
pixel that has non-integer coordinates is set by setting the colors of its four neighboring
pixels that do have integer coordinates. In this way, an optical illusion that a pixel that
does not exist is set is created.
The statement g2.fill(e) draws the ellipse e using the brush g2 and fills it. Alterna-
tively, the expression g2.draw(e) just draws the ellipse without filling it.
Next, let us examine the Paddle class. It is similar to the Ball class with the only
exception that a rectangle instead of a circle is drawn.
class Paddle {
public static final
int WIDTH = 5 0 ;
public static final
int HEIGHT = 1 0 ;
public static final
int START X = 200;
public static final
int START Y = 430;
private Color color ;
private int x, y;
public Paddle(Color color)
{
this . color = color ;
x=STARTX;
y=STARTY;
}
{
public void draw(Graphics2D g2)
g2 . setPaint ( color ) ;
Rectangle2D r = new Rectangle2D .Double(x, y, WIDTH, HEIGHT) ;
g2. fill (r);
}
}
The constructor sets the color of the paddle and its initial (x,y) coordinates (i.e., the
coordinates of its top left corner). The draw method takes as input the drawing brush. It
first sets the color of the drawing bush. It then creates a rectangle and draws it with its
interior filled.
Note that the Rectangle2D.Double class inherits from the Rectangle2D class. The
Rectangle2D class has two subclasses: Rectangle2D.Double and Rectangle2D.Float .In
the first class, the coordinates of the rectangle are specified as doubles, while in the second
class they are specified as floats. Note that the first two numbers in the constructor of
the Rectangle2D.Double class specify the x and y coordinates, respectively, of the top left
corner of the rectangle, while the next two numbers specify the width and height of the
rectangle, respectively.
Letusnextexaminethe Brick class.
 
Search WWH ::




Custom Search