-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePanel.java
More file actions
46 lines (37 loc) · 975 Bytes
/
ImagePanel.java
File metadata and controls
46 lines (37 loc) · 975 Bytes
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
/**
* ImagePanel.java
* Authors: Clara Smith and Cali Stenson
* Date: December 8, 2013
*
* MODIFIED VERSION OF various code found online
*
* ImagePanel creates a JPanel with an image as the background
*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public 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 setImage(String img) {
Image newImg = new ImageIcon(img).getImage();
this.img = newImg;
}
}