Results 1 to 7 of 7
- 01-12-2012, 11:29 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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
Now I need to get the screenshot of the rendered objects. I was trying to get screenshot by the following code:Java Code:public class myPanel extends Panel implements Runnable
But the second line is throwing runtime exception asJava Code:Rectangle rectangle = myCanvas.getBounds(); BufferedImage screenShot = new Robot().createScreenCapture(rectangle);
What is the other solution for getting the image from canvas/Panel?Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError
Thank you.
-
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.
- 01-13-2012, 09:46 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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.
- 01-13-2012, 12:19 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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).
- 01-13-2012, 06:05 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 01-14-2012, 04:51 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
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);
- 01-14-2012, 05:14 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
Similar Threads
-
How to save the previous state of objects drawn in a canvas?
By ravi.joshi53 in forum AWT / SwingReplies: 0Last Post: 12-14-2011, 02:30 PM -
looking for a form pane or form group panel for the jpanel to complete tutorial
By nadeemshafi9 in forum AWT / SwingReplies: 1Last Post: 03-22-2010, 09:03 AM -
SWT Canvas drawing realtive to image space not canvas space.
By bobbie in forum SWT / JFaceReplies: 0Last Post: 07-05-2009, 12:31 PM -
Canvas Image popups another image (SWT)
By SpaceY in forum New To JavaReplies: 2Last Post: 11-11-2008, 01:25 PM -
Canvas rendered over JCombobox menu
By JSK in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 08:00 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks