Hi,
I’m working on some tools and I’m trying to create a GUI with a native look and feel using Swing.
Most of this stuff (menus, toolbars, etc.) works fine, but now I want to overlay the JME Canvas with a JPanel for a paintComponent() implementation, so I can use Graphics2D draw commands on it.
Here is some initialization code:
// Render Panel
viewportPanel = new JPanel();
viewportPanel.setBorder( BorderFactory.createBevelBorder( BevelBorder.LOWERED ) );
viewportPanel.setLayout( new BorderLayout() );
window.add( viewportPanel );
JLayeredPane renderPanel = new JLayeredPane();
viewportPanel.add( renderPanel, BorderLayout.CENTER );
renderPanel.add( jmeCanvas, new Integer( 0 ) );
jmeCanvas.setBounds( 0, 0, 400, 400 );
GraphicsPanel gp = new GraphicsPanel();
gp.setDoubleBuffered( true );
gp.setBackground( new Color( 0, 0, 0, 0 ) );
gp.setOpaque( true );
renderPanel.add( gp, new Integer( 1 ) );
gp.setBounds( 0, 0, 200, 200 );
At this point I’m setting the components bounds manually to avoid any layout troubles.
Here is the super simple GraphicsPanel implementation:
public static class GraphicsPanel extends JPanel
{
@Override
public void paintComponent( Graphics gfx )
{
super.paintComponent( gfx );
Graphics2D g2d = ( Graphics2D )gfx;
g2d.setColor( Color.RED );
g2d.drawLine( 0, 0, 200, 200 );
}
}
But unfortunatly it looks like this:
It seems like although I’m setting the background color to transparent (zero alpha as suggested by some stackoverflow thread) the graphics panel does not clear its background pixels and overdraws the JME canvas!
Could this be because JME’s rendering is done using hardware accelerated OpenGL where as Graphics2D is software drawing?
I already tried drawing graphics to a BufferedImage and use that for a texture overlay on the gui node but that has a very slow performance (and gets worse as the render panel is becoming larger).