java - 如何将 JPanel 的内容复制到 BufferedImage

标签 java swing paintcomponent

我有一个列表(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的新实例,如果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);
    }

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

相关文章:

java - 如何强制调用paintComponent?

java - Swing Java 中的累积可运行项

java - 从禁用的 JTextField 获取文本

java - 可以在 JSlider 中捕捉到某些值吗?

java 2D 和 swing

java - 单击时在 JButton 上绘制一个椭圆

java - 我如何检查给定路径列表中的文件是否存在?

java - 从 Struts2 jquery Autocomplete 获取类型值

java - 改变 lucene FuzzyQuery 中的相似度

java - 注入(inject)使用不同 Java 版本编译的类