Java Reference
In-Depth Information
public double getY() {
return shape . getY() ;
}
public double getHeight () {
return shape . getHeight () ;
}
public double getWidth() {
return shape . getWidth() ;
}
{
return shape . intersects ( other . shape . getBounds () ) ;
public boolean intersects (BreakoutShape other )
}
public boolean below(BreakoutShape other )
{
return getY() > = other .getY() + other . getHeight () ;
}
public boolean above(BreakoutShape other ) {
return getY() + getHeight() < =other.getY();
}
public boolean leftOf (BreakoutShape other ) {
return getX() + getWidth () < =other.getX();
}
public boolean rightOf (BreakoutShape other ) {
return getX() > = other . getX() + other . getWidth() ;
}
public void move( int dx , int dy) {
shape . setFrame(getX() + dx , getY() + dy , getWidth() , getHeight () ) ;
}
} Let us examine the below method in greater detail, where the above , leftOf ,and
rightOf methods work in a similar way; see Figure 11.2. Assume that the ball is the
object on which the method is called and the other object is a brick. The method adds
other.getY() and other.getHeight() . This will give us the Y coordinate of the bottom
side of the brick. If this number is smaller than or equal to the y coordinate of the ball (i.e.,
the top of the ball), then the method will return true (i.e., that ball is below the brick).
Otherwise, the method will return false .
Lastly, let us examine the new version of the BreakoutPanel class, which is also the
finalversion.Nowan ArrayList of bricks will be created. Before the ball is moved, we will
check to see if the virtual ball intersects with one of the bricks. If it does, then the trajectory
of the ball is changed accordingly and the brick is removed from the ArrayList .
public class BreakoutPanel extends JPanel {
private static final int NUM BRICK ROWS = 10;
private static final int NUM BRICK COLUMNS = 30;
private javax . swing .Timer timer ;
private Ball ball ;
private ArrayList < Brick > bricks ;
private Paddle paddle ;
 
Search WWH ::




Custom Search