xiluoc un pc pour les unirs .... | ,
j ai pourtant suivit les conseils des post provenant du forum de sun mais sa marche toujours pas .. l image ne saffiche pas. parcontre un jlabel ou une imgaIcon dans un JLabel oui.
Code :
- import javax.swing.*;
- import java.awt.*;
- public class test{
- private static void createAndShowGUI() {
- JFrame frame = new JFrame("HelloWorldSwing" );
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- //JLabel label = new JLabel("Hello World" );
- //frame.getContentPane().add(label);
-
-
- Image img = Toolkit.getDefaultToolkit().getImage("fond.jpg" );
- DrawingPanel drawingPanel = new DrawingPanel(img);
- frame.getContentPane().add(drawingPanel);
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- //Schedule a job for the event-dispatching thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
-
- }
- class DrawingPanel extends JPanel {
- private Image img = null;
-
- DrawingPanel(Image image)
- { img = image; }
-
- public void paintComponent(Graphics g) {
- // First paint background unless you will
- // paint whole area yourself.
- super.paintComponent(g);
-
- // Use the image width & height to find the starting point
- int msgX = getSize().width/2 - img.getWidth(this);
- int msgY = getSize().height/2 - img.getHeight(this);
-
- //Draw image at centered in the middle of the panel
- g.drawImage(img, msgX, msgY, this);
- }
- }
|
|