Contoh Program Bentuk Geometri
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
public class BentukGeometri extends MIDlet implements CommandListener
{
private Display display;
private List list;
private Canvas canvas;
private final Command cmdExit = new Command("Exit", Command.EXIT, 1);
private final Command cmdOK = new Command("OK", Command.OK, 1);
private final Command cmdBack = new Command("Back", Command.BACK, 1);
public BentukGeometri()
{
display = Display.getDisplay(this);
list = new List("Bentuk Geometri", Choice.EXCLUSIVE);
list.append("Segiempat", null);
list.append("Kerucut", null);
list.append("Lingkaran", null);
list.append("Setengah Lingkaran", null);
list.append("Elips", null);
list.addCommand(cmdExit);
list.addCommand(cmdOK);
list.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(list);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{
if (c == cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmdBack)
{
display.setCurrent(list);
}
else
{
switch (list.getSelectedIndex())
{
case 0 :
{
canvas = (Segiempat) (new Segiempat(this));
break;
}
case 1 :
{
canvas = (Kerucut) (new Kerucut(this));
break;
}
case 2 :
{
canvas = (Lingkaran) (new Lingkaran(this));
break;
}
case 3 :
{
canvas = (SetengahLingkaran) (new SetengahLingkaran(this));
break;
}
case 4 :
{
canvas = (elips) (new elips(this));
break;
}
}
canvas.addCommand(cmdBack);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}
}
class Segiempat extends Canvas
{
private BentukGeometri midlet;
public Segiempat(BentukGeometri midlet)
{
this.midlet = midlet;
}
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawRect(55, 55, 60, 60);
g.setColor(255, 0, 0);
g.fillRect(56, 56, 59, 59);
}
};
class Kerucut extends Canvas
{
private BentukGeometri midlet;
public Kerucut(BentukGeometri midlet)
{
this.midlet = midlet;
}
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawArc(60, 60, 60, 150, 60, 60);
g.setColor(0, 255, 0);
g.fillArc(60, 60, 60, 150, 60, 60);
}
};
class Lingkaran extends Canvas
{
private BentukGeometri midlet;
public Lingkaran(BentukGeometri midlet)
{
this.midlet = midlet;
}
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 255);
g.fillArc(55, 55, 60, 60, 0 , 360);
}
};
class SetengahLingkaran extends Canvas
{
private BentukGeometri midlet;
public SetengahLingkaran(BentukGeometri midlet)
{
this.midlet = midlet;
}
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 255);
g.fillArc(55, 55, 60, 60, 0 , 180);
}
};
class elips extends Canvas
{
private BentukGeometri midlet;
public elips(BentukGeometri midlet)
{
this.midlet = midlet;
}
public void paint(Graphics g)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawRoundRect(40, 40, 40, 80, 80 , 80);
g.setColor(255, 0, 0);
g.fillRoundRect(40, 40, 40, 80, 80 , 80);
}
};
}
Posting Komentar