Java Reference
In-Depth Information
Figure 7-32.
Select Negative from the Process menu to invert pixel components.
EXERCISES
ThefollowingexercisesaredesignedtotestyourunderstandingofAWT,Swing,and
Java 2D:
1. Create an AWT application named
RandomCircles
that presents a can-
vasfordisplayingfilledcircularovals(renderedvia
fillOval()
).Anew
randomly colored, randomly positioned, and randomly sized (from 5 to 35
pixels for both the width and height—use the same value for each extent)
filledcircleisdisplayedbythe
paint()
methodatapplicationstartupand
eachtimethemousebuttonispressedwhilethemousepointerappearsover
the canvas. At startup, you might notice that the canvas first displays one
randomlycolored/positioned/sizedcircleandimmediatelydisplaysanother.
This has to do with AWT invoking the
paint()
method at least twice at
startup.Becauseyoudon'tknowhowmanytimes
paint()
willbecalled,
neverrelyon
paint()
tochangethestateofthecomponent.Instead,you
must only use this method to render the component in response to the cur-
rent state.
Nimbus Look and Feel before the GUI is created.
3. Swing's
AbstractButton
class, which
JButton
extends, declares a
void setMnemonic(int mnemonic)
methodforsettingakeyboard
mnemonic
(memoryaid),whichservesasakeyboardshortcutalternativeto
clickingamousebutton.Theargumentpassedto
mnemonic
isoneofthe
virtual key constants declared in the
KeyEvent
class (e.g.,
VK_C
). When
you invoke this method, the first occurrence of the character (from left to
rightonthebuttonlabel)definedbythemnemonicisunderlined.Whenyou
press this key with the current look and feel's mouseless modifier (typic-
TempVerter
applicationtoassignthe
VK_C
mnemonictotheConvertto
Celsius button and the
VK_F
mnemonic to the Fahrenheit button.
suffersfromaliasingthatmakesthetextandgraphicslookjagged.Youcan
fixthisproblembyinstallingtheantialiasingrenderinghintbeforerendering
graphics.Createanewversionof
SplashCanvas
thattakesadvantageof
antialiasing.