Using a native renderer for Swing

Hi,

I have implemented a new Graphics2D class that uses the Scenic graphics library to do rendering. Now I want to use this new renderer in Swing. I have managed to create a JFrame that uses native rendering. However, I don’t now how to make the swing components use the new renderer. Does anyone now what is the best way to do this?

The homepage of Scenic is at http://www.sceniclibrary.org/

Br,
Jouni

Wow, great idea! :o 8)

I’m no expert on this and if I were you I’d ask this question on the these forums: http://forums.java.net/jive/forum.jspa?forumID=74. There are some serious Swing experts there, including the Sun Swing/AWT team.

But let me suggest something. This is java.awt.Component’s getGraphics method:

public Graphics getGraphics() {
if (peer instanceof LightweightPeer) {
// This is for a lightweight component, need to
// translate coordinate spaces and clip relative
// to the parent.
if (parent == null) return null;
Graphics g = parent.getGraphics();
if (g == null) return null;
if (g instanceof ConstrainableGraphics) {
((ConstrainableGraphics) g).constrain(x, y, width, height);
} else {
g.translate(x,y);
g.setClip(0, 0, width, height);
}
g.setFont(getFont());
return g;
} else {
ComponentPeer peer = this.peer;
return (peer != null) ? peer.getGraphics() : null;
}
}

Since JFrames are heavyweight they must use the red code, and I don’t know how to over-ride ComponentPeer.getGraphics() (it isn’t even in the java docs). However if you sub-class JFrame and over-ride its getGraphics() method so that it returns your custom Graphics2D object then any components that you add to the JFrame should recieve your custom Graphics object. This isn’t an ideal solution though since any new Window or Dialog you create will have the default Graphics2D object and not yours. Let us know how this project of yours turns out!

Keith

PS: Under the DirectX mode does Scenic do everything using DirectX and not software rendering? If so this could be better than the Java2D windows pipeline which doesn’t do everything using DirectX hardware acceleration (I’m not sure what exactly the pipeline doesn’t do, but I know it runs at 2 FPS as soon as you start scaling translucent images, even with all of the VM options on).

I got the thing working by implementing my own ScenicJFrame class, which overrides the getGraphics and paint functions. I also extended RepaintManager to disable double buffering. I can already run Sun’s Java2D demo but the frame rate is slow. This is probaly because Scenic is a scene-based library and therefore I need to first create a scene and then render it. Because Java2D is immediate mode, I need to recreate the entire scene for every frame, which is costly.

Both under DirectX and OpenGL Scenic uses hardware rendering for all primitives, including antialiased lines and polygons. So the performace should be good, at least if there are no other bottlenecks.

Sorry but I never understood why people buy all those hassles when having something like Java2d already for free :wink:

I have done more development and the renderer now works quite well. The performance is a lot better and is already quite good. You can try the Java2D demo at:

http://www.sceniclibrary.org/jar/0.12.4/

The demo works only on Windows.