Java Reference
In-Depth Information
Rectangle2D textBox = myFont. getStringBounds(s , g2.
getFontRenderContext () ) ;
g2. drawString(s , (
int
) (getWidth() / 2
−
textBox . getWidth() / 2) , (
int
) (getHeight () / 2
−
textBox . getHeight ())) ;
}
public void
paintComponent(Graphics g)
{
super
. paintComponent(g) ;
Graphics2D g2 = (Graphics2D) g ;
if
( bricks . size () == 0 && ! gameStarted)
{
showMessage(
"YOU WIN!"
,g2);
gameStarted =
false
;
}
{
showMessage(
"GAME OVER!"
,g2);
gameStarted =
false
;
else if
(!player . isAlive())
}
{
ball .draw(g2);
paddle .draw(g2) ;
for
(Brick brick : bricks)
else
{
brick .draw(g2) ;
}
if
(gameStarted)
{
player .draw(g2) ;
}
}
public void
changeBallSpeed(
int
speed)
{
timer . setDelay(speed) ;
}
class
TimeListener
implements
ActionListener
{
public void
bounceBall(Ball ball , Brick brick)
{
if
(ball.below(brick))
{
b a l l . goDown ( ) ;
if
( ball . above( brick ) )
{
ba l l . goUp ( ) ;
if
(ball.leftOf(brick))
{
ball . goLeft () ;
if
(ball . rightOf(brick))
{
ball . goRight () ;
}
}
public void
bounceBall(Ball ball , ArrayList
<
Brick
>
bricks)
{
if
(bricks.size() == 0)
{
return
;
if
(bricks.size() == 1)
{
bounceBall(ball , bricks . get (0)) ;
return
;