Overall, I'm looking to draw a displayed JPanel onto a bufferedImage and save it to a file. But I'm having a hard time understanding how do I reference the JPanel I'm painting on.
My class extends JPanel, and I am drawing on it but I never explicitly define a JPanel object except for I guess in my main method(when I add it to the JFrame), but this is in a different class obviuosly. How do I reference the JPanel I am implicitly drawing on in the class that extends JPanel so that I can pass it as a parameter to :
public static BufferedImage createImage(JComponent component)//<---I want to pass my JPanel in here to create a buffered image then use ImageIO.write() to save the file. { Dimension d = component.getSize();
if (d.width == 0 || d.height == 0)
{
d = component.getPreferredSize();
component.setSize( d );
}
Rectangle region = new Rectangle(0, 0, d.width, d.height);
return ScreenImage.createImage(component, region);
}
I hope this question makes sense. I know I sound like a newbie, and it is because I am. Please help me. Thanks!
I think that
createImage(YourClassName.this)
works
this
, it always refers to the currentObject
of the class, no need to createObject
likeMyClass objectName = new MyClass();
, use this instead, anywhere (except the constructor), you need the reference of theClass
you working in :- - nIcE cOw 2012-04-05 04:50