Drawing ImagesDeliver Image for applets and applications Use MediaTracker to insure complete delivery Use offScreenImage or double buffering Place image with the mouse click Instructor Jeff Zhuk |
// ImagePlacer
import java.applet.*;
import java.awt.*;
/**
* ImagePlacer class uses a simple double buffering technique and event handling
* There is also deliverImage() method providing image delivery for applets
* and applications with MediaTracker class
* To transform this applet into an application just change class inheritance
public class ImagePlacer extends Frame
* and run it from a command line: java ImagePlacer
* @author Jeff Zhuk
*/
public class ImagePlacer extends Applet
{
public static String imageFile = "jar.gif";
private Image image, offScreenImage = null;
private Graphics offScreenGraphics = null;
private int w=0, h=0; // image size
private int last_x = 30;
private int last_y = 30;
/**
* init() method is called by JVM of Web browser when applet is loaded
*/
public void init() {
image = deliverImage(this, imageFile);
}
/**
* deliver an image based on a string with URL
* @param iComponent to which image should be delivered
* @param iString fileName
* @return image or null
* @@ author Jeff Zhuk
**/
public static Image deliverImage(Component iComponent, String iName) {
MediaTracker tracker = new MediaTracker(iComponent);
Image image = null;
try {
if(iComponent instanceof Applet) {
Applet applet = (Applet)iComponent;
image = applet.getImage(applet.getCodeBase(), iName);
} else {
image = Toolkit.getDefaultToolkit().getImage(iName);
}
tracker.addImage(image,0);
tracker.waitForID(0);
} catch (Exception e) {
System.out.print("deliverImage failure: " + e);
return null;
}
if(tracker.isErrorID(0)) {
System.out.print("deliverImage failure for " + iName);
return null;
}
return image;
}
/**
* Overriding update() method we make painting faster.
* Regular update() method clears screen before painting
* Our version skips clearing the screen and directly calls paint()
* Reminder: update() method is called by JVM to redraw the window
*
* @param g graphics to draw on
**/
public void update(Graphics g)
{
if(image == null)
return; // to avoid NullPointerException
if(offScreenImage == null) { // create offScreenImage for future use
w = image.getWidth(this);
h = image.getHeight(this);
offScreenImage = this.createImage(w, h);
offScreenGraphics = offScreenImage.getGraphics();
offScreenGraphics.drawImage(image, 0, 0, null);
}
paint(g);
}
/**
* Drawing happens here
*
* @param g graphics to draw on
**/
public void paint(Graphics g)
{
g.drawString("Click below to place a picture...", 30,30);
if(offScreenImage != null)
g.drawImage(offScreenImage, last_x, last_y, this);
}
/**
* Event handler of JDK1.0 called by JVM when MOUSE_UP event happens
* It is used to draw image at mouse click location
* Writing in JDK1.1 you'll need this.addMouseListener(new MouseListener) { ...
*
* @param e event structure
* @param coordinate x
* @param coordinate y
**/
public boolean mouseUp(Event e, int x, int y) // JDK 1.0
{
// clean previous picture
Graphics g = this.getGraphics();
g.setColor(this.getBackground()); // fill rect with bgcolor
g.fillRect(last_x, last_y, w, h);
last_x=x; // get a new mouse position
last_y=y;
repaint();
return true;
}
}
Back To AWT basics