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

📄 mygamecanvas.java

📁 SymbianOS J2ME一书的源码
💻 JAVA
字号:
/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.html
 */

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.IOException;


public class MyGameCanvas extends GameCanvas implements Runnable {
    private Command exit;
    private Helloworld midlet;
    private MySprite sprite;
    private LayerManager layerManager;
    private Thread thread;
    private boolean running;
    private final int SLEEP = 100;
    
    public MyGameCanvas(Helloworld midlet) throws IOException {
        super(true);
        this.midlet = midlet;
        
        sprite = createSprite();
        // initialize the layer manager
        layerManager = new LayerManager();
        // append the sprite to the layer manager
        layerManager.append(sprite);
    }
    
    public boolean isRunning(){return running;}
    
    synchronized void start() {
        running=true;
        thread=new Thread(this);
        thread.start();
    }
    
    public void run() {
        Graphics graphics=getGraphics();
        try {
            while (running) {
                draw(graphics);
                tick();
                Thread.sleep(SLEEP);
            }
        }
        catch(InterruptedException ie) {
            System.out.println(ie.toString());
        }
    }
    
    synchronized void stop() {
        running=false;
    }
    
    private void tick() {
        sprite.tick();
    }
    private void draw(Graphics g) {
        // calculate the centre of the screen based upon the
        // the images and canvas size
        int x= (getWidth()/2-sprite.getWidth()/2);
        int y= (getHeight()/2-sprite.getHeight()/2);
        
        // set and draw the back ground
        g.setColor(0,0,0);
        g.fillRect(0,0,getWidth(),getHeight());
        
        // paint the sprite on the screen
        layerManager.paint(g,x,y);
        flushGraphics();
        
    }
    
    private MySprite createSprite() {
        Image image=null;
        int height=0;
        int width=0;
        try {
            image = Image.createImage("/Splash.png");
            width = image.getWidth();
            height = image.getHeight() / MySprite.RAW_FRAMES;
        }
        catch(IOException io) {
            io.printStackTrace();
        }
        return new MySprite(image, width, height);
    }
}

⌨️ 快捷键说明

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