Tuesday, September 14, 2010

mc lab manual 4th expt

Experiment No:4

Experiment Name: MIDlet to create buffered stop watch.

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:

When we perform animations, we can first calculate the display content and then call repaint() in order to paint the new frame. But how do we know that the call to paint() has finished? One possibility would be to call serviceRepaints(), which blocks until all pending display updates are finished. The problem with serviceRepaints() is that paint() may be called from another thread. If the thread calling serviceRepaints() holds any locks that are required in paint(), a deadlock may occur. Also, calling serviceRepaints() makes sense only from a thread other than the event handling thread. Otherwise, key events may be blocked until the animation is over. An alternative to serviceRepaints() is calling callSerially() at the end of the paint() method. The callSerially() method lets we put Runnable objects in the event queue. The run() method of the Runnable object is then executed serially like any other event handling method. In the run() method, the next frame can be set up, and a new repaint can be requested there.

To demonstrate this execution model, we will build a simple stopwatch that counts down a given number of seconds by showing a corresponding pie slice using the fillArc() method. The Canvas implementation stores the current slice in degree, the start time, the total amount of seconds and the MIDlet display in local variables. In order to make use of callSerially(), our Canvas implements the Runnable interface. When the StopWatchCanvas is created, you store the given display and seconds. Then, the current time is determined and stored, too. In the paint() method, you clear the display. If you need to draw more than 0 degrees, you fill a corresponding arc with red color and request recalculation of the pie slice using callSerially(). Finally, you draw the outline of the stopwatch by setting the color to black and calling drawArc().This method is invoked by the event handling thread as a result of the previous display.callSerially(this) statement. In this case, it just calculates a new pie slice and requests a repaint().

As always, we need a MIDlet to actually display our StopWatchCanvas implementation.

Code For BufferedStopWatch.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

class BufferedStopWatchCanvas extends Canvas implements Runnable {

int degree = 360;

long startTime;

int seconds;

Display display;

Image offscreen;

BufferedStopWatchCanvas (Display display, int seconds) {

this.display = display;

this.seconds = seconds;

if (!isDoubleBuffered () && false)

offscreen = Image.createImage (getWidth (), getHeight ());

startTime = System.currentTimeMillis ();

}

public void paint (Graphics g) {

Graphics g2 = offscreen == null

? g

: offscreen.getGraphics ();

g2.setGrayScale (255);

g2.fillRect (0, 0, getWidth (), getHeight ());

if (degree > 0) {

g2.setColor (255, 0, 0);

g2.fillArc (0,0, getWidth (), getHeight (), 90, degree);

display.callSerially (this);

}

g2.setGrayScale (0);

g2.drawArc (0, 0, getWidth ()-1, getHeight ()-1, 0, 360);

if (offscreen != null)

g.drawImage (offscreen, 0, 0, Graphics.TOP | Graphics.RIGHT);

}

public void run () {

int permille = (int) ((System.currentTimeMillis ()

- startTime) / seconds);

degree = 360 - (permille * 360) / 1000;

repaint ();

}

}

public class BufferedStopWatch extends MIDlet {

public void startApp () {

Display display = Display.getDisplay (this);

display.setCurrent (new BufferedStopWatchCanvas (display, 10));

}

public void pauseApp () {

}

public void destroyApp (boolean forced) {

}

}


Conclusion : Thus we have successfully created Buffered stop watch.

No comments:

 

Free HTML Hit Counters

Locations of visitors to this page