Back Contents Next

Summary

This chapter has been a basic introduction to handling images and producing animations. Java provides much greater capabilities in this area than we have looked at, but with what you now know, you should not find it too difficult to explore those additional facilities yourself.

 

The important points that we covered in this chapter were:

 

q        Implementing an applet generally involves implementing four methods that are called by the browser or the context in which the applet is running – the init() method that initializes the applet, the start() method that is called to start the applet, the stop() method that is called to stop() whatever the applet is doing and the destroy() method that is called when the applet ends. You should also implement the getAppletInfo() and getParameterInfo() methods for your applet.

q        Objects of the URL class represent uniform resource locators on the Internet. You can define a URL in an applet either as an absolute URL, or relative to the original URL.

q        An Image object represents an image, and you can create images from files in GIF, PNG,or JPEG format.

q        The control of the timing interval for an animation generally runs in a separate thread, and in an applet the thread is usually started in the start() method and stopped in the stop() method.

q        You can use an object of the java.util.Timer class to control an animation that you expedite through a TimerTask object. You can apply Timer and TimerTask object to scheduling any operation that recurs at a fixed time interval.

q        The alpha component for a pixel specifies its transparency, where an alpha value of zero is transparent and a value of one is opaque.

q        Alpha compositing determines how the components of the pixels for a source image are combined with the components of the destination image over which it is rendered.

q        Rendering hints provide a way for you to trade between speed and quality in rendering operations.

q        The type of line produced by drawing operations in a graphics context is determined by the current stroke set in the context. A line type is specified by a BasicStroke object.

q        The glass pane for a JApplet or JFrame object overlays the content pane, so you can draw animated elements of an image in the glass pane and reserve the content pane for the fixed elements. You must ensure the background to the glass pane is transparent to enable the content pane to be seen.

q        You can synthesize images by creating a BufferedImage object and drawing on it using the Graphics2D object that represents the image.

Exercises

1.       Create an applet that will create an image from a GIF file and display four copies of it in two rows of two.

2.       Modify the result of the previous exercise so that the applet obtains the URL for the image file from a PARAM.

3.       Modify the last example to use the glass pane to display the sprite, and arrange that the logo is only visible inside the circle that the sprite rolls around. (Hint: draw an image on top of the logo with a transparent circle.)

4.       Adapt the last example of the chapter to simultaneously display a fading logo and the rotating sprite in two threads.

5.       The animation in the last example in the chapter could be generated by a fixed sequence of images that repeats cyclically. Generate the animation by creating all the images you need beforehand and making the paint() method in the ImagePanel class display the appropriate image.

6.       Modify Sketcher to support lines of various types and widths – at least dashed and dotted lines in addition to solid lines.

7.       Create an applet to create a sequence of images from an arbitrary number of GIF files, and replay them as an animation at an interval specified by a <PARAM> tag. (Hint: you can give your GIF files names such as image0.gif, image1.gif, image3.gif and so on. The getImage() method that your applet class inherits returns null if the URL cannot be found.)


addImage() (MediaTracker), 15

AffineTransform class, 21

at member, 22

alpha channels, 24, 33

alpha components, 24

alpha compositing rules, 24

alpha composting, 24

AlphaComposite class, 24

getInstance(), 24

animation, 13

applets, 14

Applet class, 1

control methods, 1

getAppletInfo(), 2

getCodeBase(), 5

getDocumentBase(), 6

getImage(), 10

getParameter(), 2

getParameterInfo(), 3

applets, 1, 9

animated, 14

HTML tags, 2

appletviewer program, 1

BasicStroke class, 35

constructors, 36

browser, 1

BufferedImage class, 30

checkAll() (MediaTracker), 16

classes

AffineTransform, 21

AlphaComposite, 24

Applet, 1

BasicStroke, 35

BufferedImage, 30

ColorModel, 30

Image, 4

ImageIcon, 3

JApplet, 1

MediaTracker, 15

RenderingHints, 33

URL, 5

ColorModel class, 30

constructors

BasicStroke(), 36

ImageIcon(), 3, 4, 9, 10

MediaTracker(), 15

URL(), 5

control methods

Applet class, 1

currentTimeMillis() (System), 17

data sources

network, 5

drawImage() (Graphics), 8, 10

arguments, 9

drawImage() (Graphics2D)

arguments, 19

equals() (URL), 6

fillRect() (Graphics2D), 20

FTP (File Transfer Protocol), 5

getAppletInfo() (Applet), 2

getCodeBase() (Applet), 5

getDocumentBase() (Applet), 6

getHeight() (Image), 10

getIconHeight() (ImageIcon), 9

getIconWidth() (ImageIcon), 9

getImage() (Applet), 10

getInstance() (AlphaComposite), 24

getParameter() (Applet), 2

getParameterInfo() (Applet), 3

getPort() (URL), 6

getWidth() (Image), 10

GIF (Graphics Interchange Format), 4

compared with JPEG, 4

Graphics class

drawImage(), 8, 10

arguments, 9

Graphics2D class

drawImage()

arguments, 19

fillRect(), 20

rotate(), 40

setRenderingHint(), 33

arguments, 33

HTML files, 8

HTML tags, 2

HTTP (HyperText Transfer Protocol), 5

Image class, 4

creating Image objects, 4, 10

getHeight(), 10

getWidth(), 10

ImageIcon class, 3

constructors, 3, 4, 9, 10

getIconHeight(), 9

getIconWidth(), 9

ImageIcon objects, 4

ImageObserver interface

imageUpdate(), 10

implementation, 11

parameters, 11

ImageObserver objects, 9

images, 10

circles, 34

creating an image, 31

displaying an image, 8

drawing an image, 34

fading an image, 28

GIF, 4

JPEG, 4

lines, 34

obtaining an image, 3

spinning an image, 20

synthesizing an image, 30

transforming an image, 20

transparency, 24

imageUpdate() (ImageObserver), 10, 11

parameters, 11

interfaces

Stroke, 35

isErrorAny() (MediaTracker), 16

isErrorId() (MediaTracker), 16

JApplet class, 1, 9

resize(), 9

JPanel class

JPanel objects, 20

JPEG (Joint Photographic Experts Group), 4

compared with GIF, 4

MediaTracker class, 15

addImage(), 15

checkAll(), 16

constructor, 15

isErrorAny(), 16

isErrorId(), 16

waitForAll(), 16

waitForID(), 15

methods

addImage() (MediaTracker), 15

checkAll() (MediaTracker), 16

control methods

Applet class, 1

currentTimeMillis() (System), 17

drawImage() (Graphics), 8, 10

arguments, 9

drawImage() (Graphics2D)

arguments, 19

equals() (URL), 6

fillRect() (Graphics2D), 20

getAppletInfo() (Applet), 2

getCodeBase() (Applet), 5

getDocumentBase() (Applet), 6

getHeight() (Image), 10

getIconHeight() (ImageIcon), 9

getIconWidth() (ImageIcon), 9

getImage() (Applet), 10

getInstance() (AlphaComposite), 24

getParameter() (Applet), 2

getParameterInfo() (Applet), 3

getPort() (URL), 6

getWidth() (Image), 10

imageUpdate() (ImageObserver), 10, 11

parameters, 11

isErrorAny() (MediaTracker), 16

isErrorId() (MediaTracker), 16

openStream() (URL), 6, 7

resize() (JApplet), 9

rotate() (Graphics2D), 40

setRenderingHint() (Graphics2D), 33

arguments, 33

waitForAll() (MediaTracker), 16

network

data sources, 5

objects

BasicStroke, 35

Image

creating Image objects, 4, 10

ImageIcon, 4

ImageObserver, 9

JPanel, 20

URL

defining a URL object, 5

openStream() (URL), 6, 7

pixels

alpha components, 24

rendering hints, 33

RenderingHints class, 33

resize() (JApplet), 9

rotate() (Graphics2D), 40

setRenderingHint() (Graphics2D), 33

arguments, 33

sprites, 31

Stroke interface, 35

System class

currentTimeMillis(), 17

time

measuring time intervals, 17

URL class, 5

constructors, 5

defining a URL object, 5

equals(), 6

getPort(), 6

openStream(), 6, 7

URLs (Uniform Resource Locators), 2

reading a URL, 5

waitForAll() (MediaTracker), 16

waitForID() (MediaTracker), 15


Back Contents Next
©1999 Wrox Press Limited, US and UK.