-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImagePanel.java
48 lines (42 loc) · 1.11 KB
/
ImagePanel.java
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
// Import Statements
import javax.swing.*;
import java.awt.*;
/**
* This creates the ImagePanel for our Game
*
* This was taken from an Internet Resource.
*
* LINK: http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
*/
class ImagePanel extends JPanel
{
private Image img;
public ImagePanel(String img)
{
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g)
{
g.drawImage(img, 0, 0, null);
}
public void update (Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
}