JavaMe MIDlet 和 Canvas

发布于 2024-10-18 00:33:38 字数 3258 浏览 1 评论 0原文

我正在尝试制作一个使用 midlet 作为菜单的程序。当从菜单访问某些命令时,它将访问画布(就像在弹出按钮中选择 fillrectangle 和 fillarc 一样)。如果我选择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 {

        }
}
}

I'm trying to make a program that uses midlet for the menu. When certain command accessed from the menu,it will access the canvas (like in a popup button a selection of fillrectangle and fillarc). If I select the fillrectangle it will access the canvas that draws the fill rectangle.
The problem is when I access the fillarc nothing happens, but on the fillrectangle it does.

Another problem is that I don't know how to apply the X and Y coordinates in the menu to the fillrectangle for the user to control the position of the selected object. '

Here is my code:

/*
 * 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 {

        }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-10-25 00:33:38

看看这段代码...(我的评论和缩进)

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){
    // It will never ever ever reach this block
    int select = Cg1.getSelectedIndex();
    if (select==1){
      display.setCurrent (new DrawingArc ());
    }
  }else {
    // WTF?
  }
}

我并不是要粗鲁,但是这段代码不太稳定...您应该澄清您的 java me 概念或尝试澄清您的代码...
我相信,如果您更改为下面的代码,您可以解决第一个问题:

public void commandAction(Command c, Displayable d) {
  if (c==OK) {
    int select = Cg1.getSelectedIndex();
    if (select==0){
      display.setCurrent (new DrawingRect ());
    }else if (select==1){
      display.setCurrent (new DrawingArc ());
    }
  }
}

对于第二个问题,我可能会在表单中添加某种 Move to... 按钮。如果你按下它,你可以用给定的 X 和 Y 创建画布,或者......下面有一些提示:

class DrawingRect extends Canvas implements CommandListener {
  Command Bk;
  int x1, y1;

  public DrawingRect (int newX, int newY){
    this.addCommand(Bk= new Command("Back", Command.BACK, 0 ) );
    this.setCommandListener(this);
    x1 = newX;
    y1 = newY;
  }


  public void paint (Graphics g) {
    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);
    }
  }
}

显然,像这样调用它:

display.setCurrent (new DrawingRect(tfX.getString(),tfY.getString()));

但是,所有这些都是非常基本的 Java 概念,我认为你应该在之前改进它们进一步...只是一个建议。

Have a look to this piece of code... (comments and indents by me)

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){
    // It will never ever ever reach this block
    int select = Cg1.getSelectedIndex();
    if (select==1){
      display.setCurrent (new DrawingArc ());
    }
  }else {
    // WTF?
  }
}

I don't mean to be rude, but this code is not very understable... you should clarify your java me concepts or try to clarify you code...
I believe if you change to the code below, you could solve the first problem:

public void commandAction(Command c, Displayable d) {
  if (c==OK) {
    int select = Cg1.getSelectedIndex();
    if (select==0){
      display.setCurrent (new DrawingRect ());
    }else if (select==1){
      display.setCurrent (new DrawingArc ());
    }
  }
}

For the second problem, I would probably add some sort of Move to... button to the form. If you press it, you can create the canvas with given X and Y, or... Some hint below:

class DrawingRect extends Canvas implements CommandListener {
  Command Bk;
  int x1, y1;

  public DrawingRect (int newX, int newY){
    this.addCommand(Bk= new Command("Back", Command.BACK, 0 ) );
    this.setCommandListener(this);
    x1 = newX;
    y1 = newY;
  }


  public void paint (Graphics g) {
    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);
    }
  }
}

And obviously, calling it like this:

display.setCurrent (new DrawingRect(tfX.getString(),tfY.getString()));

But, all of these are very basic Java concepts, I think you should improve them before going any further... Just a suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文