【发布时间】:2011-02-20 00:02:30
【问题描述】:
我正在尝试制作一个使用 midlet 作为菜单的程序。当从菜单访问某个命令时,它将访问画布(如在弹出按钮中选择填充矩形和填充弧)。如果我选择 fillrectangle,它将访问绘制填充矩形的画布。
问题是当我访问 fillarc 时没有任何反应,但在 fillrectangle 上它会发生。
另一个问题是我不知道如何将菜单中的X和Y坐标应用到fillrectangle中,以便用户控制所选对象的位置。 '
这是我的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Nico
*/
public class emp extends MIDlet implements CommandListener {
Display display;
Form frm = new Form ("Main");
ChoiceGroup Cg1;
ChoiceGroup Cg2;
TextField tfX = new TextField ("X Axis"," ",40,TextField.ANY);
TextField tfY = new TextField ("Y Axis"," ",40,TextField.ANY);
Command OK;
public emp () {
OK = new Command ("OK",Command.OK,1);
Cg2 = new ChoiceGroup("Color", Choice.POPUP);
Cg1 = new ChoiceGroup("Type", Choice.POPUP);
Cg1.append("Rectangle", null);
Cg1.append("Arc", null);
Cg1.append("Line", null);
Cg2.append("Red", null);
Cg2.append("Blue", null);
Cg2.append("Green", null);
frm.append(Cg1);
frm.append(tfX);
frm.append(tfY);
frm.append(Cg2);
frm.addCommand(OK);
frm.setCommandListener(this);
}
public void startApp () {
display = Display.getDisplay(this);
display.setCurrent(frm);
}
public void pauseApp () {}
public void destroyApp (boolean forced) {}
class DrawingRect extends Canvas implements CommandListener {
Command Bk;
public DrawingRect (){
this.addCommand(Bk= new Command("Back", Command.BACK, 0 ) );
this.setCommandListener(this);
}
public void paint (Graphics g) {
int x1=100,y1=100;
g.setColor (0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor (0, 0, 255);
g.fillRect(x1,y1, 50, 50);
g.drawString("aaaaaa", getWidth()/2, getHeight()/2, Graphics.TOP|Graphics.HCENTER);
}
public void commandAction(Command c, Displayable d) {
if (c==Bk){
display.setCurrent(frm);
}
}
}
public class DrawingArc extends Canvas implements CommandListener {
Command Bk;
public DrawingArc (){
this.addCommand(Bk= new Command("Back", Command.BACK, 0 ) );
this.setCommandListener(this);
}
public void paint (Graphics g) {
g.setColor (0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setGrayScale(13*16);
g.fillArc(0,0,getWidth(),getHeight(),90,360);
}
public void commandAction(Command c, Displayable d) {
if (c==Bk){
display.setCurrent(frm);
}
}
}
public void commandAction(Command c, Displayable d) {
if (c==OK) {
int select = Cg1.getSelectedIndex();
if (select==0){
display.setCurrent (new DrawingRect ());
}
}
else if (c==OK){
int select = Cg1.getSelectedIndex();
if (select==1){
display.setCurrent (new DrawingArc ());
}
}
else {
}
}
}
【问题讨论】:
-
只是一个评论,你的画布应该是你的 midlet 的一部分。
-
另一条评论,大多数 IDES 都有一个“格式化”工具,它确实可以帮助其他人理解您的代码(特别是如果您向其他人寻求帮助)。此外,如果您想improve your coding style,我建议您阅读此内容。