animstioncanvas.java

来自「上课自己制作的一个小的J2me程序」· Java 代码 · 共 66 行

JAVA
66
字号
import java.io.IOException;import javax.microedition.lcdui.Canvas;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** * * @author hp */public class AnimstionCanvas extends Canvas implements Runnable{    private Image[] img;    private int direction;    public AnimstionCanvas(){        try {            direction = 0;            img = new Image[4];            img[0] = Image.createImage("/image1.png");            img[1] = Image.createImage("/image2.png");            img[2] = Image.createImage("/image3.png");            img[3] = Image.createImage("/image4.png");        } catch (IOException ex) {            ex.printStackTrace();        }           }        protected void paint(Graphics g) {        g.setColor(0xffffff);        g.fillRect(0, 0, getWidth(), getHeight());                switch(direction){            case UP:                 g.drawImage(img[0], 0, 0, 0);                 break;            case DOWN:                 g.drawImage(img[1], 0, 0, 0);                 break;            case LEFT:                 g.drawImage(img[2], 0, 0, 0);                 break;            case RIGHT:                 g.drawImage(img[3], 0, 0, 0);                 break;                                   }    }    public void run() {        //throw new UnsupportedOperationException("Not supported yet.");    }     protected void keyPressed(int keyCode){  //(this.getGameAction(keyCode))这是将keycode转换为游戏按键下的按键数字代码         direction=this.getGameAction(keyCode);         repaint();//重新去执行paint这样就可以有效果实现了         }}

⌨️ 快捷键说明

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