navigate2canvas.java

来自「j2me的程序」· Java 代码 · 共 54 行

JAVA
54
字号
import javax.microedition.lcdui.*;

class Navigate2Canvas extends Canvas {

    int width = 0;
    int height = 0;
   private Image image;
    private int   newX=0;
    private int   newY=0;
    private int   stepX=0;
    private int   stepY=0;


    public Navigate2Canvas(Image image) {
        this.image=image;
        newX=0;
        newY=0;
        stepX = getWidth()/4;
        stepY = getHeight()/4; 
    }

    public void steppingXY(int x, int y) {
            newX += x;
            newY += y;
    }

    public void paint(Graphics g) {
        width=this.getWidth();
        height=this.getHeight();

        g.setGrayScale(255);
	g.fillRect(0,0,width-1,height-1);
	g.setGrayScale(0);
        g.drawRect(0,0,width-1,height-1);

        g.translate(newX, newY);
        g.drawImage(image,0,0, g.TOP|g.LEFT);
    }

    protected void keyPressed(int keyCode){
      int gameaction = getGameAction(keyCode);
      switch(gameaction){
       case UP:steppingXY(0,stepY);
               break;
       case DOWN:steppingXY(0,-stepY);
                break;
       case LEFT:steppingXY(stepX,0);
                break;
       case RIGHT:steppingXY(-stepX,0);
                break;
      }
      repaint();  
  }  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?