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

📄 examplecanvas.java

📁 这是j2me的例子,可供初学者学习,里面有源代码,是我写的
💻 JAVA
字号:
/*
 * ExampleCanvas.java
 *
 * Created on 2007年3月29日, 上午11:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.tan.ui.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Layer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author USER
 */
public class ExampleCanvas extends GameCanvas implements Runnable{
    
    private boolean isPlay;
    private long delay;//to give thread delay;
    private int currentX,currentY;//To hold current position of the "X","Y"
    private int width;
    private int height;//hold the screen x,y;
    private Sprite playerSprite;
    private Sprite backgroundSprite;
    private LayerManager layerManager;
    /** Creates a new instance of ExampleCanvas */
    public ExampleCanvas() throws Exception{
        super(true);
        width = getWidth();
        height = getHeight();
        currentX = getWidth()/2;
        currentY = getHeight()/2;
        delay = 20;
        Image playImage = Image.createImage("/transparent.png");
        playerSprite = new Sprite(playImage,32,32);
        Image backgroundImage = Image.createImage("/background.png");
        backgroundSprite = new Sprite(backgroundImage);
        layerManager = new LayerManager();
        layerManager.append(playerSprite);
        layerManager.append(backgroundSprite);
    }
    
    public void run() {
        Graphics g = getGraphics();
        while(isPlay == true) {
            input();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public void start() {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void stop() {
        isPlay = false;
    }
    
    private void input() {
        int keyStates = getKeyStates();
        playerSprite.setFrame(0);
        if((keyStates&LEFT_PRESSED)!=0){
            currentX = Math.max(0,currentX-1);
            playerSprite.setFrame(1);
        }
        if((keyStates&RIGHT_PRESSED)!=0)
            if(currentX+5<width){
            currentX = Math.min(width,currentX+1);
            playerSprite.setFrame(3);
            }
        if((keyStates&UP_PRESSED)!=0){
            currentY = Math.max(0,currentY-1);
            playerSprite.setFrame(2);
        }
        if((keyStates&DOWN_PRESSED)!=0){
            if(currentY+10<height){
                currentY = Math.min(height,currentY+1);
                playerSprite.setFrame(4);
            }
        }
    }
    
    private void drawScreen(Graphics g) {
        g.setColor(0xffff);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(0x0000ff);
        playerSprite.setPosition(currentX,currentY);
//        playerSprite.paint(g);
//        backgroundSprite.paint(g);
//        g.drawString("X",currentX,currentY,Graphics.TOP|Graphics.LEFT);
        layerManager.setViewWindow(55,20,140,140);
        layerManager.paint(g,20,20);
        flushGraphics();
    }
    
}

⌨️ 快捷键说明

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