Java 如何将JPanel的内容复制到BuffereImage
我从一个列表(this.docList)中有两个BuffereImage。在我的paintComponent方法中,我将它们绘制到JPanel,同时在'this.createdImage'上绘制它们Java 如何将JPanel的内容复制到BuffereImage,java,swing,paintcomponent,Java,Swing,Paintcomponent,我从一个列表(this.docList)中有两个BuffereImage。在我的paintComponent方法中,我将它们绘制到JPanel,同时在'this.createdImage'上绘制它们 @Override protected void paintComponent(Graphics g){ super.paintComponent(g); this.createdImage = newBufferedImage(this.getWidth(),this.getHei
@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
的新实例,如果controlwhichichimage
是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
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);
每次调用
paintComponent
时,您都会创建一个BufferedImage
的新实例,如果controlwhichichimage
是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
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);
好的,我已将其更改为:if(createdImage==null){this.createdImage=new buffereImage(this.getWidth(),this.getHeight(),buffereImage.TYPE_INT_ARGB);},但在2上它仍然为空。但是谢谢你的回复,好吧,因为你选择不提供一个能证明你的问题的答案,其他所有的事情都是纯粹的猜测。对不起,我试着让它尽可能简单。这并不是说我不想提供一个可运行的示例,而是说如何使其保持简单。好的,我已经将其更改为:if(createdImage==null){this.createdImage=new buffereImage(this.getWidth(),this.getHeight(),buffereImage.TYPE_INT_ARGB)},但在2上它仍然是空的。但是谢谢你的回复,好吧,因为你选择不提供一个能证明你的问题的答案,其他所有的事情都是纯粹的猜测。对不起,我试着让它尽可能简单。这并不是说我不想提供一个可运行的示例,而是如何让它保持简单。不要执行昂贵的操作,比如在paint方法中创建BuffereImage。每秒可以调用多次paintComponent(和所有绘制方法)!重复。不要执行昂贵的操作,如在绘制方法内创建BuffereImage。每秒可以调用多次paintComponent(和所有绘制方法)!副本。