Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By camickr

Thread: How to get rendered objects in a form of image from the canvas/Panel?

  1. #1
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Thumbs up How to get rendered objects in a form of image from the canvas/Panel?

    Hi, I have drawn some simple objects in both a canvas and a Panel.MyPanel class is
    Java Code:
    public class myPanel extends Panel implements Runnable
    Now I need to get the screenshot of the rendered objects. I was trying to get screenshot by the following code:
    Java Code:
    Rectangle rectangle = myCanvas.getBounds();
    BufferedImage screenShot = new Robot().createScreenCapture(rectangle);
    But the second line is throwing runtime exception as
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError
    What is the other solution for getting the image from canvas/Panel?

    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    Any errors when you compile this code?

    Also, have you tried creating a BufferedImage of the size of interest, getting a Graphics object from it, and calling paint on the Panel, passing in your BufferedImage's Graphics object as the paint method's parameter? Don't forget to dispose of the Graphics object when done though.

  3. #3
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    Hey Fubarable,
    How to use the Graphics object in this context? Can you please elaborate it? Thank you in advance.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    The Graphics object from the BufferedImage providea a way to draw to that image.
    By passing it into the paint() method of a Component (in this case a JPanel) you get the Component to draw itself to the BufferedImage.
    You can then save that BufferedImage to disk (using the ImageIO utility class usually).

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    Screen Image Java Tips Weblog uses both of the approach described here depending on which method you invoke. The BufferedImage code is more complicated than described above because it tries to handle painting the component whether it is visible in the GUI or not.

  6. #6
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    I was using the following code:
    Java Code:
    Rectangle rectangle = myCanvas.getBounds();
    Image screenShot = <Image>new Robot().createScreenCapture(rectangle);
    JPanel myPanel=new JPanel();
    JLabel img = new JLabel(new ImageIcon(screenShot));
    myPanel.setLayout(new BorderLayout());
    myPanel.add(img, BorderLayout.CENTER);

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to get rendered objects in a form of image from the canvas/Panel?

    6 lines of code doesn't help us.

    If you need more help then post your Short, Self Contained, Correct Example.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 0
    Last Post: 12-14-2011, 03:30 PM
  2. Replies: 1
    Last Post: 03-22-2010, 10:03 AM
  3. Replies: 0
    Last Post: 07-05-2009, 01:31 PM
  4. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 02:25 PM
  5. Canvas rendered over JCombobox menu
    By JSK in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2007, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •