Tuesday, September 14, 2010

mc lab manual 5th expt

Experiment No:5

Experiment Name: MIDlet to create Smiling Face Game.

Resources Required:

a . Equipments/Machines:

PIII 810 MHz, 20 GB HDD, 128 MB RAM, 1.44 FDD

14” color Monitor, 101 Keyboard, Serial mouse, LAN card

Dot matrix printer.

S/W : Sun Java Wireless Toolkit , Java

b . Consumables: Printer pages for printouts.

Theory:

For several reasons, the Canvas may move into the background—for example, if the display is set to another displayable object or if the device displays a system dialog. In these cases, the Canvas is notified by the hideNotify() method. When the Canvas becomes visible (again), the corresponding counterpart, showNotify(), is called. Before you begin development, let us first say a few words about the Javagochi itself. A Javagochi has a weight that is initialized with its IDEAL_WEIGHT. It also owns an instance of Display, Face, and Consumption, which will be explained later. Finally, it stores a score value for the care the owner spends on the Javagochi.The happiness of the Javagochi is determined by the deviation of its current weight from the ideal weight, ranging from 10 to 0.This formula also demonstrates how to circumvent problems with the absence of floating point arithmetic. In order to avoid loss of significant fractions, the values are scaled up before division.Like all other known life forms, the Javagochi can die. Javagochies only die from sadness when their happiness level reaches zero.

The only other action a Javagochi can perform besides dying is to transform energy to matter and back. Since a weight change may change the Javagochi's look, a repaint is requested in the transform() method.In many cases, it is a good idea to scale displayed graphics depending on the actual screen size. Otherwise, the display will look nice on one particular device type, but won't fit the screen on devices with a lower screen resolution or become unnecessarily small on devices with higher screen resolutions. We will now show how scaling works for the Javagochi example. A picture of a Javagochi is shown in Figure 3.19. You will start by drawing the shape of the face, a simple ellipse. In this case, the ellipse will reflect the Javagochi's weight. If the Javagochi is at its ideal weight, the ellipse becomes a circle.

Code For Javagochi.java

import java.util.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

class Consumption extends TimerTask {

Javagochi javagochi;

public Consumption (Javagochi javagochi) {

this.javagochi = javagochi;

}

public void run () {

javagochi.transform (-1 - javagochi.score/100 );

}

}

class KeyConfirmer extends TimerTask {

Face face;

public KeyConfirmer (Face face) {

this.face = face;

}

public void run () {

face.keyConfirmed ();

}

}

class Face extends Canvas {

public static final String[] keys = {"abc", "def", "ghi", "jkl",

"mno", "pqrs", "tuv", "wxyz"};

Javagochi javagochi;

Timer keyTimer;

int keyMajor = -1;

int keyMinor;

char needed = 'a';

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;

g.translate (getWidth () / 2, getHeight () / 2);

g.setColor (255, 255, 255 - javagochi.getHappiness () * 25);

g.fillArc (- width / 2, - height / 2, width, height, 0, 360);

g.setColor (0, 0, 0);

g.drawArc (- width / 2, - height / 2, width, height, 0, 360);

g.drawString ("Score: "+javagochi.score, 0, -getHeight ()/2,

Graphics.TOP|Graphics.HCENTER);

String keySelect = "";

if (keyMajor != -1) {

String all = keys [keyMajor];

keySelect = all.substring

(0, keyMinor) + "[" + all.charAt (keyMinor)

+ "]" + all.substring (keyMinor+1);

}

g.drawString ("Feed: " + needed + " " + keySelect,

0, getHeight ()/2, Graphics.BOTTOM|Graphics.HCENTER);

drawEye (g, - width / 6, - height / 5, height / 15 + 1);

drawEye (g, width / 6, - height / 5, height / 15 + 1);

switch (javagochi.getHappiness () / 3) {

case 0:

case 1:

g.drawArc (-width/6, height/7, width/3, height/6, 0, 180);

break;

case 2:

g.drawLine (-width/6, height/7, width/6, height/7);

break;

default:

g.drawArc (-width/6, height/7, width/3, height/6, 0, -180);

}

}

void drawEye (Graphics graphics, int x0, int y0, int w) {

if (javagochi.isDead ()) {

graphics.drawLine (x0 - w/2, y0, x0 + w/2, y0);

graphics.drawLine (x0, y0 - w/2, x0, y0 + w/2);

}

else

graphics.fillArc (x0-w/2, y0-w/2, w, w, 0, 360);

}

public synchronized void keyPressed (int keyCode) {

int index = keyCode - KEY_NUM2;

if (keyTimer != null) keyTimer.cancel ();

if (index <> keys.length)

keyMajor = -1;

else {

if (index != keyMajor) {

keyMinor = 0;

keyMajor = index;

}

else {

keyMinor++;

if (keyMinor >= keys [keyMajor].length ())

keyMinor = 0;

}

keyTimer = new Timer ();

keyTimer.schedule (new KeyConfirmer (this), 500);

}

repaint ();

}

synchronized void keyConfirmed () {

if (keyMajor != -1) {

if (keys [keyMajor].charAt (keyMinor) == needed) {

javagochi.score += javagochi.getHappiness ();

if (!javagochi.isDead ())

needed = (char) ('a'

+ ((System.currentTimeMillis () / 10) % 26));

javagochi.transform (10);

}

keyMajor = -1;

repaint ();

}

}

}

public class Javagochi extends MIDlet {

static final int IDEAL_WEIGHT = 100;

Display display;

Face face = new Face (this);

int weight = IDEAL_WEIGHT;

Timer consumption;

int score;

public int getHappiness () {

int happiness = 20 - (weight > IDEAL_WEIGHT

? 10 * weight / IDEAL_WEIGHT

: 10 * IDEAL_WEIGHT / weight);

if (happiness < happiness ="">

else if (happiness > 10) happiness = 10;

return happiness;

}

public boolean isDead () {

return getHappiness () == 0;

}

public void transform (int amount) {

if (!isDead ()) {

weight += amount;

face.repaint ();

}

}

public void startApp () {

display = Display.getDisplay (this);

display.setCurrent (face);

consumption = new Timer ();

consumption.scheduleAtFixedRate (new Consumption (this), 500, 500);

}

public void pauseApp () {

consumption.cancel ();

}

public void destroyApp (boolean forced) {

}

}



Conclusion : Thus we have successfully created MIDlet for smiling face game.

No comments:

 

Free HTML Hit Counters

Locations of visitors to this page