tags:

views:

310

answers:

4

Hi,

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

A: 

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().

kdgregory
the link you referenced seems to be local, you might want to update that before someone downvotes you for an innocent mistake.
Anthony Forloney
+1  A: 

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.

OscarRyz
Oscar - where did you spend all your reputation?? Bounty? ^^
Andreas_D
Every time I reach 10K I reborn :)
OscarRyz
+2  A: 

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.

Thomas
+1  A: 

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();
    }
Carlos Heuberger
You may find you need to use printAll(Graphics) rather than paint(Graphics). I think that is the recommended method for this sort of thing.
Russ Hayward