Wednesday, November 18, 2009

To demonstrate “TIMER”

PROBLEM STATEMENT
Create an application to display text moving across the screen ad change the background along the moving text.

CODE

//MovingBannerMidlet.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MovingBannerMidlet extends MIDlet {

//Declaring object/s
MovingBannerCanvas mbc;
Display display;

public MovingBannerMidlet()
{
//Initailizing MovingBannerCanvas object.
mbc = new MovingBannerCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(mbc);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void exitButton()
{
// Notifying that the object is Destroyed
destroyApp(false);
notifyDestroyed();
}
}





//MovingBannerCanvas.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class MovingBannerCanvas extends Canvas implements CommandListener, Runnable {

//Declaring Objects and variables

MovingBannerMidlet mbm;
Display display;
Command cmdquit;
Command cmdstart;
Command cmdpause;
Random random;

String strmessage = " University Department of Information Technology ";


Thread t = null;
int stopFlag, x, y, z, x1, y1, z1;

public MovingBannerCanvas() { }

public MovingBannerCanvas(MovingBannerMidlet mbm)
{
this.mbm = mbm;

//Initailizing Objects and variables

display = Display.getDisplay(mbm);
cmdquit = new Command("Quit", Command.EXIT, 0);
cmdstart = new Command("Start", Command.SCREEN, 0);
cmdpause = new Command("Pause", Command.STOP, 1);
addCommand(cmdquit);
addCommand(cmdpause);

//Adding Command Listener

setCommandListener(this);

// initializing Random variable

random = new Random();

// initializing Thread to get process the current object
t = new Thread(this);
stopFlag = 0;

//Starting the execution of the thread process
t.start();
}
protected void paint(Graphics g)
{
g.setColor(x = Math.abs(random.nextInt()%255), y = Math.abs(random.nextInt()%255), z = Math.abs(random.nextInt()%255));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Math.abs(x1 = random.nextInt()%255), y1 = Math.abs(random.nextInt()%255), z1 = Math.abs(random.nextInt()%255));

if(x == x1 | y == y1 | z == z1)
{

g.setColor(Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255), Math.abs(random.nextInt()%255));
}
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE));
g.drawString(strmessage, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER );
}
Command getCurrentCommand(){

switch(stopFlag)
{
case 0:

return cmdpause;
case 1:

return cmdstart;
default:
return cmdstart;
}
}

public void commandAction(Command command, Displayable displayable)
{
addCommand(getCurrentCommand());
if(command == cmdquit)
{
bm.exitButton();
}
else if(command == cmdstart)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(cmdstart);
addCommand(cmdpause);
t.start();
}
else if(command == cmdpause)
{
stopFlag = 1;
t = null;
this.removeCommand(cmdpause);
addCommand(cmdstart);
}
}
public void run()
{
char ch;
for( ; ; )
{
try
{
Rpaint();
Thread.sleep(250);
ch = strmessage.charAt(0);
strmessage = strmessage.substring(1, strmessage.length());
strmessage += ch;
if(stopFlag == 1)
break;
} catch(InterruptedException ie) {
}
}
}
}

No comments:

Post a Comment