Java Reference
In-Depth Information
image observer that is notified as more of the image becomes available. For the purposes of
this topic, the last parameter of the
drawImage
method will always be equal to null.
Below is a listing of all our code so far for the game Breakout. It displays the ball,
bricks, and paddle, but there is no code to move them. However, the different components
are separated into different classes, which makes adding the ball motion and keyboard and
mouse interaction easier. We will complete the game in Chapter 11.
import
java . util .
∗
;
import
javax . swing .
∗
;
import
java .awt .
∗
;
import
java .awt.geom.
∗
;
import
java . io .
∗
;
import
javax . imageio .
∗
;
public class
Test
{
public static void
main(String [] args)
{
BreakoutFrame frame =
new
BreakoutFrame () ;
frame. setVisible(
true
);
}
}
class
BreakoutFrame
extends
JFrame
{
public static final
int
HEIGHT = 600;
public static final
int
WIDTH = 488;
public static final
int
LOCATION X = 50;
public static final
int
LOCATION Y = 100;
public
BreakoutFrame ()
{
setDefaultCloseOperation(JFrame.EXITON CLOSE) ;
setLocation(LOCATIONX, LOCATION Y) ;
setSize(WIDTH, HEIGHT);
setResizable(
false
);
BreakoutPanel panel =
new
BreakoutPanel () ;
add(panel) ;
}
}
{
public static final int
NUM BRICK ROWS = 10;
public static final int
NUM BRICK COLUMNS = 30;
private
Ball ball =
new
Ball (Color . red) ;
private
ArrayList
class
BreakoutPanel
extends
JPanel
<
Brick
>
bricks =
new
ArrayList
<>
() ;
private
Paddle paddle =
new
Paddle(Color .BLUE) ;
private
Player player =
new
Player () ;
public
BreakoutPanel ()
{
for
(
int
row = 0; row
<
NUM BRICK ROWS ; row++)
{
for
(
int
col = 0; col
<
NUM BRICK COLUMNS ; c o l ++)
{
bricks .add(
new
Brick (row , col , getRandomColor () ) ) ;
}
}
}
public
Color getRandomColor ()
{
Color color =
new
Color ((
int
)
(Math . random ( )
256) ,
∗