Beginning Java 2- JDK 1.3 Version Images and Animation
In this chapter we will introduce some of the basic techniques for reading
images from a file and displaying them, and creating animation effects in your
applications and applets. In this chapter you will learn:
- How to read an image into an application or an applet
- How to display an image
- How to create animation effects
- How to draw on an image
- How to create an image internally to your program
- What alpha compositing is
- How to set the transparency of an image and create fading effects
We will use applets as the primary vehicle for working examples in this chapter,
but don't forget that everything you will learn here can also be applied to your
applications.
Applet Operations
We have already seen a few simple examples of applets, but we haven't really
gone into how they are structured in any detail. Since we will be writing
several rather more complicated applets in this chapter, we will remedy this
right now.
To recap, an applet is defined by a class that has the Applet class
as a base. We will use the Swing class JApplet which is derived
from Applet to define our applets because it provides more
capabilities. As you know, an applet is a program that is embedded in a Web
page, so it executes under the control of a Web browser, or the
appletviewer program that comes with the Java Development Kit. As we
discussed in Chapter 1, you are likely to need to have the Java plug-in
installed if Java 2 applets are to run with your browser. If you want to be sure
that your applet will run with Netscape or Microsoft browsers without the plug-
in being installed, then you must forgo the added capabilities of the
JApplet class and limit yourself to using the Applet
class as the base for your applet.
Whether an applet runs or not, and when it starts execution and when it stops,
are all controlled by the browser, not by code in the applet. Of course, whether
things work as they should still depends on the applet being implemented
properly. When a browser loads a Web page containing an applet, it will create
an object of the class defining the applet. It will then use four methods for
the applet class object to control the operation of the applet. All four methods
are public, have a void return type and require no arguments. These methods are:
| Method
|
Description
|
init()
|
To start your applet, the browser always calls the default constructor for
your applet class to create the object, and then calls its init()
method to do any initialization that is necessary. When init() is
called, the applet is loaded and any parameters specified for the applet are
available. You typically create any objects that are needed within the applet in
this method and initialize any data members of the applet class. You will also
implement the init() method to create any threads that the applet
requires. Of course, your applet class can still have data members that you
supply initial values for just like any other class. You can also implement the
default constructor and do the initialization of data members there, too.
|
start()
|
This method is called by the browser to start or restart the operation of the
applet. When the applet is started initially, the start() method
will be called after the init() method has executed. The
start() method is typically used to start any additional threads
required in the applet. You don't always have to implement this method, but we
will be using it a lot in this chapter to start animations.
|
stop()
|
This method is called when an applet should stop what it is doing temporarily –
when the applet is 'hidden', for instance – because the user has scrolled the
Web page so the applet region is no longer visible. You would typically stop any
continuously running operations that are in separate threads in this method –
ones that might display an animation, for instance. This avoids wasting
processor time on preparing images that can't be seen. To restart the applet the
browser will call the start() method.
|
destroy()
|
This is called when the applet is being terminated so you should use it to free
up any system resources that your applet uses. The stop() method is
always called prior to this method being called.
|
The default implementations of these methods are inherited in your applet class,
but they all do nothing. It is up to you to implement your versions of these
methods so that they do what is necessary in the context of your applet. One
other method that your applets should implement is getAppletInfo(). This method
is expected to return a String object that contains information
about the author, the version and copyright for the applet suitable for
displaying in an "About" dialog.
The browser finds out about your applet through the HTML code that appears in
the Web page, so let's take a quick look at the bits that are likely to be
involved.
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.
|