Getting JPanel's Image

I'm having problems creating a BufferedImage from an offscreen JPanel. Specifically, I'm trying a draw the image of a JPanel (which contains some Java3D elements) as a background image for an application I'm working on.

I've found several threads describing how to do get a JPanel's Image by painting the JPanel to a BufferedImage's graphics context, but when I draw the BufferedImage, all I get is a big white rectangle.

Here's my code:

SimpleWorld j3DPanel; // a custom JPanel that contains some simple Java3D elements

// CONSTRUCTOR

public GameBackgroundObject()
{
    super();

    // Here I set up a JPanel that contains some Java3D elements.

    j3DPanel = new SimpleWorld();
    j3DPanel.setSize(mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.setBounds(0,0,mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.doLayout();
    j3DPanel.validate();
} 


protected void draw(Graphics2D parentComponentGraphics, ...)
{
    super.draw(parentComponentGraphics, ...);

    int w = j3DPanel.getWidth();
    int h = j3DPanel.getHeight();

    BufferedImage j3DImg = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = j3DImg.createGraphics();
    j3DPanel.paint(g);

    parentComponentGraphics.drawImage(j3DImg, null, null);

}

Also, I've tried adding my JPanel to the contents of a JFrame. It works there and shows the rendered Java3D elements correctly. However, I just get this big white rectangle whenever I try to get the BufferedImage of the JPanel and draw that image.

Answers


Drawing a Java 3D scene probably fails because its Canvas3D component is heavyweight. Using the lightweight panel 'com.sun.j3d.exp.swing.JCanvas3D' might solve this problem. Its use is a bit tricky. See the corresponding Java 3D sample 'JCanvas3DExample' or try one of the derived works from here http://www.interactivemesh.org/testspace/j3dmeetsswing.html#leightweight


Need Your Help

Reading XML using PIG

xml xml-parsing apache-pig

I am trying to read the data from the xml file using PIG but I am getting incomplete output.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.