j ai essayer de faire un code qui affiche une image dans jpanel
mes je ni arrive pas pouvez vous m aidez a faire un code pour affichier une image dans un JPanel merci d'avance de votre
voici mon code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on 22 sept. 2010, 16:47:24
*/
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.*;
/**
*
* @author sofiene
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
panel = new panneau();
getContentPane().add(panel);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
panel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 334, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 418, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(103, 103, 103)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(54, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/*public void paintComponent (Graphics g){
ImageIcon imIc = new ImageIcon("C:/Users/sofiene/Desktop/Lighthouse.jpg");
Image im = imIc.getImage();
jPanel1.paintComponents(g);
g.drawImage(im, 10, 10, 10, 10, null);
}*/
class panneau extends JPanel {
ImageIcon image = new ImageIcon("C:/Users/sofiene/Pictures/kt_impact.gif");
public void paintComponent (Graphics g){
// ImageIcon imIc = new ImageIcon("C:/Users/sofiene/Desktop/Lighthouse.jpg");
// Image im = imIc.getImage();
panel.paintComponents(g);
g.drawImage(image.getImage(), 100, 100, 10, 10, panel);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJFrame frame = new NewJFrame();
panel.setVisible(true);
// jPanel1.paintComponents(Graphics JPanel);
// frame.paintComponents(jPanel1);
//JLabel image = new JLabel( new ImageIcon( "mon_image.jpg"));
//pane.setLayout(new BorderLayout, CENTER);
/* //frame.drawImage();
jPanel1.checkImage(im, frame);
jPanel1.setVisible(true);
*/
// jLabel1.setIcon(new ImageIcon("C:/Users/sofiene/Desktop/Lighthouse.jpg"));
frame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private static javax.swing.JLabel jLabel1;
private static javax.swing.JPanel panel;
// End of variables declaration
}
