Bonjour,
J'utilise ce code ci dessous pour insérer un jpanel qui a une image de fond dans un jpanel existant.
Le jpanel ne s'affiche pas dans l'application, et j'ai mis un sysout dans la redefinission du paintComponent de la classe JImagePanel et il ne s'affiche jamais.
N'y aurait il pas un problème avec les layout ?
Ne faut il pas spécifier des layout dans le nouveau jPanel qu'il créer ?
Je vous joint ci dessous le descriptif des layout de mon panel existant.
Et voici le code du layout
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String chemin = "/ressources/img/pingouins.jpg"; URL url = this.getClass().getResource(chemin); System.out.println(url.toString()); jLabel1.setText(url.toString()); // On met l'image sur le jLabel jLabel2.setIcon(new javax.swing.ImageIcon(url)); /** Accès au toolkit : */ java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); /** lecture de l'image : */ Image image = toolkit.getImage(url); //BufferedImage image = ImageIO.read( /* File, URL ou InputStream */ ); JImagePanel jp = new JImagePanel(image, 10, 0, true, true); jPanel1.add(jp); jPanel1.updateUI(); }
Ci quelqu'un a une idée qu'il n'hésite surtout pas car la je ne sais vraiment plus quoi faire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Loader Image"); jLabel1.setText("jLabel1"); jButton1.setText("Cliquez Ici"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Label")); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Panel")); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 144, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 135, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jButton1) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jButton1) .addGap(41, 41, 41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)) .addContainerGap()) ); pack(); }
La classe JImagePanel est la :Ou peut être avec une solution plus simple.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
105
106
107
108
109
110
111
112
113 * import javax.imageio.*; * import javax.swing.*; * import java.awt.*; * import java.io.*; * * public class JImagePanel extends JPanel { * * private static final long serialVersionUID = 2724980460740151616L; * * private Image _image; * private int _x; * private int _y; * private boolean _autoSize; * private boolean _keepAspect; * * public JImagePanel(File file, int x, int y, boolean autoSize, boolean keepAspect) throws IOException { * super(); * * setImage(ImageIO.read(file)); * setX(x); * setY(y); * setAutoSize(autoSize); * setKeepAspect(keepAspect); * } * * public JImagePanel(File file, int x, int y, boolean autoSize) throws IOException { * this(file, x, y, true, false); * } * * public JImagePanel(File file, int x, int y) throws IOException { * this(file, x, y, true); * } * * public JImagePanel(File file) throws IOException { * this(file, 0, 0, true); * } * * public JImagePanel(Image image, int x, int y, boolean autoSize, boolean keepAspect) { * super(); * * setImage(image); * setX(x); * setY(y); * setAutoSize(autoSize); * setKeepAspect(keepAspect); * } * * public JImagePanel(Image image, int x, int y, boolean autoSize) { * this(image, x, y, true, false); * } * * public JImagePanel(Image image, int x, int y) { * this(image, x, y, true); * } * * public JImagePanel(Image image) { * this(image, 0, 0, true); * } * * public JImagePanel() { * super(); * setX(0); * setY(0); * setImage(null); * setAutoSize(false); * } * * public void setImage(Image image) { * _image = image; * } * * public void setX(int x) { * _x = x; * } * * public void setY(int y) { * _y = y; * } * * public void setAutoSize(boolean autoSize) { * _autoSize = autoSize; * } * * public void setKeepAspect(boolean keepAspect) { * _keepAspect = keepAspect; * } * * public void paintComponent(Graphics g) { * super.paintComponent(g); * * if (_image != null) { * if (!_autoSize) { * g.drawImage(_image, _x, _y, _image.getWidth(null), _image.getHeight(null), null); * } * else { * Graphics2D g2d = (Graphics2D) g; * Double scaleWidth = new Double(getWidth()) / new Double(_image.getWidth(null)); * Double scaleHeight = new Double(getHeight()) / new Double(_image.getHeight(null)); * if (_keepAspect) { * if (scaleWidth > scaleHeight) { * scaleWidth = scaleHeight; * } * else { * scaleHeight = scaleWidth; * } * } * g2d.scale(scaleWidth, scaleHeight); * g2d.drawImage(_image, _x, _y, null); * } * } * } * * }
Je précise que je suis en JAVA 6
Merci encore.
Chaque problème a sa solution. S'il n'y a pas de solution c'est qu'il n'y a pas de problème.
Partager