I wish to take reference a JPanel I created

Go To StackoverFlow.com

1

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!

2012-04-05 01:08
by user1313973
Simply use this, it always refers to the current Object of the class, no need to create Object like MyClass objectName = new MyClass();, use this instead, anywhere (except the constructor), you need the reference of the Class you working in :- - nIcE cOw 2012-04-05 04:50


1

I think that

createImage(YourClassName.this)

works

2012-04-05 01:33
by pancatrevisan
Ads