java – 如何获取paint / paintComponent生成的图像?

我有一个快速的问题.如何获取JComponent.paint或paintComponent生成的图像?

我有一个JComponent,我用它作为’工作区’,我已经覆盖了paintComponent方法到我自己的方法.问题是我的工作区JComponent也有子项,它们有自己的paintComponent方法.

因此,当Swing渲染我的工作区组件时,它会渲染工作区图形,然后渲染其子图形.

但是,我想获取工作区组件生成的图像(包括工作区图形和子图形).

我怎么做?

我试图通过使用我自己的Graphics自己调用paintComponent / paint-method,但我刚刚返回了一个黑色图像.这是我尝试过的;

public void paintComponent(Graphics g) {

    if (bufferedImage != null) {
        g.drawImage(bufferedImage,this);
    }
    else {
        g.setColor(Color.WHITE);
        g.fillRect(0,bufferedImage.getWidth(),bufferedImage.getHeight());
    }
}

public BufferedImage getimage() {

    BufferedImage hello = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
    Graphics g = hello.getGraphics();
    paintComponent( g );

    return hello;
}

欢迎任何想法或意见!

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...