Java Reference
In-Depth Information
The RectangularShape abstract class is part of the Java library. Shapes that are
determined by a surrounding rectangle, such as Rectangle and Ellipse inherit from
this class. It supports methods for getting the x and y coordinates of the top left
corner of the rectangle and the width and height of the rectangle. The class supports
the intersects method that checks if the rectangular shape intersects a rectangle. The
class also supports the setFrame method that can be used to change the coordinates
of the rectangular shape and the getBounds method that returns the coordinates of
the shape as an object of type Rectangle .
Note that the BreakoutShape class contains an object of type RectangularShape .This
is a superclass for the classes Rectangle2D.Double and Ellipse2D.Double , and therefore
can be used to store either a reference to an ellipse or rectangle. In both cases, we store the
top left corner and the width and height of the surrounding rectangle. The draw method
is straightforward. It uses the input brush to draw the shape. If we need to fill the shape,
then we call the fill method to do so. Note that the Player class is not a subclass of the
BreakoutShape class because an image is drawn differently and an image is not a subclass
of the RectangularShape class.
The BreakoutShape class has a getter and setter method for the color of the shape.
For convenience, we have also included methods that show the current coordinates of the
surrounding rectangle. The move method allows us to move the breakout shape in the X
direction by dx andinthe Y direction by dy . For example, the ball and the paddle will be
moved using this method.
Next, let us focus our attention on the Ball class, which inherits from the
BreakoutShape class. The code for the class is shown below.
import java .awt . ;
import java .awt.geom. ;
public class Ball extends BreakoutShape
private static final int SIZE = 10;
private static final int START X = 200;
private static final int START Y = 400;
private int dx = 1;
private int dy =
private BreakoutPanel panel ;
public Ball (Color color , BreakoutPanel panel) {
super ( new Ellipse2D.Double(STARTX, STARTY, SIZE, SIZE) , color ,
true );
this . panel = panel ;
public void move ( ) {
if (getX() + dx < 0)
dx = 1;
if (getX() + getWidth() + dx > panel .getWidth() )
dx = 1;
if (getY() + dy < 0)
dy = 1;
Search WWH ::

Custom Search