-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagePanel.java
More file actions
executable file
·96 lines (85 loc) · 2.54 KB
/
imagePanel.java
File metadata and controls
executable file
·96 lines (85 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* imagePanel.java
*
* Created on June 6, 2006, 8:25 PM
*/
/**
* @author Chen-Fu Liao
* Sr. Systems Engineer
* ITS Institute, ITS Laboratory
* Center For Transportation Studies
* University of Minnesota
* 200 Transportation and Safety Building
* 511 Washington Ave. SE
* Minneapolis, MN 55455
*/
import java.awt.*;
import java.net.URL;
import java.io.*;
import java.awt.image.*;
public class imagePanel extends Panel
{
String imageFilename = "" ;
int imageW = 0, imageH=0 ;
Image img ;
// class construction
imagePanel(String name)
{
imageFilename = name ;
setBackground(new Color(121,217,157));
setSize(154, 150) ;
URL url = getImageResource(imageFilename);
img = Toolkit.getDefaultToolkit().getImage(url);
PixelGrabber pg=new PixelGrabber(img, 0,0,-1,-1,true);
try{
if(pg.grabPixels()){
imageW = pg.getWidth();
imageH = pg.getHeight();
}
} catch(InterruptedException ie){
System.out.println("Error: "+ie.toString()) ;
}
}
public void changeImagePanel(String imageFilename)
{
URL url = getImageResource(imageFilename);
img = Toolkit.getDefaultToolkit().getImage(url);
PixelGrabber pg=new PixelGrabber(img, 0,0,-1,-1,true);
try{
if(pg.grabPixels()){
imageW = pg.getWidth();
imageH = pg.getHeight();
}
} catch(InterruptedException ie){
System.out.println("Error: "+ie.toString()) ;
}
repaint() ;
}
public void paint(Graphics g)
{
Rectangle r = bounds();
g.drawImage(img, (r.width-imageW)/2, (r.height-imageH)/2, this) ;
// g.drawImage(img, (r.width-154)/2, (r.height-150)/2, this) ;
//System.out.println("w="+imageW+",H=" + imageH) ;
} // paint
public URL getImageResource(String img) {
URL url = null;
//try {
url = getClass().getResource(img+".png");
if (url==null) {
url = getClass().getResource(img+".PNG");
}
//} catch (IOException ioe) {
// System.out.println(url.toString());
//}
return url ;
} //getImageResource
public Dimension preferredSize()
{
return(new Dimension(154,150));
}
public boolean mouseDown(Event e, int x, int y)
{
return(true);
} //mouseDown
}