如何将JPanel的内容复制到BufferedImage

时间:2016-02-13 09:34:49

标签: java swing paintcomponent

我从列表中有两个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);
}

1 个答案:

答案 0 :(得分:2)

每次调用paintComponent时,您都会创建BufferedImage的新实例,如果controlWhichImage1,则会将图像绘制到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);
    }

createdImaegcontrolWhichImage

时,您应该只创建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);