2016-02-13 3 views
0

J'ai deux images buffered à partir d'une liste (this.docList). Dans ma méthode paintComponent, je les dessine sur le JPanel et les dessine en même temps sur 'this.createdImage.Comment copier le contenu de JPanel dans BufferedImage

@Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics g2 = this.createdImage.getGraphics(); 
    if (controlWhichImage == 1){ 
     for(BufferedImage eachImage : docList){ 
      g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,null); 
      g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,null); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
      } 

     if (intx >= this.getWidth() || inty >= this.getHeight()){ 
      inty = 0; 
     } 

Mon problème est que lorsque j'utilise createImage, tout ce que je reçois est un panneau vide.

if (controlWhichImage == 2){ 
    g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),null); 
} 
+2

Ne pas effectuer cher opérations comme la création d'un BufferedImage à l'intérieur d'une méthode de peinture. paintComponent (et toutes les méthodes de peinture) peuvent être appelées plusieurs fois par seconde! – VGR

+0

Dupliquer de [* Copier le contenu d'un JPanel sur un BufferedImage *] (http://stackoverflow.com/q/35363892/230513). – trashgod

Répondre

2

Chaque fois paintComponent est appelé, vous créez une nouvelle instance de BufferedImage, si controlWhichImage est 1 il peint les images à la BufferedImage, si elle est 2 il peint, bien rien.

Fondamentalement, votre code est essentiellement fonctionne comme ...

Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB); 
    Graphics g2 = this.createdImage.getGraphics(); 
    if (controlWhichImage == 1){ 
     for(BufferedImage eachImage : docList){ 
      g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,null); 
      g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,null); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
     } 

     if (intx >= this.getWidth() || inty >= this.getHeight()){ 
      inty = 0; 
     } 
    } else if (controlWhichImage == 2){ 
     g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),null); 
    } 

Ce que vous ne devriez créer une instance de createdImaeg quand controlWhichImage est 1

Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if (controlWhichImage == 1){ 
     this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     Graphics g2 = this.createdImage.getGraphics(); 
     for(BufferedImage eachImage : docList){ 
      g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,this); 
      g.drawImage(eachImage, 0,inty,imageWidth,imageHeight,this); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
     } 

     if (intx >= this.getWidth() || inty >= this.getHeight()){ 
      inty = 0; 
     } 
     g2.dispose(); // This is kind of important... 
    } else if (controlWhichImage == 2){ 
     g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),this); 
    } 

ou lorsque createdImage est nul ...

Override 
protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if (createdImage == null){ 
     this.createdImage = newBufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     Graphics g2 = this.createdImage.getGraphics(); 
     for(BufferedImage eachImage : docList){ 
      g2.drawImage(eachImage,0,inty,imageWidth,imageHeight,this); 
      intx += eachImage.getWidth(); 
      inty += eachImage.getHeight() * zoomAdd; 
     } 

     if (intx >= this.getWidth() || inty >= this.getHeight()){ 
      inty = 0; 
     } 
     g2.dispose(); // This is kind of important... 
    } 
    g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),this); 
+0

Ok Je l'ai changé pour: – Gerry

+0

if (createdImage == null) { this.createdImage = nouvelle BufferedImage (this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); } – Gerry

+0

mais sur 2 il est toujours vide. Mais merci pour votre réponse – Gerry