Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1412 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Swing, afficher une image en fond de jpanel (code inside)

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Swing, afficher une image en fond de jpanel (code inside)

n°730471
xiluoc
un pc pour les unirs ....
Posté le 18-05-2004 à 16:21:37  profilanswer
 

:hello: ,
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 :
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class test{
  4.     private static void  createAndShowGUI() {
  5.         JFrame frame = new JFrame("HelloWorldSwing" );
  6.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.        
  8.         //JLabel label = new JLabel("Hello World" );
  9.         //frame.getContentPane().add(label);
  10.        
  11.        
  12.         Image img = Toolkit.getDefaultToolkit().getImage("fond.jpg" );
  13.         DrawingPanel drawingPanel =  new DrawingPanel(img);
  14.         frame.getContentPane().add(drawingPanel);
  15.         frame.pack();
  16.         frame.setVisible(true);
  17.     }
  18.     public static void main(String[] args) {
  19.         //Schedule a job for the event-dispatching thread:
  20.         //creating and showing this application's GUI.
  21.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  22.             public void run() {
  23.                 createAndShowGUI();
  24.             }
  25.         });
  26.     }
  27.    
  28. }
  29. class DrawingPanel extends JPanel {
  30.     private Image img = null;
  31.    
  32.     DrawingPanel(Image image)
  33.     { img = image; }
  34.  
  35.     public void paintComponent(Graphics g) {
  36.         // First paint background unless you will
  37.         // paint whole area yourself.
  38.         super.paintComponent(g);
  39.        
  40.         // Use the image width & height to find the starting point
  41.         int msgX = getSize().width/2 - img.getWidth(this);
  42.         int msgY = getSize().height/2 - img.getHeight(this);
  43.        
  44.         //Draw image at centered in the middle of the panel
  45.         g.drawImage(img, msgX, msgY, this);
  46.     }
  47. }


    [:dams86]

mood
Publicité
Posté le 18-05-2004 à 16:21:37  profilanswer
 

n°730517
xiluoc
un pc pour les unirs ....
Posté le 18-05-2004 à 16:54:43  profilanswer
 

:/


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Swing, afficher une image en fond de jpanel (code inside)

 

Sujets relatifs
Question sur les JPanel [résolu] + question sur les listeners [résolu][php] arrive pas a afficher le contenu d'une table SQL [nb inside]
[c++/pascal] code de grey???[Swing][JprogressBar]Problémé d'affichage de Mon JprogressBar
[html] Un texte qui prend en compte la position de l'imageDessin et rafraichissement sous swing
Inserer un code ASCII dans une chaîne de caractèreImage en background d'un site
Cherche code source pour bench compilation 
Plus de sujets relatifs à : Swing, afficher une image en fond de jpanel (code inside)


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR