Java Reference
In-Depth Information
displayed) when you execute
java Planets
; and the
main()
entry-point
method that launches the application.
•
SwingCanvas
is organized into
iiPhoto
and
d
(dimension)
static
fields, a
SwingCanvas(ImageIcon iiPhoto)
constructor that dimen-
sions this component to the size of each image and saves the initial image
iconfordisplay,anoverriding
Dimension getPreferredSize()
meth-
od that returns the preferred size of this component so that images are fully
displayed, an overriding
void paint(Graphics g)
method that paints
the current image icon's image over the component's surface, and a
void
setPhoto(ImageIcon iiPhoto)
methodthatassignsanewimageicon
tothecanvascomponentandcausesitsimagetobepaintedoverthecompon-
ent's drawing surface.
The need for brevity restrains me from presenting the entire source code, so I'll
presentcodefragmentsinstead—you'llfindthecompletesourcecodeinthisbook'sac-
companying code file (see this topic's introduction for more information).
Consider the following
initDB()
source code:
static void initDB()
{
String[] planets = { "mercury", "venus", "earth",
"mars", "jupiter",
"saturn", "uranus", "neptune" };
double[] diameters = { 4880, 12103.6, 12756.3, 6794,
142984, 120536,
51118, 49532 };
double[]
masses
=
{
3.3e23,
4.869e24,
5.972e24,
6.4219e23, 1.9e27,
5.68e26, 8.683e25, 1.0247e26 };
double[] distances = { 0.38, 0.72, 1, 1.52, 5.2, 9.54,
19.218, 30.06 };
String url = "jdbc:derby:planets;create=true";
try (Connection con = DriverManager.getConnection(url))
{
try (Statement stmt = con.createStatement())
{
String
sql
=
"create
table
planets(name
varchar(30),"+