本文介绍了来自Canvas问题的GameCanvas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这段代码,成功切换了Form< ==> Canvas,但是如何从此Midlet制作GameCanvas?
(我使用GameCanvas的原因有两个:按住键可移动光标,并在需要时轻松访问Graphics)
请帮忙.

I found this code, that successfully switching Form<==>Canvas, but how to made GameCanvas from this midlet ?
(I''m using GameCanvas for 2 reasons: moving cursor by holding key, and easily access to Graphics when I want it)
Please, help.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class CanvasForm extends MIDlet implements CommandListener{
  private Display display;
  private Form form;
  private Displayable current;
  private TextField one, two;
  private StringItem item;
  private long result;
  private Command quit, add ;
  private CanvasClass canvas;
  int first=0, second=0;

  public CanvasForm(){
    form = new Form("Calculator");
    one = new TextField(null, "10000002", 8, TextField.NUMERIC );
    two = new TextField(null, "10000003", 8, TextField.NUMERIC );
    item = new StringItem("Result", "");
    quit = new Command("Quit", Command.EXIT, 0);
    add = new Command("Add", Command.SCREEN, 0);
    form.append(one);
    form.append(two);
    form.append(item);
    form.addCommand(add);
    form.addCommand(quit);
    form.setCommandListener(this);
  }

  public void startApp(){
    display=Display.getDisplay(this);
    if (current!=null)
      display.setCurrent(current);
    else{
      display.setCurrent(form);
      current=form;
    }
  }

  public void pauseApp(){}

  public void destroyApp(boolean b){}

  private void calculate(){
    try {
      first = Integer.parseInt( one.getString() );
      second = Integer.parseInt( two.getString() );
      result = first + second ;
      item.setText( result + "" );
    } catch (NumberFormatException e){
      item.setText("unknown");
      e.printStackTrace();
    }
  }

  public void commandAction(Command c, Displayable s){
    if (c == quit){
      notifyDestroyed();
      return;
    }
    calculate();
    if (canvas==null) canvas= new CanvasClass() ;
    current=canvas;
    display.setCurrent(canvas);
  }

  class CanvasClass extends Canvas implements CommandListener{
    CanvasClass(){
      this.addCommand( new Command("Back", Command.BACK, 0 ) );
      this.setCommandListener(this);
    }

      protected void keyPressed(int keyCode) {
        if (keyCode > 0) {
          System.out.println("keyPressed " + ((char) keyCode));
        } else {
          System.out.println("keyPressed action " + getGameAction(keyCode));
        }
      }
    protected void paint(Graphics g){
      int w = getWidth();
      int h = getHeight();
      g.setColor(244,244,244);
      g.fillRect( 0,0,w,h );
      g.setGrayScale(12*14);
      h = Math.min( w, h );

      long mf = 100000000;
      int angle = (int)(( (first*(36000*mf/result)) +50*mf)/(100*mf)) ;
      int origin=180;
      g.fillArc(0,0,h,h, origin, (int)angle);
      g.setGrayScale(13*16);
      g.fillArc(0,0,h,h, (int)(origin+angle), (int)(360-angle) );

      g.setColor(123);
      g.drawString("A = "+first+"  ", h/2, h/2-10, Graphics.BASELINE|Graphics.RIGHT);
      g.drawString("  B = "+second, h/2, h/2-10, Graphics.BASELINE|Graphics.LEFT);
      g.drawString("Total = "+(first+second), h/2, h/2, Graphics.TOP|Graphics.HCENTER);

    }

    public void commandAction( Command c, Displayable d){
      current=form;
      display.setCurrent(form);
    }
  }
}

推荐答案

class CanvasClass extends Canvas implements CommandListener{




with

class CanvasClass extends GameCanvas implements CommandListener{


稍后可能还有其他一些事情需要清理,但这将为您提供一个开始. GameCanvas引入的额外"行为不是很多.

请记住对答案进行投票,并根据需要将其标记为接受.


There might be a few other things to clean up later, but this will give you a start. The "extra" behaviour introduced by GameCanvas is not very much.

Remember to vote for the answer, and mark it accepted if you like it.


这篇关于来自Canvas问题的GameCanvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:03