A few years ago, we completely overhauled our simple, static PageFlow mode and started from scratch with Java 3D. It’s served us well, but we think now is the time to move on to newer, better supported and more widespread technologies.
We thought it might be interesting to look back at our experience with Java 3D, and perhaps see how other technologies can learn from its successes and failures.
5. Swing really can’t do everything…
Here’s what PageFlow looked like when we wrote the first version in Swing:
It’s completely static and has rough edges. Even this depends on the abandoned JAI libraries for Perspective transforms, which are pretty slow – you always see blank pages while the images load in.
And in Java 3D:
I think that pretty much tells the story.
4. …But you still need to deal with it
Even though we wrote PageFlow itself in Java 3D, it still had to slot into our Swing based viewer. That meant putting it inside a standard Swing LayoutManager with the hope that it’d just fit in and work.
Well, for the most part, it did – with the notable exception of the menu bar. No matter what you do, standard Swing menus will always hide behind a Canvas3D – the only workaround is to use the old Heavyweight menus.
Another nice feature is the ability to draw over the top of the 3D canvas using Swing’s Graphics2D interface. This works through overriding the postRender() method of the canvas. This was originally used to draw the navigation bar over the scene, until it became clear that this was the primary cause of some pretty major performance issues! Unfortunately this forced us to place the scrollbar in its own component below the main scene, where it does not affect performance in the slightest.
3. Developers expect the same kind of backwards compatibility as with the core libraries
One of the key features of PageFlow is being able to click on a page to make it central. You probably expect that this is simple.
Well, it’s not as simple as you might think. Rather than events going to the objects being rendered, all events go to the Canvas. You then use a process called ‘Picking’ to work out which object was under the cursor during the event.
Java 3D has always supported Picking, but 1.2 introduced a completely new (and, at least, much improved) API for picking, while deprecating those present in 1.1.
I found that similar situations were present across most versions of Java 3D, including differences between the Mac and PC versions, meaning I eventually had to lock it down to version post 1.31 on PC and 1.4 on Mac.
2. Scene graphs work
Admittedly ‘scene graph’ is a term with fuzzy edges – one could even argue that Swing’s system of nested layout managers could be considered a scene graph – there is no doubting that Java 3D uses the concept to great effect.
For example, a page is actually an object of type Box. The page needs to move, so we add it to a TransformGroup. However, we want a shiny reflection from the surface below - but Java 3D can’t do that, so we fake it.
We have a second box, the same as the first, but upside down – we created another TransformGroup, this one simply to flip the page upside down. We now add this whole new subtree which creates the reflection to our main TransformGroup which already holds the Page.
Now we have both a page and it’s reflection, ready to be transformed. Want the page to move left and right? Sure. Zoom in from the back of the scene? No problem. Repeat for the number of pages in the PDF you’re viewing, and we’re in business. It’s hard to imagine a clearer solution to the organisational problem of arranging visual elements than the scene graph.
1. If you’re not included in the main Java download, nobody will use you
Really, I find it hard to believe that any end user is likely to bother to install Java 3D – certainly not by downloading it from Oracle’s page. The obvious solution is to offer a bundle with both your application and Java 3D – but Java 3D is, unlike Java itself, platform dependent. This leaves creating separate bundles for each platform as your only option, which somehow just doesn’t feel right when working with Java.
So there we have it – have you played with Java 3D? What did you think?
And for those of you worried about our PageFlow mode disappearing, fear not – we already have something better waiting in the wings.
Latest posts by Sam Howard (see all)
- Music and Coding Part 1: Why listen while you work? - July 16, 2014
- 3 Reasons why we don’t convert Type 1 glyphs to TrueType - June 25, 2014
- Web fonts: What’s next for typography online? - June 5, 2014
- Google Chrome Font Caching Bug - May 14, 2014
- Profiling Vs. Sampling in Java’s VisualVM - April 24, 2014