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 {

        }
}
}
4

1 に答える 1

1

このコードを見てください...(私によるコメントとインデント)

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 ());
    }
  }
}

2 番目の問題については、おそらく何らかの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 の概念であり、先に進む前に改善する必要があると思います... 単なる提案です。

于 2011-02-21T12:09:33.817 に答える