-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageDisplay.java
More file actions
104 lines (83 loc) · 2.11 KB
/
ImageDisplay.java
File metadata and controls
104 lines (83 loc) · 2.11 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
97
98
99
100
101
102
103
104
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
public class ImageDisplay extends JLabel
{
protected int xPos;
protected int yPos;
protected int w;
protected int h;
protected Boolean visible = true;
protected Image myimage;
private Image dbImage;
private Graphics dbg;
public void setImage( String fileName, int x, int y )
{
xPos = x;
yPos = y;
setOpaque(false);
myimage = readImage(fileName);
this.setSize( myimage.getWidth(this), myimage.getHeight(this) );
makeTheMove();
}
public Image readImage( String url )
{
try
{
return ImageIO.read(getClass().getResource(url));
}
catch (IOException ie) {
System.err.println( url + " not found!" );
return null;
}
}
public URL getURL( String name )
{
return this.getClass().getResource(name);
}
void setImageSize()
{
this.setSize( myimage.getWidth(this), myimage.getHeight(this) );
}
protected void makeTheMove()
{
visible = true;
this.setLocation( xPos, yPos );
repaint();
}
public int getX()
{
return xPos;
}
public int getY()
{
return yPos;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if ( visible )
g.drawImage( myimage, 0, 0, null);
}
public boolean isOptimizedDrawingEnabled()
{
return false;
}
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
}