KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > ImageCanvas


1 package JSci.awt;
2
3 import java.awt.*;
4
5 /**
6 * The ImageCanvas class allows an image to be directly added to a container.
7 * @author Daniel Lemire
8 */

9 public class ImageCanvas extends Canvas {
10         protected Image image;
11
12         /**
13         * Constructs an image canvas.
14         */

15         public ImageCanvas(Image img) {
16                 image = img;
17                 waitForImage();
18         }
19         /**
20         * Paints the canvas.
21         */

22         public void paint(Graphics g) {
23                 g.drawImage(image, 0, 0, this);
24         }
25         private void waitForImage() {
26                 MediaTracker tracker = new MediaTracker(this);
27                 try {
28                         tracker.addImage(image, 0);
29                         tracker.waitForAll();
30                         tracker.waitForID(0);
31                         if (tracker.checkID(0))
32                                 repaint();
33                         else
34                                 System.err.println("Could not load the image.");
35                 } catch(InterruptedException JavaDoc e) {}
36         }
37 }
38
39
Popular Tags