Java Reference
In-Depth Information
In order to leave some space for the Javagochi to grow, the diameter of the ideal circle is half the
minimum of the screen width and height. Thus, the height of the Javagochi is calculated using the
following formula:
int height = Math.min (getHeight(), getWidth()) / 2;
Based on the current weight, the ideal weight, and the calculated height, which is also the diameter of
the "ideal" Javagochi , you can now calculate the width of the Javagochi :
int width = height * javagochi.weight / javagochi.IDEAL_WEIGHT;
Other applications may of course have other dependencies from the actual screen size, but this example
should be sufficient to show the general idea.
The Javagochi 's skin color is dependent on its happiness. If the Javagochi feels well, its skin has
a bright yellow color. With decreasing happiness, the Javagochi becomes pale. This is reflected by
the following setColor() command:
setColor (255, 255, 28 * javagochi.happiness);
Using the given width and height, you can now implement your first version of the Javagochi 's
Face class:
import javax.microedition.lcdui.*;
class Face extends Canvas implements CommandListener {
Javagochi javagochi;
Face (Javagochi javagochi) {
this.javagochi = javagochi;
}
public void paint (Graphics g) {
g.setColor (255, 255, 255);
g.fillRect (0, 0, getWidth(), getHeight());
int height = Math.min (getHeight(), getWidth()) / 2;
int width = height * javagochi.weight /
javagochi.IDEAL_WEIGHT;
Search WWH ::




Custom Search