Wednesday, November 18, 2009

To Demonstrate Geometric Figure

PROBLEM STATEMENT
Create an application in j2me to demonstrate different events shapes, circle, square, rectangle, triangle on canvas.

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

public class practical8 extends MIDlet implements CommandListener
{

Display display;
public mycanvas can;
public practical8()
{
display=Display.getDisplay(this);
can=new mycanvas(this);
display.setCurrent(can);

}
public void startApp()
{
display.setCurrent(can);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
destroyApp(false);
notifyDestroyed();
}
public void finishp()
{
// Indicating the object to be Displayed


notifyDestroyed();
}


public void commandAction(Command c, Displayable d)
{

}
}
class mycanvas extends Canvas implements CommandListener
{
//declearing & Initailizing Command buttons

private Command cmdexit;
practical8 p;
int i;

String str1="Press 1 for Square";
String str2="Press 2 for Rectangle";
String str3="Press 3 for Traingle";
String str4="Press 4 for Circle";

public mycanvas(practical8 p)
{
this.p= p;

//Initailizing exit Command button

cmdexit=new Command("EXIT",Command.EXIT,1);
//Adding Command buttons to the Canvas

addCommand(cmdexit);

// Adding Command Listner
setCommandListener(this);
}

protected void paint (Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0xff000000);
g.drawString(str1,50,230,Graphics.BOTTOM|Graphics.LEF);
g.drawString(str2,50,241,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str3,50,252,Graphics.BOTTOM|Graphics.LEFT);
g.drawString(str4,50,263,Graphics.BOTTOM|Graphics.LEFT);


if(i==1)
{

g.setColor(0xff0000);
g.drawRect(getWidth()/4,getWidth()/4,getWidth()/4,
getHeight()/5);
repaint();
}
if(i==2)
{

g.setColor(0xff0000);
g.drawRect(getHeight()/4, getWidth()/2,(getHeight()/4)+2,
(getWidth()/2)+2);
repaint();
}
if(i==4)
{
g.setColor(0xff0000);
g.drawArc(getWidth()/4,getWidth()/4,getWidth()/4,
getWidth()/4,360,-360);
repaint();
}
if(i==3)
{
g.setColor(0xff0000);
g.drawLine(30,getHeight()/2,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/4,
getWidth()-50,getHeight()/2);
g.drawLine(30,getHeight()/2,30,getHeight()/4);
repaint();
}
}
public void commandAction(Command c, Displayable d)
{
if(c==cmdexit)
{
p.finishp();
}
repaint();
}
protected void keyPressed(int key)
{
try
{
switch ( key )
{
case KEY_NUM1:
i=1;
break;

case KEY_NUM2:
i=2;
break;

case KEY_NUM3:
i=3;
break;

case KEY_NUM4:
i=4;
break;
}
}
catch(Exception e){}
repaint();
}
}

No comments:

Post a Comment