Pular para o conteúdo principal

Porting Battleship Game to Android

 The final goal of creating a battleship game was porting it to Android so I can run it on my phone. I was not in the mood to do all configuration and remember how to do it. This changed when GluonHQ introduced start.gluon.io.

In this brief post I will share my steps porting the Battleship Game to JavaFX. 


Android application development


To take advantage of start.gluon.io I created a project and made it part of battleship maven project:



The I modified the original JavaFX app so I could use it in the Android ready app:

  • Decoupled the application setup from a pure desktop app: Instead doing setup on desktop main, I moved it to a separated class, so I could reuse the setup in mobile app;
  • Resize: Since I didn't know the target device size, I had to react to scene resize to redraw the game boards. This was easily accomplished by adding a default method in Screen interface, which is called when Scene is resized. Then screen can modify boards size accordingly.
I made more small changes and adaption, you can see the changes in this commit.

Pros


This is what I found great during battleship mobile app creation:

  • Speed: You can quickly develop, debug and test your application locally, don't have to wait a long time for compiling and things like that;
  • Compatibility: Everything we do in JavaFX seems to work on Android. In battleship we have transitions, use of CSS and Canvas, everything seems to work in my mobile phone. It means that charts, binding, animation and other cool JavaFX APIs can be used for Mobile applications as well;
  • Events: I used MouseEvent to retrieve where user clicks on boards, but in mobile phones we don't have mouse events, I didn't have to adapt mouse events, it just works!
  • Modern: Use modern Java 11;
  • Rich API: I noticed Gluon also provides great APIs to build apps, I didn't use it, but it is great to see that we could make use of some tested and mobile specific components.

Cons


  • ComboBox and ChoiceBox does not seem to work. That's the only thing that really upset me, but I don't think that combox/choicebox are mobile friendly;

Final result


I finished the app in a few hours, I just reused my code and get used to Gluon plugin, hence there's room for improvements. You can download the APK from my drive - you will have to trust me on this one - but you can also build from the sources. See some screenshots:

         


Conclusion

In this post I shared how I ported my JavaFX application to run on Android. That was a great experience, I really glad Gluon is working hard and I truly feel that Java WORA is true again. Thanks Johan, Jose Pereda and Gluon Engineering team for making it possible.


Source code: https://github.com/jesuino/battleship-game


Comentários

Postagens mais visitadas deste blog

Creating custom Work Item Handler in BPM Suite/jBPM 6

Hi all! In this post I am going to share my experience creating a work item handler for BPM Suite 6.0.3 (which is very similar to jbpm as explained in a previous post ). The Hello World Work Item Handler Let's create a really simple WorkItemHandler just to print "Hello World" in the console. Of course you can implement more feature and make it receive parameters, access database, web services, etc. But in this post we will keep it simple. Really simple. First thing to do is to create a maven project for my WorkItemHandler. Here's the pom.xml of my Maven project: Notice that we are importing jbpm dependency so we can find the java interface that is used to create work item handlers. Now we can start coding. Let's create a class name HelloWorkItemHandler in package org.jugvale.jbpm, here is the simple code: Now we can build our project and go to BPM Suite to configure it so we can use this workitem. Registering the WorkItemHandler UPDATE...

Using the REST Task from jBPM 6

In this post I am going to share my experience using the BPM Suite 6 REST Task. BPM Suite  X jBPM jBPM is a JBoss community project to offer an open source and efficient BPM Suite and BPM Suite is a product created by Red Hat based on jBPM. In this post I will be using BPM Suite because I have it installed already, but what I am going to described should work for jBPM as well.   REST Task  The REST Task is used to invoke a REST resource (or perform an HTTP Request) from your Business Process. You can set the URL, HTTP Method, timeout and credentials from the Process Modeller interface and when the process gets into that task, it will generate a HTTP Request and return the response as String. It is available on the left side pallet under the "Service Tasks" tab: The Target REST Web Service The service we are going to access from our process is a really simple one, that's why it is called simple-hello-rs and it is based on JBoss helloworld-rs qu...

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.