Showing posts with label JavaFX. Show all posts
Showing posts with label JavaFX. Show all posts

Friday, 5 October 2012

Highlights From Java One 2012

Highlights From Java One 2012

When my talks were first accepted for Java One 2012, I never imagined I would feel like this at the end of the week. Currently, I'm a mixture of so burnt out I can barely walk yet so full of adrenaline I don't want to sleep. As a technologist this week has been the highlight of my career. This post will detail a few of the highlights of the week for me.

Keynote and Lunch with James Gosling

If someone had told me I'd be sat having lunch with James Gosling after participating in the Java One community keynote I would have branded them insane. However, that's exactly what happened today. With my friends in the London Java Community we had a panel session discussing a flag ship initiative we have been working on known as Adopt-a-JSR. The keynote was so far from corporate it was refreshing, it was all about community for the community. 


We saw key technical companies participate in the initial panel, us and then a crazy robot chasing down Oracle executive Georges Saab on stage. Then something special happened, James Gosling entered the stage. He was talking about his new development at Liquid Robotics. The best part about his appearance was James was back amongst a community of developers that have a high degree of respect for him and his work with Java. I found his presentation a highlight of innovation that is ongoing in the industry. After the keynote I headed over to Stephen Chin's Java Desktop lunch. We were sat at the same table as James and discussed details of the UI that he had built and demonstrated during the keynote and discussed some of the problems that data heavy systems would cause web based applications. What a great note to end Java One on.

Java FX

Java FX has been technically the most exciting product of Java One for me. It really starts to represent the revival of Java on the desktop. Although Swing has lived on over the last few years, how many people have really wanted to do this? Java FX simplifies a developer's ability to rapidly create flexible and maintainable user interfaces. Scene Builder also provides an abstraction away from code, so you can now have a design team that builds the basic CSS and component layout and the development team adds the functionality on top. 

At Java One myself and Ben Evans presented our toy project visualising garbage collection in Java FX.   One other highlight of the week was when I met Jim Weaver near the exhibition hall. He asked me if I'd like to come over to watch him give a demo. Within about 15 minutes I was stood at the front of the demo grounds giving a presentation on I'm new to Java FX and this is what I've built. Thanks Jim! That said it was great fun and all the Java FX team are really fun people to hang around with. 


My challenge for our next JUG meeting is to get a Java FX example running on the Raspberry Pi. Hopefully the Pi will arrive before then, if not I'm really looking forward to sharing everything else about Java FX back in the London Java Community.

Adopt a JSR

Adopt-a-JSR is a global initiative that was kicked off in London to help make the JCP (Java Community Process) relevant to Java developers and increase their participation. Adaptavist's Dan Hardiker had the following to say about the project:
Jax - What in the development world (project or otherwise) is really interesting you at the moment?
Dan - The Adopt-a-JSR programme ran by our own LJC, is one of the most important initiatives I've seen in a long time. It puts developers back in the driving seats helping to steer decisions on important areas of the language they live and breathe.
From attending Java One it was clear to see that although we have managed to reach senior members of the community there are still many members of the Java community that are not aware of the program or the aims. A second interesting point was that many of the developers attending our sessions are not members of Java User Groups. In order to address these points we are going to look at how we can be wider reaching to developers that are not members of JUGs. Recording sessions and broadcasting them to the wider community is going to be part of the strategy we look at to address this.

JSR-310 - New Date Time API

In the background to Java One we have been working hard with Oracle and Stephen Colebourne to address the final design decision of JSR-310. A lot of hard work is going into this JSR behind the scenes and at the end of some fairly heated debates we are looking to be on track for Java 8. If you're reading this and you still want to help, we are making a push for people to help us with the following:
In our continued efforts to evaluate and improve the Threeten Date/Time API for Java 8 we were discussing the effort of porting code from existing APIs. Does anyone have open source examples of code which they wrote against the existing java.util.Date or Calendar that you felt was overly complicated? I have some code myself, but I'd really like to hear from you if you have stuff that's open source and available for hacking on.
If you can help please feel free to drop me a tweet @JavaJimLondon.

Embedded

One final point that really excites me is robots! There has been a taster of Java running on embedded hardware, we've also seen some really cool demonstrations of Java FX running on embedded hardware and a Raspberry Pi. This is what I'm going to be playing with over the next few months, along with Java FX to try push the bounds of what is possible.

Thanks

A big thanks has to go out to Sharat for organising a fantastic Java One, this year really had a community feel about it. Thanks to all the speakers and the content committee, it's been awesome!

Sunday, 30 September 2012

Highlights From Java One: Day 1

Java One - San Francisco



Today was my first day at Java One, and my first international conference. It has been an immensely fun day with loads going on.

8.15 AM - First talk of the conference - ours!

The first session we attended was our talk titled "The bluffers guide to terminology". The talk walked through terminology that complicates our industry, and made observations on how business, technical and academic domains are complicated enough without additional terminology. We walked through the different sections explaining terms and simplifying them so everyone could understand. For 8.15am the audience were very engaged, and it was clear that this was something everyone could relate to. We concluded that you have to be very careful when picking terms to represent your domain model objects in software development. You need to think about having a suitable abstraction that is clear as to what it represents in the domain, but doesn't complicate the domain further.

Winning an award

For the last 18 months, myself and colleagues from the London Java Community have been working on two key initiatives - Adopt a JSR and Adopt Open JDK. Today we were awarded with a Duke's Choice Award for the hard work we have put in inspire the community to be more involved in contributing to the future of Java. It was very exciting to see the LJC's name on the big screens at the Java One keynotes. Although winning an award was not something we set out to achieve, it is an honor. It has inspired me to carry on pushing on the initiatives we started and to use Java One as an opportunity to encourage global participation. It's also got me thinking about a future initiative, but for now that will remain top secret.


Technical Keynote

I'm still processing some of what was in the keynote, but my initial reaction is how excited I am about Java on the desktop. Having experienced the pain of Swing and context switching between .Net and Flex as alternatives, I can really see a future for Java on the desktop. That wasn't all however, we actually got to see Java FX running on a Raspberry Pi and other embedded devices. There's actually an embedded version of the Java One conference scheduler deployed around the hotels! This has really steered my interest for the conference as I'll be looking to attend a large number of Java FX talks this week at Java One. We also have our talk on visualising the Java GC model on Tuesday, which will go into building an application from the beginning in Java FX what we have learnt about the technology and how to use Java FX as a teaching and visualising technology from a slightly different perpective to grids and graphs.

Party

The day ended with collecting the Duke's Choice Award and a party in the street next to the hotel. Most of us have now returned to try and shake the jet lag before a packed day tomorrow.

Wednesday, 12 September 2012

JavaFX Help: Too much animation at the same time!

I am currently working on building a Java memory model teaching tool in JavaFX, the project is available on GitHub. Whilst building the user interface I ran into a performance issue, which is easily worked around using JavaFX. This post gives the detail to the problem and how to easily resolve it.

In the memory model we have several memory sections made of memory blocks. In our application we represent each memory block with a StackPane. This has a Rectangle object at the bottom and a Text object on top of this to represent the current generation that block is in.

A block has three states it can be in:
  • Free - nothing has been allocated here yet.
  • Alive - the block holds some active memory.
  • Dead - the memory in the block has now died and is awaiting garbage collection.

We have potentially several threads that are running against the model allocating and killing memory. The challenge is animating each of the state transitions on the UI. A picture shows the animation in action:


The code segment that we used for the transition is as follows (there might be a better way to do this):

     FadeTransition fadeOldBlockOut = new FadeTransition(Duration.millis(10), view);  
     fadeOldBlockOut.setFromValue(1.0);  
     fadeOldBlockOut.setToValue(0.0);  
     fadeOldBlockOut.setCycleCount(1);  
     fadeOldBlockOut.setAutoReverse(false);  
     fadeOldBlockOut.play();
  
     FadeTransition fadeNewBlockIn = new FadeTransition(Duration.millis(1500), view);  
     fadeNewBlockIn.setFromValue(0.0);  
     fadeNewBlockIn.setToValue(1.0);  
     fadeNewBlockIn.setCycleCount(1);  
     fadeNewBlockIn.setAutoReverse(false);  
     fadeNewBlockIn.setDelay(Duration.millis(1));  
     fadeNewBlockIn.play();   

This code was added to the view object. However, once you get multiple of these firing the application will quickly start to hurt itself to keep up with the transitions required. Having read Java FX Pro 2 I quickly realised that I had taken the wrong approach. Large amounts of updating graphics, especially timeline style animation, does not belong on the main application thread. Thankfully JavaFX has an easy paradigm to resolve the issue.

I created an Object that implements Runnable, which would take the memory block I want to animate in the constructor and perform the logic above:

 class CustomMemoryBlockViewTransition implements Runnable {  
   private final MemoryBlockView view;  
   public CustomMemoryBlockViewTransition(MemoryBlockView view_) {  
     view = view_;  
   }  
   @Override  
   public void run() {  
     //...  
   }  
 }  

Once you have your Runnable defined (you can also do this as an anonymous inner class) you can simply call the Platform to run the animation at a later point. The Platform keeps a queue of activities submitted and will execute these in order.

 Platform.runLater(new CustomMemoryBlockViewTransition(this));  


Monday, 10 September 2012

Book Review: Pro JavaFX 2


I'm a server side Java developer with 10 years experience, and 2 years experience in Adobe Flex. With Flex taking a recent slating in the press, I decided it was time to broaden my UI experiences to JavaFX and HTML 5 with Java Script. Because I know Java well, and having previous experiences with Swing, I chose JavaFX and this was the book I chose to help me learn the basics.

The first thing to note about this book is it's written by the key people behind the JavaFX technology. The advantage of this is the reader is immediately learning how the platform was designed and intended for use.

The book is split into 10 chapters, each with comprehensive code listings on how the examples and components are built. The chapters are outlined below, along with how I found them useful in the work we have been doing building a basic Java Memory Visualizer in JavaFX.

Getting a Jump Start in JavaFX

The book begins with a brief history of JavaFX, and discusses the original version and the introduction of 2.0. The book goes immediately into an example, building an application using a Java Class extending Application. There's also enough to get started in this chapter with getting Netbeans set up and ready to go to build your applications. There's a lot of content here, so don't let this put you off. There's some important code segments that you will probably need to flick back to along the way. My only observation at this point would be remember that there is still the option of using FXML to simplify a lot of the code that is in this chapter - so this chapter should be viewed as a primer.

Creating a User Interface in JavaFX

We're back out of the deep end now and will be walked through the scene and layouts of creating a typical user interface. This chapter is critical to when you come to build your own UIs. If you want to grab a chapter to help you get started quickly this would be the one. One minor criticism would be this would be a good chapter one, but I can appreciate why leading with an example is good. It really depends on your learning style if you want to read this chapter first. After some theory there is a whole host of examples for you to try and figure out, all with both screenshots and code samples. This is a real codex of information, for me I found reading the first part and trying the second part with my own examples worked best.

Properties and Bindings

Binding is a simple concept, but there are some subtleties that need to be appreciated when building JavaFX applications. The first part of the chapter is split into titles of Understanding X, which makes it easy to refer back to later. Once again, everything is backed up with an appropriate example that demonstrates the power of bindings. This chapter is similar to the previous, the first part of the chapter is easy to read for theory but the latter is example heavy. I'd use the latter part of the chapter as a reference to come back to once you start writing your own code.

Building Dynamic UI Layouts in JavaFX

We used the concepts that are in the Reversi example for the foundations of our Java Memory Visualizer to manipulate memory cells that are on the screen. This example is again in the format of extending Application, but building out components on there. We built our application using FXML based on the information from this chapter. There is enough in this chapter to build your own board game, or if you are building any binding state UI I'd recommend reading this chapter.

Using the JavaFX UI Controls

I personally haven't read this chapter in great depth as I view it as a reference chapter. It looks like this is the codex for building your own controls.

Collections and Concurrency

In chapter 6 we're really going back to basics, so in my opinion this is not a chapter you can shelve for later. For the collections part, if you are familiar with Flex, ObservableList reminded me of ArrayCollection. The concurrency section introduces the threading model that is used in JavaFX, and that it is single threaded like most other UI frameworks. The JavaFX tasks are then explained for removing logic from the main application thread to avoid bloating your application re-render. Personally when we approached this, due to the nature of the simulation, we used plain old java.util.concurrent.

Creating Charts in JavaFX

Great overview into building charts in JavaFX and shows off the power of what is capable in the API. Examples and screenshots are available to get you started with what you are trying to build.

Using the Media Classes

Because of the type of work I do it's likely I'll never get to do any of the fun stuff in this chapter. I've saved this one for a rainy day, although on a quick parse it does look like a chapter I will genuinely come back to.

Accessing Web Services

An awesome chapter with practical examples of how you would integrate your JavaFX client with real world technologies. This sample walks you through creating a REST API call to twitter, using JAXB to create an appropriate response and display the information. If you are looking at using this for EE work this would be a good chapter to read. 

JavaFX Languages Markup

In this chapter we see some other languages using JavaFX (Scala and Groovy). The finally we get to the section on FXML. The sample is fairly simple and somewhat limited, it would have been good to have seen more of this earlier. 

Summary

This is an excellent book to have if you are trying to do anything with JavaFX. Although the online documentation is getting better, this book provides an awesome introduction to most components, tricks and tips that you would need to get started in this space. My only criticism of the book is a lack of FXML examples and that it only really appears at the end of the book. This shouldn't take away however from some of the excellent explanations and examples in this unfamiliar space. It's also worth noting that with a bit of playing around it's very simple to convert the samples over to using FXML. 

5/5 - I bought this with my own money and don't want it back :-). 

JavaFX Cleanly Closing an FXML Based Application

Whilst developing an FXML based JavaFX application for JavaOne I had to overcome the problem of cleaning up Java background threads running in the controller on a ExecutorService.

If you had chosen to use the builder patterns rather than FXML this is a fairly trivial task, as you are likely to have direct access to the controller from your main class which extends Application. However, when you use FXML you will more than likely be declaring your controller as follows:
 <VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.jclarity.anim.memory.MemoryController"> ...  

When you launch the FXML you are likely to do something along the lines of:

 @Override  
 public void start(Stage stage) throws Exception {  
    Parent root = FXMLLoader.load(getClass().getResource("MemoryMainView.fxml"));  
    Scene scene = new Scene(root, 600, 500);  
    scene.getStylesheets().add(getClass().getResource("Memory.css").toExternalForm());  
    stage.setScene(scene);  
    stage.setTitle("JavaFX Memory Visualizer");  
    stage.show();  
 }  

At this stage you launch the FXML and never grabbed hold of your reference to the controller. This now means when you override stop() you can't delegate this to your controller. The solution I found to this was to separate out the code above to ensure that you keep access to the FXML loader. If you have access to the FXMLoader you easily do the following in the stop method:
   @Override  
   public void stop() {  
     ((MemoryController) fxmlLoader.getController()).haltSimulation();  
   }  

I separated the code as follows:
 private FXMLLoader fxmlLoader;  

   @Override  
   public void start(Stage stage) throws Exception {  
     URL location = getClass().getResource("MemoryMainView.fxml");  
     fxmlLoader = new FXMLLoader();  
     fxmlLoader.setLocation(location);  
     fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());  
     Parent root = (Parent) fxmlLoader.load(location.openStream()); 
     ...
    }