2008年(3500)
分类:
2008-05-04 20:53:34
/×
×/
import javax.microedition.midlet.×;
import javax.microedition.lcdui.×;
public class canvasDemo4 extends MIDlet implements CommandListener
{
Display display;
//声明List清单
List list;
int viewType;
Command okCommand;
Command exitCommand;
canvasDraw canvasdraw;
public canvasDemo4()
{
canvasdraw=new canvasDraw();
display=Display.getDisplay(this);
list=new List("线条几何形状绘制清单", List.IMPLICIT);
okCommand = new Command("确认", Command.OK, 2);
exitCommand = new Command("离开", Command.EXIT, 2);
list.append("绘制线条", null);
list.append("绘制矩形", null);
list.append("绘制圆角矩形", null);
list.append("绘制弧形", null);
list.append("绘制三角形", null);
list.addCommand(okCommand);
list.addCommand(exitCommand);
list.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(list);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command cmd, Displayable disp)
{
//取得清单选取的list项目
viewType=list.getSelectedIndex();
if (cmd == okCommand)
{
display.setCurrent(canvasdraw);
}
else if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
class canvasDraw extends Canvas implements CommandListener
{
int gameaction=0;
Image handImage;
int changeSD=1;
int changeFull=0;
Command backCommand;
public canvasDraw()
{
try{
handImage=Image.createImage("/hand.png");
}catch(Exception ex){}
backCommand = new Command("返回", Command.BACK, 1);
addCommand(backCommand);
setCommandListener(this);
}
public void keyPressed(int keyCode)
{
gameaction=getGameAction(keyCode);
switch(gameaction)
{
//按左、右键选择线条状态
case LEFT:
changeSD=1;
break;
case RIGHT:
changeSD=3;
break;
//按上、下键选择是否填充形状
case UP:
changeFull=0;
break;
case DOWN:
changeFull=1;
break;
}
repaint();
}
public void paint(Graphics g)
{
//设置绘制颜色
g.setColor(0xffffff);
//绘制一个手机全屏幕填充颜色的矩形
g.fillRect(0,0,getWidth(),getHeight());
//设置下列显示画面的颜色
g.setColor(0);
//选择要显示图形是实线(DOLID)或虚线(DOTTED)
if(changeSD==1)
g.setStrokeStyle(Graphics.SOLID);
else
g.setStrokeStyle(Graphics.DOTTED);
//选择要绘制的种类
if(viewType==0)
{
g.drawString("绘制线条", getWidth()/2, 5, Graphics.HCENTER |
Graphics.TOP);
//绘制一条直线
g.drawLine(getWidth()/4,100,3×getWidth()/4,100);
}
else if(viewType==1){
g.drawString("绘制矩形", getWidth()/2, 5, Graphics.HCENTER |
Graphics.TOP);
if(changeFull==0)
//绘制一个矩形
g.drawRect(getWidth()/4,100,getWidth()/2,40);
else
g.fillRect(getWidth()/4,100,getWidth()/2,40);
}
else if(viewType==2){
g.drawString("绘制圆角矩形", getWidth()/2, 5, Graphics.HCENTER |
Graphics.TOP);
if(changeFull==0)
//绘制一个圆角矩形
g.drawRoundRect(getWidth()/4,100,getWidth()/2,40,30,40);
else
//绘制一个圆角填充矩形
g.fillRoundRect(getWidth()/4,100,getWidth()/2,40,30,40);
}
else if(viewType==3){
g.drawString("绘制弧形", getWidth()/2, 5, Graphics.HCENTER |
Graphics.TOP);
if(changeFull==0)
//绘制一个弧形形状
g.drawArc(getWidth()/3,100,40,50,0,150);
else
//绘制一个填充的弧形形状
g.fillArc(getWidth()/3,100,40,50,0,150);
}
else {
//绘制一个填充三角形形状
g.drawString("绘制三角形", getWidth()/2, 5, Graphics.HCENTER |
Graphics.TOP);
g.fillTriangle(50,80,80,60,100,120);
}
if(viewType!=4)
{
g.drawString("实线", getWidth()/4, 50, Graphics.HCENTER | Graphics.TOP);
g.drawString("虚线", 3×getWidth()/4, 50, Graphics.HCENTER | Graphics.TOP);
g.drawImage(handImage, changeSD×getWidth()/4, 60, Graphics.HCENTER |
Graphics.TOP);
}
//选择几何形状是否要填充
if(viewType!=0 && viewType!=4)
{
g.drawString("几何形状", getWidth()/2, 30, Graphics.HCENTER | Graphics.TOP);
g.drawString("填充几何形状", getWidth()/2, 70, Graphics.HCENTER |
Graphics.TOP);
g.drawImage(handImage, getWidth()/2, 40 changeFull×40, Graphics.HCENTER |
Graphics.TOP);
}
}
public void commandAction(Command cmd, Displayable disp)
{
//设定返回List清单
if (cmd == backCommand)
display.setCurrent(list);
}
}
}
运行结果如图6-15所示。
图6-15