开发者

Convert a region of a JPanel into a BufferedImage

I need to convert a certain region of an jpanel into a bufferedImage, or other format to be shown in another jpanel.

By now, I only saw codes that 开发者_高级运维converts the whole jpanel into a bufferedImage, but in my case, I need just an area inside an jpanel.

thanks


create a BufferedImage with the requested size to receive the image.
Get a Graphics2D for drawing on this image and let the JPanel paint on it.

    JPanel panel = ...
    BufferedImage image = new BufferedImage(200, 200, TYPE_INT_ARGB);
    Graphics2D gg = image.createGraphics();
    try {
        gg.translate(-100, -20);  // start point of region negated
        panel.paint(gg);
    } finally {
        gg.dispose();
    }


Since you already have code to convert the entire thing to a BufferedImage, you can use that, then call getSubImage on the resulting BufferedImage to get a subregion.


By now, I only saw codes that converts the whole jpanel into a bufferedImage, but in my case, I need just an area inside an jpanel.

Then take that image and repaint the desired area into a new image and you're done.


The easiest would probably be Robot.createScreenCapture()

You'll need to translate from the panel's coordinate system to the screen coordinate system. See Component.getBounds() and Component.getLocationOnScreen().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜