KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JImageCanvas


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

10 public class JImageCanvas extends JComponent {
11         protected Image image;
12
13         /**
14         * Constructs an image canvas.
15         */

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

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