PROBLEM STATEMENT
Create an application to demonstrate Bouncing Ball, create a menu for the above with 2 options: start and stop to pause and resume the bouncing ball.
CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BouncingBallMidlet extends MIDlet {
//Declaring BouncingBallCanvas object
private BouncingBallCanvas bbc;
//Declaring Display object
private Display display;
public BouncingBallMidlet()
{
//Initailizing BouncingBallCanvas object.
bbc = new BouncingBallCanvas(this);
}
public void startApp()
{
// Indicating the object to be Displayed
display = Display.getDisplay(this);
display.setCurrent(bbc);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void exitButton()
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
public class BouncingBallCanvas extends Canvas implements CommandListener, Runnable
{
BouncingBallMidlet bbm;
Display display;
Command quitCommand;
Command startCommand;
Command pauseCommand;
//Declaring Random’s object
Random rand;
//Declaring Thread’s object
Thread t;
int stopFlag;
int x, y;
int dx, dy;
int r, gr, b;
public BouncingBallCanvas()
{
}
public BouncingBallCanvas(BouncingBallMidlet bbm)
{
this.bbm = bbm;
// Indicating the object to be Displayed
display = Display.getDisplay(bbm);
//Initailizing Random’s object.
rand = new Random();
//Initailizing Command buttons
quitCommand = new Command("Quit", Command.EXIT, 0);
startCommand = new Command("Start", Command.SCREEN, 0);
pauseCommand = new Command("Pause", Command.STOP, 0);
//Adding Command to current form
addCommand(quitCommand);
addCommand(pauseCommand);
// Adding Command Listner
setCommandListener(this);
//Initailizing Thread’s object.
t = new Thread(this);
stopFlag = 0;
dx = 10;
dy = 10;
x = 8;
y = 12;
//Generating the Random number in the range of 0 to 255 and assigning it to the variable.
r = gr = b = Math.abs(rand.nextInt()%255);
//Starting the Thread t
t.start();
}
protected void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(r, gr, b);
g.fillArc(x , y, 20, 20, 0, 360);
if (x > getWidth()-23 || x < 8)
{
dx = -dx;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
if (y > getHeight()-21 || y < 12)
{
dy = -dy;
r = Math.abs(rand.nextInt()%255);
gr = Math.abs(rand.nextInt()%255);
b = Math.abs(rand.nextInt()%255);
}
x += dx;
y += dy;
}
//Condition to get and check the user input
Command getCurrentCommand()
{
switch(stopFlag)
{
case 0:
return pauseCommand;
case 1:
return startCommand;
default:
return startCommand;
}
}
public void commandAction(Command command, Displayabl displayable)
{
addCommand(getCurrentCommand());
if(command == quitCommand)
{
bbm.exitButton();
}
else if(command == startCommand)
{
t = new Thread(this);
stopFlag = 0;
this.removeCommand(startCommand);
addCommand(pauseCommand);
t.start();
}
else if(command == pauseCommand)
{
stopFlag = 1;
t = null;
this.removeCommand(pauseCommand);
addCommand(startCommand);
}
}
public void run()
{
for( ; ; )
{
try
{
repaint();
//Thread is sleep for 170 mill second
Thread.sleep(170);
if(stopFlag == 1)
break;
}
catch(InterruptedException ie)
{
}
}
}
}
No comments:
Post a Comment