Java Reference
In-Depth Information
(
int
)
(Math . random ( )
256) , (
int
)
(Math . random ( )
256)) ;
∗
∗
if
(getBackground() . equals ( color ))
{
return
Color .RED;
return
color ;
}
public void
showMessage( String s , Graphics2D g2)
{
Font myFont =
new
Font(
"SansSerif"
, Font .BOLD + Font . ITALIC, 40) ;
g2. setFont(myFont) ;
g2 . setColor (Color .RED) ;
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)
{
showMessage(
"YOU WIN!"
,g2);
}
else if
(!player . isAlive())
{
showMessage(
"GAME OVER!"
,g2);
}
else
{
ball .draw(g2);
paddle .draw(g2) ;
for
(Brick brick : bricks)
{
brick .draw(g2) ;
}
}
player .draw(g2) ;
}
}
class
Ball
{
public static final
int
SIZE = 10;
public static final
int
START X = 200;
public static final
int
START Y = 400;
private
Color color ;
private int
x, y;
public
Ball (Color color )
{
this
. color = color ;
x=STARTX;
y=STARTY;
}
public void
draw(Graphics2D g2)
{
g2 . setPaint ( color ) ;
Ellipse2D e =
new
Ellipse2D.Double(x, y, SIZE, SIZE) ;
g2 . f i l l (e) ;
}
}