Salut a tous,
J aimerais savoir comment faire pour afficher dans un JPanel une image ainsi qu une couleur de fond(background) en meme temps ?
merci
Version imprimable
Salut a tous,
J aimerais savoir comment faire pour afficher dans un JPanel une image ainsi qu une couleur de fond(background) en meme temps ?
merci
Une solution en Swing sans surcharger le JPanel :
Sinon tu peux utiliser le JXImagePanel du projet SwingX (https://swinglabs.dev.java.net) ou surcharger la méthode paintComponent(Graphics g) :Code:
1
2
3 JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(new Color(127, 127, 127)); panel.add(new JLabel(new ImageIcon(getClass().getResource('chemin/image.png'))));
Je fais ça de tête donc il se peut que cela ne marche pas/compile pas mais tu as l'idée générale et la majeure partie du code. Cette solution permet d'ajouter des composants au JPanel alors que la première ne le permet pas. Tu peux mélanger les deux grâce à mon StackLayout :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class MyPanel extends JPanel { protected BufferedImage image = null; public MyPanel(Color background, String imagePath) { setBackground(background); try { image = ImageIO.read(getClass().getResource(imagePath)); } catch (IOException ioe) { } catch (IllegalArgumentException iae) { } } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(getBackground()); g2.fill(g.getClip()); if (image != null) { int x = (getWidth() - image.getWidth()) / 2; int y = (getHeight() - image.getHeight()) / 2; g2.drawImage(image, x, y, null); } } }
Tu peux ensuite utiliser panel.add(unComposant, StackLayout.TOP).[/code]Code:
1
2
3 Panel panel = new JPanel(new StackLayout()); panel.setBackground(new Color(127, 127, 127)); panel.add(new JLabel(new ImageIcon(getClass().getResource('chemin/image.png'))), StackLayout.BOTTOM);
Salut, merci de ta réponse
J ai essayer de faire en surchargeant la methode paintcomponent comme dans ta deuxieme methode mais je comprends pas cette partie du code
ca passe bien a la compil mais ca me fait pas de couleur de background sur mes JPanels, pourtant j ai prealablement definit sur les JPanels un background :Code:
1
2 g2.setColor(getBackground());
bien sur cela ne marché pas a partir du moment ou je rajoute un composants par dessus(image)Code:
1
2 pic1.setBackground(Color.blue);
comment faire alors ?
merci
cette methode n est pas reconnue aussi ^^Code:
1
2 g2.fill(g.getClip());
enfin bref , si tu pouvais me detailler un peu ce que fait chaque methode dans ce paintcomponent , j t en serait reconnaissant :D , j suis pas mal debutant en java.
merci
Le programme suivant fonctionne parfaitement :
Code:
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 import java.awt.*; import java.awt.image.*; import javax.swing.*; import javax.imageio.*; import java.io.*; public class MyPanel extends JPanel { protected BufferedImage image = null; public MyPanel(Color background, String imagePath) { setBackground(background); try { image = ImageIO.read(getClass().getResource(imagePath)); } catch (IOException ioe) { } catch (IllegalArgumentException iae) { } } protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(getBackground()); g2.fill(g.getClip()); if (image != null) { int x = (getWidth() - image.getWidth()) / 2; int y = (getHeight() - image.getHeight()) / 2; g2.drawImage(image, x, y, null); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("Image Panel"); MyPanel panel = new MyPanel(Color.blue, "Death Valley 002.jpg"); f.setContentPane(panel); f.pack(); f.setVisible(true); } }); } }