KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > whiteboard > PicView


1 package demo.notification.whiteboard;
2
3
4
5 import java.io.File JavaDoc;
6
7 import java.awt.Canvas JavaDoc;
8
9 import java.awt.Toolkit JavaDoc;
10
11 import java.awt.MediaTracker JavaDoc;
12
13 import java.awt.Dimension JavaDoc;
14
15 import java.awt.Image JavaDoc;
16
17 import java.awt.Graphics JavaDoc;
18
19
20
21 // das im konstruktor u"bergebene bild laden
22

23 // und darstellen ...
24

25
26
27 public class PicView extends Canvas JavaDoc {
28
29     private Toolkit JavaDoc toolkit;
30
31     private MediaTracker JavaDoc mediaTracker;
32
33     private Image JavaDoc image;
34
35
36
37     public PicView(String JavaDoc filename) {
38
39     super();
40
41     toolkit = Toolkit.getDefaultToolkit();
42
43     mediaTracker = new MediaTracker JavaDoc(this);
44
45     image = loadImage(filename);
46
47     setSize(getDim() );
48
49     }
50
51
52
53     private Dimension JavaDoc getDim() {
54
55     Dimension JavaDoc d = new Dimension JavaDoc (0, 0);
56
57     if (image != null) {
58
59         d.width = image.getWidth (this);
60
61         d.height = image.getHeight (this);
62
63     }
64
65     return d;
66
67     }
68
69
70
71     Image JavaDoc loadImage(String JavaDoc filename) {
72
73     if ( !new File JavaDoc(filename).exists ()) {
74
75         System.out.println("File "+filename+" not Found !");
76
77         System.exit(1);
78
79     }
80
81
82
83     Image JavaDoc imageData = toolkit.getImage(filename);
84
85     mediaTracker.addImage(imageData,0);
86
87     
88
89     try {
90
91         mediaTracker.waitForID(0);
92
93     } catch (InterruptedException JavaDoc ie) {}
94
95
96
97     return imageData;
98
99     }
100
101         
102
103     public void paint(Graphics JavaDoc g) {
104
105     g.clearRect (0, 0, getDim().width, getDim().height);
106
107     g.drawImage(image,0,0,null);
108
109     }
110
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Popular Tags