Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


CHAPTER 39
Graphics and Animation

by Mike Morgan

In this chapter
Displaying Graphics 1078
Displaying Graphics Primitives 1079
Displaying Colors 1091
Displaying Text 1094
Displaying Images 1101
Adding Animation 1104
Using the 2D API 1111
Java Resources on the Web 1118

Displaying Graphics

Colorful graphics and animation can change a dull, static, and gray Web page into an exciting and interesting place to visit. Java provides a wide range of tools for building and displaying graphics. These tools are found in Swing (package javax.swing*), the Abstract Windowing Toolkit (AWT) package (java.awt), and especially in the Graphics2D class (java.awt. Graphics2D). Most of what you need is part of the AWT. In fact, the majority of Java’s graphics methods are contained in the Graphics class.

• For more information on the AWT, see “Using the Abstract Windowing Toolkit,” p. 1027.

• For more information on Swing, see “The Swing Architecture,” p. 1052 and “Swing Component APIs,” p. 1055.

Using Java’s Graphics Class

Java’s Graphics class provides methods for manipulating a number of graphics features, including the following:

  Drawing graphics primitives
  Displaying colors
  Displaying text
  Displaying images
  Creating flicker-free animation

The following sections discuss all these graphics features and show how to implement them in Java applets. Along the way, you will acquire a complete understanding of the Graphics class and its methods.

You can find Java’s Graphics class in the java.awt (the Java Abstract Window Toolkit) package. Be sure to properly import the Graphics class when you use it in your code. Include the following line at the beginning of your file:

import java.awt.Graphics;


NOTE:  The Graphics class is a nice class with plenty of capabilities. To graphics professionals, however, Graphics seems a bit anemic. More mature GUI platforms, such as Windows 95 and the Macintosh, support features that are not present in Graphics.

If you need to go beyond Graphics, take a look at java.awt.Graphics2D, a richer, two-dimensional graphics class. It’s described later in this chapter, in the section titled “Using the 2D API.”


Using Java’s Coordinate System

You display the various graphics you produce—lines, rectangles, images, and so on—at specific locations in an applet or application window. A simple Cartesian (x, y) coordinate system defines each location within a Java window, as shown in Figure 39.1. The upper-left corner of a window is its origin (0, 0). x increases by the number of screen pixels that you move to the right of the left edge of an applet’s window. The number of pixels you move down from the top of a window is y.


FIGURE 39.1  Java’s graphics coordinate system increases from left to right and from top to bottom.

Displaying Graphics Primitives

The Java Graphics class provides you with methods that make it easy to draw two-dimensional graphics primitives. You can draw any two-dimensional graphics primitive, including

  Lines
  Rectangles
  Ovals
  Arcs
  Polygons

The following sections explain how to draw these graphics primitives.

Drawing Lines

Perhaps the simplest graphics primitive is a line. The Java Graphics class provides a single drawLine() method for drawing lines. The complete definition of the drawLine() method is:

public abstract void drawLine(int  x1, int  y1, int  x2,  int  y2)

The drawLine() method takes two pairs of coordinates—x1, y1 and x2, y2—and draws a line between them.

The applet in Listing 39.1 uses the drawLine() method to draw some lines. Figure 39.2 shows the output from this applet.

Listing 39.1 DrawLines.javaUse Graphics Primitives to Display Two Lines


import java.awt.Graphics;

public class DrawLines extends java.applet.Applet
{
   public void paint(Graphics g)
   {
     g.drawLine(0, 0, 400, 200);
     g.drawLine(20, 170, 450, 270);
   }
}


FIGURE 39.2  This applet displays two lines drawn using the drawLine() method.

Drawing Rectangles

The Java Graphics class provides six methods for drawing rectangles: drawRect(), fillRect(), drawRoundRect(), fillRoundRect(), draw3DRect(), and fill3DRect(). You can use these methods to draw and fill three designs of rectangles.

To draw a simple rectangle using the drawRect() method, use this definition:

public void drawRect(int  x, int  y, int  width, int  height)

Pass the x and y applet window coordinates of the rectangle’s upper-left corner along with the rectangle’s width and height to the drawRect() method. Assume, for example, that you want to draw a rectangle that is 300 pixels wide (width = 300) and 170 pixels high (height = 170). You also want to place the rectangle with its upper-left corner 150 pixels to the right of the left edge of the applet’s window (x = 150) and 100 pixels down from the window’s top edge (y = 100). You would write

g.drawRect(150, 100, 300, 170);


NOTE:  The preceding drawRect() call assumes that you already have a Graphics object g. See Listing 39.2 for an example of where such an object might come from.

To build an applet named OneRectangle that uses the Graphics class drawRect() method, you might write code similar to that shown in Listing 39.2. Figure 39.3 shows the applet’s output.

Listing 39.2 OneRectangle.javaDisplay a Simple Rectangle


import java.awt.Graphics;

public class OneRectangle extends java.applet.Applet
{
   public void paint(Graphics g)
   {
      g.drawRect(150, 100, 300, 170);
   }
}


FIGURE 39.3  The rectangle displayed by this applet was built with the drawRect() method.

Use the fillRect() method if you want to draw a solid rectangle. The complete definition of the fillRect() method follows:

public abstract void fillRect(int  x, int  y, int  width,  int  height)


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.