我从列表中有两个BufferedImages(this.docList)。在我的paintComponent方法中,我将它们绘制到JPanel,同时在'this.createdImage上绘制它们。
@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;
}
我的问题是,当我使用createdImage时,我得到的只是一个空白面板。
if (controlWhichImage == 2){
g.drawImage(this.createdImage,this.getWidth(),this.getHeight(),null);
}
答案 0 :(得分:2)
每次调用paintComponent
时,您都会创建BufferedImage
的新实例,如果controlWhichImage
为1
,则会将图像绘制到BufferedImage
,如果它是2
它画的,没什么。
基本上你的代码基本上就像......一样......
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);
}
当createdImaeg
为controlWhichImage
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);
}
或当createdImage
为空时......
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);