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

  FORUM HardWare.fr
  Programmation
  Java

  afficher une image dans un JPanel imbriqué

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

afficher une image dans un JPanel imbriqué

n°1302486
lordankou
Posté le 09-02-2006 à 21:00:15  profilanswer
 

Bonjour,
 
J'ai un problème d'affichage d'image dans deux JPanel imbriqués.
J'ai une class PanelAvecFond qui permet d'afficher une image dans le panel
Cette classe marche très bien sauf quand elle est imbriquée dans un autre panel de type PaneAvecFond auquel cas le panel imbriqué ne s'affichera pas (idem si on rajoute un JLabel dans celui ci).
Je pense que le problème doit venir d'un paint à refaire au moment ou j'inclue mon second panel mais je suis pas sur.
Voilà le code de ma classe :
 

Code :
  1. //////////
  2. // library
  3. //////////
  4. import javax.swing.JComponent; // JComponent library
  5. import javax.swing.ImageIcon; // ImageIcon library
  6. import java.awt.TexturePaint; // TexturePaint library  
  7. import java.awt.image.BufferedImage; // BufferedImage library  
  8. import java.awt.Graphics; // graphics library
  9. import java.awt.Image; // image library
  10. import java.awt.Toolkit; // toolkit library
  11. import java.awt.Rectangle; // rectangle library
  12. import java.awt.Graphics2D; // Graphics2D library
  13. import java.awt.Color; // color library
  14. /** declaration of a JComponent which can display a background picture
  15. * @see : JComponnent
  16. * @author :  
  17. */
  18. public class PanelAvecFond extends JComponent {
  19.     ///////////////////////
  20.     // Variable declaration
  21.     ///////////////////////
  22.     private TexturePaint maTexture; // texture to display  
  23.     private BufferedImage monBuffer; // Image Buffer
  24.     //////////////////////
  25.     // Methode declaration
  26.     //////////////////////
  27.     /** constructor of the JPanel with a background
  28.      * @param : file name of the background
  29.      * @return : void
  30.      */
  31.     public PanelAvecFond(String monNomDeFichier){
  32. this.monBuffer = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage(monNomDeFichier)); // get image from a file and "stock" it to a buffer
  33. this.maTexture = new TexturePaint(monBuffer,new Rectangle(0, 0, monBuffer.getWidth(), monBuffer.getHeight())); // create a texte from the image buffer created juste above
  34.     }
  35.     /** method to display the picture
  36.      * @param : component where we can draw
  37.      * @return : void
  38.      */
  39.     public void paintComponent(Graphics g){
  40. Graphics2D g2d = (Graphics2D)g; // create a 2D graphics  
  41. g2d.setPaint(maTexture); // applay texture to 2D graphics
  42. g2d.fillRect(0, 0, getWidth(), getHeight() ); // draw a rectangle which contain the picture
  43.     }
  44.    
  45.    
  46.     /** method to buffer an image
  47.      * @param : picture to buffer
  48.      * @return : Buffered picture corresponding to picture
  49.      */
  50.     private BufferedImage toBufferedImage(Image image){
  51. // create a new image  
  52. image = new ImageIcon(image).getImage();
  53. // create a buffer image from the picture
  54. BufferedImage monBuffer = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
  55. // create a new graphic
  56. Graphics monGraphique = monBuffer.createGraphics();
  57. // define a color (not really useful)
  58. monGraphique.setColor(Color.white);
  59. // draw a rectangle to store the picture
  60. monGraphique.fillRect(0, 0, image.getWidth(null),image.getHeight(null));
  61. // draw a image with graphics
  62. monGraphique.drawImage(image, 0, 0, null);
  63. monGraphique.dispose();
  64. // Return the image buffer
  65. return monBuffer;
  66.     }
  67.    
  68. }


 

mood
Publicité
Posté le 09-02-2006 à 21:00:15  profilanswer
 


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

  afficher une image dans un JPanel imbriqué

 

Sujets relatifs
Mettre une image en fondAfficher un message d'excuse quand le serveur est surchargé
pb comportement de <input type="image">Pb de compilation (traitement d'image)
Capture d'une image par webcam en C puis traitement de cette imageAfficher un point sur une carte selon ses coordonnées géographiques
Construction d'imageimage qui se repete à l'infini
Problème d'imageAfficher dans un arbre n aire
Plus de sujets relatifs à : afficher une image dans un JPanel imbriqué


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