Java Reference
In-Depth Information
public Brick add(Brick other) {
Rectangle2D rectangle1 = super . getBounds () ;
Rectangle2D rectangle2 = other . getBounds () ;
rectangle1 .add(rectangle2);
return new Brick(rectangle1 , super .getColor());
The add method is used to add two bricks. Each of the bricks is first converted into a
rectangle using the getBounds() method. Next, the add method for the Rectangle class is
used to add the two rectangles. Finally, a brick is created from the new rectangle. Note that
the constructor that creates a brick out of a rectangle is private because we only want to
use it from within the Brick class. The add method is used when the virtual ball intersects
two bricks. In this case, we will merge the bricks and bounce the ball off the new big brick.
In practice, this only happens when there are two bricks that are adjacent horizontally or
Next, we will modify the BreakoutShape class. When the virtual ball intersects one or
more bricks, we want to determine the relative location of the ball to the brick. That is, is
the ball to the left, right, above, or below the brick. The trajectory of the ball will change
public class BreakoutShape
private Color color ;
private boolean fill ;
private RectangularShape shape ;
public BreakoutShape(RectangularShape shape , Color color , boolean
fill) {
this . shape = shape ;
this . color = color ;
this . fill = fill ;
} public Color getColor ()
return color ;
protected Rectangle getBounds () {
return shape . getBounds () ;
} public void changeColor(Color color )
this . color = color ;
public void draw(Graphics2D g2)
g2 . setPaint ( color ) ;
g2 . draw( shape) ;
if (fill) {
g2 . f i l l ( shape) ;
public double getX() {
return shape . getX() ;
Search WWH ::

Custom Search