Beginning Java 2- JDK 1.3 Version
Images and Animation
How It Works
In the init() method we try to read the two parameter values.
If the <PARAM> tags are not specified, then the
String object returned by the getParameter()
calls will be null. In this case the data members
fadeTime and frameRate will be left at their
default values. We use the values in these members to calculate
maxCount, the count of the number of steps needed to
completely fade the image.
Since the ImagePanel class loads its own icon image, all we
have to do in the init() method is create the
ImagePanel object and add it to the content pane for the
applet. We pass the size of the applet to the ImagePanel
constructor, which will scale the image to fit. We also initialize the
composite member that is the AlphaComposite object used in the
paint() method for the imagePanel object to
SrcOver, which implements the SRC_OVER rule with
the alpha set to 1.
The Timer object that we create in the start()
method manages the animation. After initializing count to the number of
steps to a complete fade, we calculate the increment for the alpha value
between steps, and the time interval in milliseconds between one step and
the next. We use the timer's schedule() method to fade the
image. In the run() method for the TimerTask object, the alpha
is set to a new value – greater that the previous value if we are fading
in, and less than the previous value if we are fading out – after each
repaint of the panel. An AlphaComposite object with the rule
SRC_OVER and the current alpha value is stored in the
composite member of the applet class. In this way we cycle the alpha for
the AlphaComposite object from 0.0f to 1.0f and back again.
The ImagePanel constructor loads the image as an
ImageIcon object – you will recall that the constructor takes
care of loading the image, and will not return until loading is complete.
The constructor then extracts the Image object from the
ImageIcon object. Since we want to be sure the image fits
comfortably in the space available to the applet, we call the
getScaledInstance() method for the Image object
to creates a new object that is scaled to the x and y dimensions supplied
as the first two arguments. The third argument to the getScaledInstance()
method is an integer that must be one of five constant values that are
defined in the Image class, and that select a particular
scaling algorithm:
SCALE_DEFAULT |
The default scaling algorithm |
SCALE_FAST |
A fast scaling algorithm |
SCALE_SMOOTH |
An algorithm designed for a smooth resultant image rather than speed |
SCALE_REPLICATE |
Use the algorithm defined by the ReplicateScaleFilter class. This algorithm duplicates pixels to scale up, or deletes pixels to scale down. |
SCALE_AREA_AVERAGING |
Use the algorithm defined by the AreaAveragingScaleFilter class. |
Scaling an image is achieved by applying an algorithm that calculates the
pixel values for the new image from those of the old. Some algorithms such
as that defined by the ReplicateScaleFilter are very simple,
and hence very fast to execute. Others such as the
SCALE_SMOOTH algorithm are more complex, and hence slower, but
produce a much better result.
The getScaledInstance() method returns immediately, even if
the scaled image has not yet been constructed, so we use a tracker to wait
for the image to complete.
The image is drawn in the paint() method for the
imagePanel object. The paint() method starts by
filling the panel in light gray to provide the background to the image. The
current composite object is then set in the graphics context and the alpha
for this determines the transparency of the image when we draw it. The
image is then drawn using the drawImage() method. The second
and third arguments are the coordinates of the top left corner of the
image. The last argument can be a reference to an
ImageObserver, but we just supply null as we have
ensured that the image is completely loaded in the ImagePanel
constructor.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.