PROBLEM STATEMENT
Create an application in j2me to demonstrate Different picture by pressing selected button.
CODE
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class practical7 extends MIDlet
{
//Declaring Display’s object
Display display;
//Declaring mycanvas’s object
public mycanvas can;
String s;
public practical7()
{
// Indicating the object to be Displayed
display=Display.getDisplay(this);
//Initailizing mycanvas object.
can=new mycanvas(this);
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void startApp()
{
// setting the mycanvas object to Display to get Displayed
display.setCurrent(can);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional)
{
// Indicating the object to be Displayed
destroyApp(false);
notifyDestroyed();
}
public void exitMIDlet()
{
// Indicating the object to be Displayed
destroyApp(true);
notifyDestroyed();
}
}
class mycanvas extends Canvas implements CommandListener
{
private Command exit;
private static Image image;
private practical7 prac7;
public mycanvas (practical7 prac7)
{
this.prac7 = prac7;
//Initailizing exit Command button
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
// Adding Command Listner
setCommandListener(this);
try
{
// create the image and assign it to the variable
image of the type Image
image = Image.createImage("/0.png");
}
catch(Exception ex)
{
//message get displayed if the image is not get assigned properly.
System.out.println("Image not found : " + ex);
}
}
protected void paint(Graphics g)
{
repaint();
// Drawing a WHTE Rectangle for the Background
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
try
{
//displaying the image at the center of the screen
g.drawImage(image, 100,100,g.TOP|g.HCENTER);
}
catch (Exception e){}
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
prac7.exitMIDlet();
}
}
protected void keyPressed(int key)
{
try
{
//capture user response when key pressed & accordingly display the images
switch ( key )
{
case KEY_NUM1:
image = Image.createImage("/1.png");
break;
case KEY_NUM2:
image = Image.createImage("/2.png");
break;
case KEY_NUM3:
image = Image.createImage("/3.png");
break;
case KEY_NUM4:
image = Image.createImage("/4.png");
break;
case KEY_NUM5:
image = Image.createImage("/5.png");
break;
case KEY_NUM6:
image = Image.createImage("/6.png");
break;
case KEY_NUM7:
image = Image.createImage("/7.png");
break;
case KEY_NUM8:
image = Image.createImage("/8.png");
break;
case KEY_NUM9:
image = Image.createImage("/9.png");
break;
case KEY_NUM0:
image = Image.createImage("/2.png");
break;
}
}
catch(Exception e){}
repaint();
}
}
No comments:
Post a Comment