The Graphics Object
(java.awt.Graphics)
Related Material From SUN Tutorials:
Drawing Methods
- drawLine
- drawRect
- fillRect
- etc...
Image Methods
Graphics Objects are also capable of displaying images:
drawImage (Image im, int x, int y, Color bgcolor, ImageObserver this)
getImage
drawImage
Notice that drawImage requires an Image Object and an ImageObserver
object. You can retrieve an image using getImage:
Image im = getImage(getDocumentBase(), "filename.gif");
Typically, a getImage is performed in the init() method, and
the image is displayed in the paint() method. There are six drawImage methods in the Graphics class
public void init() {
im = getImage(getDocumentBase(),"duke.gif"); //sometimes"./duke.gif"
}
public void paint(Graphics g) {
g.drawImage(im, x, y, Color.white, this);
}
Example:
// HelloWorld extended to draw an image
// Assumes existance of "graphics/duke.gif"
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import sun.awt.image.URLImageSource;
import java.applet.Applet;
public class DukeTest extends Applet implements MouseListener {
int MouseX=25, MouseY=25;
Image Duke;
public void init() {
Duke = getImage(getDocumentBase(),"graphics/duke.gif");
// assumes graphics directory is in same place as the html stuff
addMouseListener(this);
}
public void paint(Graphics g) {
g.drawImage(Duke, MouseX, MouseY, Color.white, this);
}
public void mousePressed(MouseEvent e) {
MouseX = e.getX();
MouseY = e.getY();
repaint();
}
public void mouseReleased(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
}
More about mouse events and MouseListeners later.
Note the this: who is observing me...loading this file...
Audio Clips
Java also has built-in methods to play audio clips. No need for the remote
computer to have an audio player; Java will do the playing. (The remote
computer does need audio-capable hardware though.)
Playing a clip
The easiest way to listen to an audio clip is through the play method:
play (URL soundDirectory, String soundfile);
or simply,
A common URL to use with the play() method is the directory where the
HTML files are located. You can access that location through the Applet method
getDocumentBase(): (this gets the absolute URL - of place of HTML
file that loaded my applet)
play (getDocumentBase(), "cuckoo.au");
For this to work, your class file and the cuckoo.au file must be in the same
directory.
// HelloWorld extended to play audio sounds
// assumes existance of "sounds/that.bark.au" file
import java.awt.Graphics;
import java.applet.Applet;
public class hwAudio extends Applet {
public void paint(Graphics g) {
g.drawString("Audio Test", 25, 25);
play(getCodeBase(), "../../sounds/bark.au");
}
}
Looping an Audio Clip
You can also treat audio clips like images. You can load them up and draw them
later.
To load an audio clip:
AudioClip sound;
sound = get AudioClip(getBaseDocument(), "./cuckoo.au") ; //?
Once a clip is loaded, you can use one of three methods with it: play, loop,
and stop.
To play the clip:
To start the clip playing and have it loop:
To stop a running clip:
Simple Looping Example:
import java.awt.Graphics;
import java.applet.*;
public class hwLoop extends Applet {
AudioClip sound;
public void init() {
sound=getAudioClip(getDocumentBase(), "../../sounds/bark.au");
}
public void paint(Graphics g) {
g.drawString("Audio Test", 25, 25);
}
public void start() {
sound.loop();
}
public void stop() {
sound.stop();
}
}
Mouse Input
One of the most useful features Java supports is direct interactivity. Your
application can pay attention to the mouse and react to changes to the mouse.
(Physical events trigger)
The most common event is the click. This event is governed by the two methods:
mousePressed() and mouseReleased(). Mouse methods are "heard" through MouseListeners so your code needs to implement the Listener. MouseListener is an interface in java.awt.event.
public void mousePressed(MouseEvent e) {
/* mouse down at e.getX(),e.getY() - so do something */ }
public void mousePressed(MouseEvent e) {
/* mouse up at e.getX(),e.getY() - so do something */ }
// HelloWorld extended to watch for the mouse
// the string is reprinted at the location of the mouse click
import java.awt.Graphics;
import java.awt.event.*;
import java.applet.Applet;
public class hwMouse extends Applet implements MouseListener { //note tha mouse is an applet
int MouseX=25, MouseY=25;
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
g.drawString("Hello World!", MouseX, MouseY);
}
public void mousePressed(MouseEvent e) {
MouseX = e.getX();
MouseY = e.getY();
repaint();
}
public void mouseReleased(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
}
}
- paint() doesn't clear first, repaint() does
- could have said this.repaint
- return true - I handled this event, so event
is handled and done
- if false, passes this to enclosing environment (on a
canvas, passes to window paint) ... events cascade up
Things to try:
Create an applet that will display the duke image wherever you
click the
mouse. Make the html wrapper , compile and test.
Modify this to add a sound whenever you click the mouse
Finally:
Common Applet Problems (and Their Solutions) This page also has links to
Common Component Problems, Common Layout Problems, and Common Graphics
Problems.