-
Notifications
You must be signed in to change notification settings - Fork 0
/
JBackgroundPanel.java
52 lines (42 loc) · 1.41 KB
/
JBackgroundPanel.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
49
50
51
52
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class JBackgroundPanel extends JPanel {
private static final long serialVersionUID = 1L;
protected BufferedImage backgroundImage = null;
public JBackgroundPanel() {
super();
}
public JBackgroundPanel(BufferedImage backgroundImage) {
super();
setBackgroundImage(backgroundImage);
}
public JBackgroundPanel(URL url) throws IOException {
super();
setBackgroundImage(url);
}
public JBackgroundPanel(String resourceLocation) throws IOException {
super();
setBackgroundImage(resourceLocation);
}
public void setBackgroundImage(BufferedImage img) {
this.backgroundImage = img;
}
public void setBackgroundImage(URL url) throws IOException {
this.backgroundImage = ImageIO.read(url);
}
public void setBackgroundImage(String resourceLocation) throws IOException {
URL backgroundUrl = getClass().getResource(resourceLocation);
this.backgroundImage = ImageIO.read(backgroundUrl);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), null);
}
}
}