Java Reference
In-Depth Information
methodtoloadanddisplaytheimage.
ScrollPane
's
void doLayout()
method
lays out this container by resizing its child (the image canvas) to its preferred size.
Exit'sactionlistenerisinvokedwhentheuserselectsExit.Itinvokes
dispose()
on
the frame window to dispose of this window's (and the contained components') native
screenresources.Furthermore,awindowclosingeventistriggeredandtheframewin-
dow's window listener's
windowClosing()
method is invoked.
The
main()
method creates the GUI on the EDT. It instantiates a scrollpane, and
setsitspreferredsizetoanarbitraryvaluethatservesastheframewindow'sdefaultsize
(following a
pack()
method call).
The
createGUI()
method call installs the menubar on its
Frame
argument, and
returnstheimagecanvas,whichissavedinthe
ImageCanvas
classfieldsothatitcan
be accessed from the Open menuitem listener. The image canvas is also added to the
scrollpane, and the scrollpane is added to the frame window.
Listing 7-5
. Displaying a user-selected image
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
class ImageCanvas extends Canvas
{
private Image image;
@Override
public void paint(Graphics g)
{
// drawImage() does nothing when image contains the
null reference.
g.drawImage(image, 0, 0, null);
}
void setImage(Image image)
{
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 1);