-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJImagePanel.java
More file actions
80 lines (70 loc) · 1.93 KB
/
JImagePanel.java
File metadata and controls
80 lines (70 loc) · 1.93 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
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Charger et afficher une image dans un panel
* @author fobec 2010
*/
public class JImagePanel extends JPanel {
private Image image = null;
private Boolean stretch = true;
/**
* Constructeur
* @param image image à afficher
*/
public JImagePanel(Image image) {
this.image = image;
}
/**
* Constructeur
* @param file nom du fichier
*/
public JImagePanel(String file) {
this.image = getToolkit().getImage(file);
}
/**
* Position de l'image sur le panel
* @param stretch true: etirer l'image / false: centrer l'image
*/
public void setStretch(Boolean stretch) {
this.stretch = stretch;
}
/**
* Surcharger le dessin du composant
* @param g canvas
*/
protected void paintComponent(Graphics g) {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
if (this.stretch) {
width = this.getWidth();
height = this.getHeight();
} else {
width = this.image.getWidth(this);
height = this.image.getHeight(this);
x=((this.getWidth()-width)/2);
y=((this.getHeight()-height)/2);
}
g.drawImage(this.image, x, y, width, height, this);
}
/**
* Exemple : jPanelImage dans un JFrame
* @param args
*/
public static void main(String[] args) {
JImagePanel imagePanel = new JImagePanel("./ressource/Logo.png");
//Centrer l'image
imagePanel.setStretch(false);
//Etirer l'image
// imagePanel.setStretch(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(imagePanel);
frame.setSize(400, 400);
frame.setLocation(200, 200);
frame.setVisible(true);
}
}