Java Reference
In-Depth Information
f.dispose();
}
};
f.addWindowListener(wa);
sp = new ScrollPane();
sp.setPreferredSize(new
Dimen-
sion(300, 300));
sp.add(ic = createGUI(f));
f.add(sp);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
ImageViewer
declares an
ImageCanvas
class field that references the image
canvas used to display the image. It also declares a
ScrollPane
class field whose
scrollpanecontainstheimagecanvas,sothatyoucanscrollhorizontallyandvertically
overimagesthataretoolargetobedisplayedintheirentiretyatthecurrentscreenresol-
ution,anda
Toolkit
instancewhose
getImage()
methodisusedtostarttheimage-
loading process for the user-selected image.
The
ImageCanvas createGUI(final Frame f)
methodcreatesaGUIcon-
sistingofamenubarwithasingleFilemenuandanimagecanvas.FileconsistsofOpen
and Exit menuitems.
Open'sactionlistenerisinvokedwhentheuserselectsOpen…(
...
indicatesthata
dialogboxwillbedisplayed).Thislistenerfirstinstantiates
FileDialog
anddisplays
it; the user sees a platform-specific dialog box for selecting a file.
Whentheuserclosesthisdialogbox,
FileDialog
's
String curFile(
)meth-
odiscalledtoreturnthenameoftheselectedfile;thismethodreturnsnullwhenafile
has not been selected.
If null is not returned,
FileDialog
's
String getDirectory()
method is
calledtoreturnthedirectoryname,whichisprependedtothefilenamesothattheselec-
tedfilecanbelocated.Theresultingpathnameispassedto
Toolkit
's
getImage()
method,andthereturned
Image
instanceispassedto
ImageCanvas
's
setImage()