JavaFX Magic 8 Ball

I was working on a Magic 8 ball written in JavaFX just to try a few things out, and people asked for the code, so here it is. It is nothing special, although I did try to comment the relevant code to make it a bit easier to understand. At around 150 lines of code (including whitespace and comments), it is a very small program, and a good example of what is possible with JavaFX.

To shake the magic 8 ball, you do that – you click on it and shake the ball around. Once you let go the message is updated.

Part of the reason I’ve posted this is so that people can tell me how to improve my code. If I’ve done some stupid (likely derived from my life as a Java developer), please let me know.

You can (hopefully) run the Java Web Start version of it here:

A screenshot of the program is shown here:
magic8

When the ball is shaken, the text and the triangle fade out, and then the triangle fades in with a new message.

A source code download (along with the background image), is available from here. I know I could have coded the ball in JavaFX, but frankly using an image is far easier 🙂

Finally, here is the code. Apologies for the need for horizontal scrolling, but it is the best I can do without changing the theme of this site to be wider.

9 thoughts on “JavaFX Magic 8 Ball”

    1. @tbee: The homework for the reader is to create the background in JavaFX 🙂 I was rushing to finish the demo with only a tiny amount of time available to do it in. In reality to create the ball all in JavaFX wouldn’t be difficult, although scaling the magic 8 ball app to work on a mobile phone would probably provide other issues, particularly around displaying the text (it’ll be too small).

      Make a mobile version, using the code in this post as a starting point. It should be an interesting experiment.

  1. Not able to offer any help here but I have a question. I was curious if I could modify the code and recompile it myself. I am a complete noob here and do not know what would be required to do so. Any help? I simply want to change the sayings that the ball produces for a joke…

    1. Sure – you’ll be best served to download NetBeans with the JavaFX SDK and plugin, then simply download the linked source code and load it up in NetBeans.

      Good luck, and have fun! 🙂

  2. Nice! I changed the code, got it compiled and got it running with my custom comments. Now I need to figure out how to make something I can email to people to run on their windows machines or perhaps something I can put in a web page or something! This is cool. Any advice on where to go from here? A link to a tutorial about how to export and use the project?

    1. Steal the jnlp link from my page and edit it to point to your own webserver, then upload the jar file generated by NetBeans (which is in your projects ‘dist’ folder).

  3. Hey there I was just wondering is it possible to have it play a specific sound when shaken. I am creating one for some friends of mine who I play World of Warcraft with. I want to make it a magic 8 ball the decides what raid our guild will be doing. How ever seeing as the text display is random is it possible to make it so that a certain sound belongs to a certain text yet the text is randomly generated. So lets say I shake it and get a response that says “Maybe” the sound played would say “maybe” but if i shake again and it says “ask later” the sound says “ask later”. I guess I am just asking is it possible to match the sound with the text so they always match each other.
    I wouldn’t want to get a text response of “maybe” and a sound response of “ask later”

Leave a Reply