我有一个列表(this.docList)中的两个 BufferedImage。在我的 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);
}
最佳答案
每次调用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);
}
当 controlWhichImage
为 1
时,您应该只创建 createdImaeg
的实例
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);
关于java - 如何将 JPanel 的内容复制到 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35378263/