Lecture 8: Graphics and Applets

What You Will Learn Today

  1. Compare the structure and use of Java applets and applications.
  2. Create a Java applet and embed in a simple HTML document.
  3. Describe graphical coordinates in Java and perform coordinate transformations.
  4. Use two-dimensional graphic components, including shapes, colors and fonts.
  5. Use applet methods, URLs, images, sounds and parameters.

Applets

Creating Applets

Example Applet

HelloWorldApplet.java

import java.awt.Graphics;
import java.applet.Applet;
public class HelloWorldApplet extends Applet {
   public void paint(Graphics g) {
      g.drawString("Hello world!", 5, 25);
   }
}

Creating an HTML File with an Applet Tag

Example HTML File

HelloWorld.html

<html>
  <head> <title>Applet: Hello World</title> </head>
  <body>
    <p>My Java applet says:
    <applet code="HelloWorldApplet.class" width="150" height="25"> </applet>
  </body>
</html>

Graphics Coordinates

Coordinate Transformations

Methods for Drawing Shapes and Other Graphics

Return type, method, parameters Description
void drawRect(int x, int y, int width, int height) paint a rectangle with upper left corner (x, y) and dimensions (width, height)
void drawOval(int x, int y, int width, int height) paint an oval bounded by the rectangle (x, y, width, height)
void drawArc(int x, int y, int width, int height,
   int startAngle, int arcAngle)
paint an arc along the oval bounded by the rectangle (x, y, width, height)
void drawPolygon(Polygon p) paint a polygon p
void fillRect(int x, int y, int width, int height)
void fillOval(int x, int y, int width, int height)
void fillArc(int x, int y, int width, int height,
   int startAngle, int arcAngle)
void fillPolygon(Polygon p)
same as above but filled with the current foreground color
void drawLine(int x1, int y1, int x2, int y2) paint a line from (x1, y1) to (x2, y2)
void drawString(String str, int x, int y) paint the character string str starting at point (x, y)

Polygons

Return type, method, parameters Description
Polygon(int[] xpoints, int[] ypoints, int n) construct a polygon p with n points. xpoints and ypoints are arrays of n integers.
void translate(int dx, int dy) translate the polygon vertices by dx horizontally and dy vertically
Rectangle getBounds() return the bounding rectangle of the polygon
boolean contains(int x, int y) return true if the polygon contains

Java Colors

Some Predefined Colors

RGB Color red green blue cyan magenta yellow black darkGray gray lightGray white
Int value 255,0,0 0,255,0 0,0,255 0,255,255 255,0,255 255,255,0 0,0,0 64,64,64 128,128,128 192,192,192 255,255,255
Float value 1.0,0.0,0.0 0.0,1.0,0.0 0.0,0.0,1.0 0.0,1.0,1.0 1.0,0.0,1.0 1.0,1.0,0.0 0.0,0.0,0.0 .25,.25,.25 0.5,0.5,0.5 .75,.75,.75 1.0,1.0,1.0

Methods for Handling Colors

Return type, method, parameters Description
void setColor(Color color) set the foreground color
void setBackground(Color color) set the background color
Color getColor() return the foreground color
Color getBackground() return the background color

Fonts

Font f = new Font("TimesRoman", Font.BOLD, 36); // creates a new Font object
g.setFont(f);
// sets the current font to f
g.drawString("Hello, World!", 50, 25);
// displays a string in the current font at the base point (50, 25)

Methods for Beginning and Ending Applets

Method Description When called Browser event
void init() initializes the applet just after the applet is loaded the browser first views the applet
void start() starts the applet after the applet is made active the user returns to the page
void stop() stops the applet after the applet is made inactive the user goes to a different web page
void destroy() destroys the applet just before the applet is closed the user exits the browser

Methods for Handling URLs, Images and Sounds

Return type, method, parameters Description
URL getCodeBase() return the URL of the HTML file containing the applet tag
URL getDocumentBase() return the URL of the directory containing the applet
Image getImage(URL url, String name) retrieves an image from the specified URL and file name
void drawImage(Image img, int x, int y, this) paints the image img at position (x, y)
AudioClip getAudioClip(URL url, String name) retrieves an audio clip from the specified URL and file name
void play(URL url, String name) plays an audio clip at the specified URL and file name

Applet Parameters

Advanced Topics

To Do After Class