Creating Cross-Platform Java and JavaScript Games with Amino
- Low-Level Drawing APIs
- Understanding the Amino APIs
- Drawing Binary Clock Elements
- Summary
In this article, we'll create a simulation using Amino, a cross-platform library for Java and JavaScript. The application we'll create is a binary clock, which uses cells or squares to show the time, with each digit being represented by a vertical column of cells. Our binary clock will use colored rectangles to indicate the binary values, red for 0 and green for 1. Because we'll need to represent six numbers, and binary format is more verbose than decimal format, we'll store the binary values in columns starting with 2^0(1) up to a maximum of 2^3(8). Figure 1 shows a column with its cells labeled representing the value 1.
Figure 1 Binary clock detail.
Low-Level Drawing APIs
Before we talk about Amino specifics, let's briefly consider the APIs on which Amino runs: the HTML5 Canvas and Java2D.
The HTML5 canvas is an immediate-mode drawing API and doesn't keep track of what was drawn to it after drawing is complete. One way to deal with this issue is to create wrapper classes around the Canvas APIs and execute drawing on every single frame. Another means is to use a scene graph[md]a structure, usually a tree, that allows applications to draw themselves onscreen more efficiently. If we were writing a game that had many actors in the scene, and the player's character was in a certain sector of the screen, we could use the scene graph to inspect only the portion of enemies that might be close to the player. SVG uses a similar technique for its drawing.
Java2D is a 2D drawing API capable of rendering shapes, images, and text using an API in Java. Due to its nature as a core Java Standard Edition API, Java2D is almost guaranteed to be present in most installations, and it can be called from any language that runs on the Java Virtual Machine, such as JRuby or Scala. Java2D supports TrueType fonts and several image formats (GIF, JPEG, PNG, BMP). If those built-in types aren't sufficient, you could use a third-party library. Java2D most closely correlates to the HTML5 Canvas API. Amino provides a common API addressing a subset of features from both, enabling you to port code from one to the other with little difficulty. Choose Java if the app needs to run on a desktop outside the browser.