Java Reference
In-Depth Information
}
public boolean
isAlive()
{
return
(numLives
>
0) ;
}
public void
draw(Graphics2D g2)
{
try
{
Image image = ImageIO. read (
new
File(
"player.gif"
));
for
(
int
x=0;x
<
numLives ; x++)
{
g2 . drawImage( image , x
∗
IMAGE DISTANCE,
IMAGE Y,
null
);
}
}
{}
catch
(Exception my)
}
}
9.4 Summary
This chapter introduced the Swing framework in Java. We showed how to create windows
and panels. We also showed how to display text, shapes, and images inside a panel and how
to change the color of drawings. The next chapter shows how the drawings that are inside
a window can react to different events, such as elapsed time period, mouse movement, and
keyboard keys being pressed.
9.5 Syntax
JFrame
⇒
The window class (we usually inherit from it).
JPanel
⇒
The panel class (we usually inherit from it).
⇒
panel.repaint();
Repaints the panel.
⇒
Sets the location (top left corner) of the frame to
frame.setLocation(10,10);
position (10,10).
frame.setSize(200,200);
⇒
Makes the frame 200 by 200.
frame.setResizable(false);
⇒
Makes the frame non-resizable. It is resizable by
default.
frame.setVisible(true);
⇒
Makes the frame visible. By default, it is invisible.
frame.setDefaultCloseOperation(
...
);
⇒
The parameter can be
EXIT ON CLOSE
,
IDE ON CLOSE
,
DISPOSE ON CLOSE
,and
DO NOTHING ON CLOSE
. All parameters are de-
fined as constants in the class
JFrame
. The method specifies the action that will be
performed when the user tries to close the frame.