⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycanvas.java

📁 这是一个手机端的地图编辑器
💻 JAVA
字号:
package snake;
/*
 * myCanvas.java
 *
 * Created on 2007年11月26日, 下午3:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import javax.microedition.lcdui.*;
import java.io.*;
/**
 *
 * @author xiaoxin
 *  QQ:179908722
 *  msn:antigalaxy@hotmail.com
 */
public class myCanvas extends Canvas{
    //缓冲层
    private Image buffer;
    private Graphics bg;
    
    //视点
    private int showX;
    private int showY;
    
    public static int ScreenWidth;
    public static int ScreenHeight;
    
    private Map map;
    
    //地图
    /** Creates a new instance of myCanvas */
    public myCanvas() {
        ScreenWidth = getWidth();
        ScreenHeight = getHeight();
        buffer = Image.createImage(getWidth(), getHeight());
        bg = buffer.getGraphics();
        //读取地图文件
        try{
            map = new Map(Image.createImage("/res/1scrn.png"));
            DataInputStream dis = new DataInputStream(this.getClass().getResourceAsStream("/res/map.x"));
            map.loadMap(dis);
            dis.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        start();
    }
    
    boolean isLeft;
    boolean isRight;
    boolean isDown;
    boolean isUp;
    public void input(){
        if(isLeft){
            if(showX - 5 >= 0)
                showX -= 5;
        }
        
        if(isRight){
            if(showX + 5 < Map.tileWidth * Map.column - ScreenWidth)
                showX += 5;
        }
        
        if(isDown){
            
            if(showY + 5 < Map.tileHeight * Map.row - ScreenHeight)
                showY += 5;
        }
        
        if(isUp){
            if(showY - 5 >= 0)
                showY -= 5;
        }
        
    }
    
    
    
    public void start(){
        
        new Thread(){
            public void run(){
                while(true){
                    try{
                      
                        input();
                        render();
                        repaint();
                        serviceRepaints();
                      
                        Thread.sleep(100);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    
    
//绘制地图
    public void render(){
        
        map.render(bg, showX, showY);
    }
    
    public void paint(Graphics g){
        g.drawImage(buffer, 0, 0, 0);
    }
    
    protected void keyPressed(int keyCode){
        int gameAction = this.getGameAction(keyCode);
        switch(gameAction){
            case LEFT:
                isLeft = true;
                break;
            case RIGHT:
                isRight = true;
                break;
            case UP:
                isUp = true;
                break;
            case DOWN:
                isDown = true;
                break;
        }
    }
    
    protected void keyReleased(int keyCode){
        int gameAction = this.getGameAction(keyCode);
        switch(gameAction){
            case LEFT:
                isLeft = false;
                break;
            case RIGHT:
                isRight = false;
                break;
            case UP:
                isUp = false;
                break;
            case DOWN:
                isDown = false;
                break;
        }
    }
    
}

⌨️ 快捷键说明

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