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);
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
Dupliquer de [* Copier le contenu d'un JPanel sur un BufferedImage *] (http://stackoverflow.com/q/35363892/230513). – trashgod