Friday, September 2, 2011

Groovy graphics...

I know i know...it's nearly a month ago the last time i blogged about something but i just can tell you i have learned a lot in the last month...
Those of you who follow me on twitter know it already i started playing with Groovy and the only thing i can say about it is ... IT REALLY ROCKS !!!
It might not be the one and only solution for everything but it makes you so much more productive, it's amazing.


Thanx to Dierk König and Andres Almiray who motivated me to choose the groovy way of life.


Two weeks ago i was at Canoo (which is btw a really really nice company) again in Basel (CH) and during the two days of my visit Dierk came up with the idea of using an Adobe fxg file as resource for the design of a component. This means you have to parse the file in process instead of preprocessing the file. This idea lights my fire and i started looking into Groovy to create a new fxg parser. 


Well Groovy and XML really is a dreamteam, so creating the parser was really not a big deal. After the parser was finished i started looking into a so called translator which translates the converted fxg data into different languages. First of all i started with Java2D which was easy, after it was ready to go HTML5 Canvas, GWT followed.


So now the new Parser-Translator duo is able to convert Adobe fxg files into files for all those languages. It's not completely finished because i have to come around the 64kb barrier for methods/classes in Java but i'll fix this asap and will use the new converter to replace the current one.


A few days ago Dean Iverson asked me if it would be possible to create also JavaFX 2.0 output and so i started looking into this. Yesterday i finished the export to JavaFX 2.0 and was really happy about it.
So excited that i could not sleep which let leave the bed around 4:30 AM to get my ideas into code.


The idea came from Dean Iverson and was about creating a JavaFX 2.0 component on the fly using an Adobe fxg file so a similar idea like the one from Dierk König. First of all i was not sure if i could easily realize but this morning i knew i could and so i started coding.


The result is 450 lines of Groovy code that makes it possible to parse a fxg file live and return JavaFX Groups for each layer of the fxg file. 


Here is a little screenshot of a JavaFX component using a fxg file on the fly:




And all you need to realize this is the following piece of JavaFX code...


public class FxgTest extends Application
{
    @Override
    public void start(Stage stage) {
        Test component = new Test(400, 400);
        Scene scene = new Scene(component, 400, 400);
        stage.setTitle("FXG -> JavaFX (live)");
        stage.setScene(scene);
        stage.show();
    }

    public class Test extends Parent {
        public Test(int width, int height) {
            String fxgFile = "/FILE.FXG";
            FxgFxParser parser = new FxgFxParser();
            Map groups = parser.parse(fxgFile, width, height, true);
            getChildren().addAll(groups.values());
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}


If you now start the application, it will load the given fxg file, parse and convert it and display it on the stage. If you would like to play with this stuff you need to have the JavaFX 2.0 beta installed otherwise it won't run. 
If you are on a Mac you need to be a member of the Java Partner Program of Oracle to get the Mac version of the JavaFX 2.0 beta. You will also need Groovy 1.8 installed to play with the code but you could easily download it at Codehaus and install it without a problem.
The code itself is hosted at github and could be found here:


git://github.com/HanSolo/FxgFXParser.git


Well it's not the biggest blogpost but i'm really excited about it and so it was worth the time to write it...


The SteelSeries 3.9.4 is also nearly finished and will be released soon...


At the moment i'm writing this Dean wrote me that FXG files exported by Adobe Illustrator do not convert nicely so i'm on that now and will hopefully fix it soon.
FXG Files exported by Adobe Fireworks should be fine (don't ask me why they export different kind of FXG dialect)


Keep coding and enjoy the upcoming weekend,


Gerrit

7 comments:

  1. I was among those following your progress on Twitter... I am amazed by your rate at producing code, even more with a language you didn't know before.
    Personally, I chose Scala (but Groovy remains in my todo list...), and I struggle to make a nice text parser. But at the same time, I learn a lot and enjoy it.

    Anyway, while you were producing code and tweets, I was wondering what is the interest of this parser. I mean, beyond the mere fun of coding it. Are there some free FXG clipart libraries around? Is there any advantage of this format over SVG, for example?

    ReplyDelete
  2. Hi PhiLho,
    Think about creating your component in a design program like Adobe Fireworks or Adobe Illustrator and use it in a scalable way directly in your software. So in the best case a designer could create outstanding componentdesigns and you could easily combine it with your code.
    Thats the way it works in Flex as far as i know.

    Cheers and keep coding,

    Gerrit

    ReplyDelete
  3. I'm curious why you would need to use a groovy parser to spit out java? Doesn't the Adobe open source FXG parser do what you need?

    http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/modules/fxgutils/

    ReplyDelete
  4. When I started writing the first version of the fxg parser around 1.5 years ago I didn't know anything about the Adobe parser and this new version was also to learn about Groovy. And if you would ask me if I would do it again even if would know that a parser exists...I would answer yes, just for the fun of coding...
    Cheers,
    Gerrit

    ReplyDelete
  5. Hey Eingestellt,

    Awesome blog! Is there an email address I can contact you in private?

    ReplyDelete
  6. OK, so the purpose isn't to use existing graphics, but to use Adobe products to export to this format. Why not.
    I can't afford Adobe products, so I use Inkscape. I see they don't support FXG files out of the box, but there is a plug-in allowing such export. FYI.

    ReplyDelete