Problème d'affichage image Swing
Bonjour,
Je n'arrive pas a comprendre la logique derriere swing, je vous balance les 2 classes de mon code que j'ai essayé de remanier, j'ai essayer imageicon mais j'ai des probleme avec les layout null .... comment afficher pour chaque Jpanel une image de fond, ça devrait pas etre si compliqué ... Désolé mais je suis dessus depuis 1 jours et demi, et si y a une logique derriere je ne l'ai pas compris.
Voici mon code :
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 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
| package design;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
static JFrame mClient;
static JPanel mMainPanel;
static JPanel mMenuPanel;
static ImagePanel mImageFond;
static ImagePanel mImageFondMenu;
public static void main(String[] args) {
mClient = new JFrame();
mImageFond = new ImagePanel();
mMainPanel = new JPanel();
mClient.setLayout(null);
mMainPanel.setLayout(null);
loadImage();
initializeParam();
initializeMainPanel();
initializeMenuPanel();
initializeConnexionPanel();
mClient.setVisible(true);
}
public static void initializeParam() {
mClient.setSize(1440, 860);
mClient.setLocationRelativeTo(null);
mClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void initializeMainPanel() {
mMainPanel.add(mImageFond);
mClient.setContentPane(mMainPanel);
}
public static void initializeMenuPanel() {
mMenuPanel = new JPanel();
mMenuPanel.setLayout(null);
mMenuPanel.setBounds(15, 15, 1395, 80);
mMenuPanel.setBackground(Color.white);
mClient.add(mMenuPanel);
}
public static void initializeConnexionPanel() {
mMenuPanel = new JPanel();
mMenuPanel.setLayout(null);
mMenuPanel.setBounds(400, 350, 600, 300);
mMenuPanel.setBackground(Color.white);
mClient.add(mMenuPanel);
}
public static void loadImage() {
BufferedImage img = null;
try {
img = ImageIO
.read(new File("ressource/client/background/fond.png"));
} catch (IOException e) {
}
mImageFond.setImage(img);
}
} |
et pour ma classe ImagePanel
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
| package design;
import java.awt.Graphics;
import java.awt.Image;
import java.io.Serializable;
import javax.swing.JPanel;
public class ImagePanel extends JPanel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Image image = null;
public ImagePanel(Image image) {
this.image = image;
}
public ImagePanel() {
}
public void setImage(Image image){
this.image = image;
}
public Image getImage(Image image){
return image;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int height = this.getSize().height;
int width = this.getSize().width;
g.drawImage(image,0,0, width, height, this);
}
}
} |
Si vous avez une solution qui marche, je suis preneur, et pitié, ne me renvoyez pas a la doc.