<< July 3, 2009 | Home | July 5, 2009 >>

On to the next widget

Screenshots and minor graphics editing with JavaFX 1.2

JavaFX 1.2 was released a while back with quite a lot of new features. The release was also accompanied by a blogging competition to get the buzz going. I didn't think I had anything to enter into that contest since I've not had much time for JavaFX. However, a couple of days ago new 1.2 compatible version of WidgetFX was released and I thought it was time to create a new widget. As I've said before, creating a widget is a fun way to explore JavaFX and right now it actually seems like I could enter a blog post in time as well :)

Anyway, on to the code...

With the increased performance now even more than before I find JavaFX to be a great way to play around with and create quite advanced stuff in a very short time... while on the other hand the other way around might be true every now and then as well.

The project

For example... a college usually attaches screenshots to mails when he want's to describe some change or feature update to the company site/admin interface. Obviously that makes it easier to quickly grasp what he's talking about. Another college mentionend that he probably has Photoshop running all the time. Well, that might be the case, but I doubt Photoshop's memory requirements allow for that. That made me think that a quick to access widget really should be the way to go for these things. It's only going to take a screenshot, how hard can it be?

Getting a screenshot

The power of JavaFX beeing built on the Java platform comes to play here... I quickly used the Robot class to get a screenshot of the desktop. The point to make here of course is that I had used this class a few years ago and even though JavaFX doesn't have a screen capturing feature I could use my previous Java knowledge for this.
        Robot robot = new Robot();
        BufferedImage bi = robot.createScreenCapture(new Rectangle(x, y, width, height));
    

Very easy... a few minutes of work with some file handling and other stuff. But hey, just taking a screenshot still means that one has to manipulate the image in another program to highlight something. I did remember that I saw a JavaFX drawing example on the JavaFX.com site. That's probably easy to add. Sure enough, following Jasper Potts' example code gave me a Node I could draw on in no time at all.


Capture screenshot & draw

Such a feature would of course be useless unless I could save the edited image. I did remember that I previously had seen something like that among the tutorials as well. After a while I found this demo with the save image feature.

Something unsupported

A few lines of code, unfortunately hacking JavaFX a bit to get it to work. Usually not a good idea, but I can't go without a save feature. Turned out the "save node as image" function didn't work any longer, probably broke in the JavaFX 1.2 release. Here's one of the bad parts beeing such a young language and all. When working with JavaFX there's been quite a few times when things like this has happened. Features like layout, controls etc. which one would think obvious parts of such a language/platform have been missing. Fortunately for my little program this exact example also shows the strenght of JavaFX in another way. I'm not talking about the language here, but what it's build on. I.e the Java platform and it's community. Someone had already solved this problem in Sun's forum. I certainly believe those things to be what will make it survive and prosper.

Of course, I'll have to maintain this code since it'll most likely break in every release, but I quickly got the feature and eventually I can replace it with a library. Perhaps the feature itself will be added to JavaFX some day.

Dragging around



Hover over the square to see what you can drag
Great. Capturing a screenshot, drawing stuff on it and saving it... done? Hmm... it really would be fun to be able to mark an area and darken the rest of the image. Would certainly look better in many cases... I've used Path before and it seemed to be suitable for this task. It did take some time and it's pretty basic, but after a couple of hours I had something useful.

This is made all by me so for this code I don't have anywhere to link. Here are the three files for Example2: Example2.fx, MovablePath.fx and Point.fx

Finishing up

I got a couple more features that would be fun to make, but it's probably a good time to put together something usable at this point. I will have to put some time into the GUI as well. Can't wait for the tool Tor demoed at JavaOne 2009 (it's part of James Gosling's toy show). Looks awesome!

That brings me to my final point. I've found that the way I like to develop in JavaFX is to always create new Stages (and sometimes whole projects) for everything. Just like all programming it's a good thing to keep classes and methods small, but with NetBeans an added benefit is to be able to preview a Stage. Jumping around in a lot of classes takes one away from that preview. When I think about it it would be nice to have an option to lock the preview to a certain class or perhaps the project main class. Anyway, when I'm done with one of these Stage-tests I "convert" it into a real class which usually mean that I'll have to remove a lot of test code, commented code and other useless lines. Then it's ready to be put to work in the real project.

Conclusion

Is short I'm having fun with JavaFX and with version 1.2 I've not felt any slowness at all. I'm still missing things, but progress is made. It would be great if the next version could be binary compatible though :)

Tags :