Java Reference
In-Depth Information
private
Ball(Color color , Ellipse2D.Double ellipse )
{
super
(ellipse ,color ,
true
);
}
public
Ball getVirtualBall ()
{
return new
Ball(
super
.getColor() ,
new
Ellipse2D.Double(getX()+dx,
getY()+dy , SIZE , SIZE) ) ;
}
public void
move ( )
{
if
(getX() + dx
<
0)
{
dx = 1;
if
(getX() + getWidth() + dx
>
panel .getWidth() )
{
1;
if
(getY() + dy
<
0)
dx =
−
{
dy = 1;
if
(getY()+getHeight () +dy
>
panel . getHeight ())
{
dy =
−
1;
super
.move(dx, dy);
}
public void
goUp ( )
{
dy =
−
1;
}
public void
goDown ( )
{
dy = 1;
}
public void
goLeft ()
{
dx =
−
1;
}
{
public void
goRight ()
dx = 1;
}
}
The
getVirtualBall
method creates a new ball that corresponds to the next position
of the ball. It uses a new private constructor that creates a new ball object from the given
color and elliptical shape. We have also included the
goDown()
,
goUp()
,
goLeft()
,and
goRight()
methods. They can be used to change the trajectory of the ball.
We also need to modify the
BreakoutShape
class and add the following method.
class
BreakoutShape
{
...
public boolean
intersects (BreakoutShape other )
{
return
shape . intersects ( other . shape . getBounds () ) ;
}
...
}