Posted by javakiddy
on September 30, 2008 at 12:29 PM PDT
Sometimes less is more. In the spirit of the '64k intros' I've been seeing how far I can push JavaFX with a minimum of code. Now I throw down the gauntlet -- does anyone else want to join me in the quest for the ultimate 'cheap thrill'?!?
There must be a name for that particular form of programming masochism which involves wringing the maximum effect out of the minimum of code. If not, someone should invent one!
I first began coding when the Apple II and Commodore 64 (et al...) opened up computing to the masses. By modern standards their 32 or 64k of RAM is an impossibly small amount — I'd love to report it was more than enough back in the day, but I'm reminded of all those evenings I spent byte shaving. Still, as frustrating as it could sometimes be, there was a certain joy in coding those older systems, a certain pride in making everything as tight as possible to fit the most in. Shaving every byte, stripping away redundant clock cycles (zero page addressing , anyone?) and banking the Kernal ROM (Commodore's spelling) in and out of the 6510 's address space to get at the RAM 'underneath'.
In this age of the gigabyte, it can still be a useful exercise to see how much one can do with a given technology under tight (if artificial) constraints. The legendary 64k intro scene is a modern example, but as long ago as the 16 bit days I seem to recall a STOS or AMOS BASIC sponsored competition for writing games in ridiculously few lines of code (ten..?) (Actually, wasn't Brainf**k the product of an Amiga owner?)
In recent weeks I've found myself needing to develop a lot of small JavaFX examples. I looked at what Joshua Marinacci had done with his 45 line 'preview' demo , setting the bar quite high, and wondered if I could do better...The increase in power of our computers has necessitated layers of abstraction. By contrast the graphics on the old 'micros' involved nothing more than writing values into the right place in RAM. Of course graphics were very much more primitive in the 8 bit days, but for instant coding gratification I have to confess nothing has ever really beaten the thrill of poke-n-play. Which is why domain specific technologies like JavaFX come as such a welcome change — not that they allow old fashioned bash the metal coding, but they certainly put some of the immediacy back into graphics work.
After experimenting with JavaFX Script for a while I came up with the following self-imposed rules, seemingly offering a suitable balance between challenge and frustration:
- One source code file.
- One hundred or fewer lines of code.
- No external data files. It's too easy to cheat with animated images and the like.
- Eighty columns maximum. No ridiculously long lines of code.
- Modestly readable source. Obviously there's a temptation to optimise the amount of code on each line, and ignore comments, but the source should still be indented and laid out in a fairly readable way.
The bouncing balls was my opening gambit, which admittedly was a bit hit and miss. There's various effects on show here: the balls bouncing on a reflective surface, colour transitions as the mouse enters of leaves the scene, and faux lighting which shifts across the balls and background as the mouse pointer moves. To be honest, I think its a bit of a dog's dinner of effects and tweens.
My second attempt was a bit more focused: a fireworks effect. At random intervals multi-coloured rockets shoot into the night's sky, before exploding with a flash into sparks, drifting back towards the ground and fading as they go. It's a pleasing enough effect, but I get the feeling there's still more which can be done. I'm thinking perhaps something faux 3D, or involving parallax scrolling?
As you may be able to tell, it's quite addictive stuff. With only 100 lines to play with the mind races, searching for cunning ways to create big effects with small amounts of code. I can certainly see why the 64k guys do it.
If you want to check out the demos for yourself the source code (all 193 lines, combined) is available here . It should build/run using the 1st Preview of JavaFX from a few weeks back, with Java 6 update 10.
And so, I hereby throw down the gauntlet to anyone who can come up with even cooler demos (or improve the two above.) Do you think you can beat my fireworks, in 100 lines or less? Surely it can't be too hard?!? Go on, give it a try — like me, you may be surprised at just how much fun it is!