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

📄 m3gcanvas.java

📁 这是有关J2ME 3D编程的源程序
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Transform;

public class M3GCanvas
extends GameCanvas
implements Runnable {
    // Thread-control
    boolean running = false;
    boolean done = true;
    
    // If the game should end
    public static boolean gameOver = false;
    
    // Rendering hints
    public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS | Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
    public static final int WEAK_RENDERING_HINTS = 0;
    public static int RENDERING_HINTS = STRONG_RENDERING_HINTS;
    
    // Key array
    boolean[] key = new boolean[5];
    
    // Key constants
    public static final int FIRE = 0;
    public static final int UP = FIRE + 1;
    public static final int DOWN = UP + 1;
    public static final int LEFT = DOWN + 1;
    public static final int RIGHT = LEFT + 1;
    
    // Global identity matrix
    Transform identity = new Transform();
    
    // Global Graphics3D object
    Graphics3D g3d = null;
    
    // The background
    Background back = null;
    
    // The global camera object
    Camera cam = null;
    
    // The particle system
    ParticleSystem ps = null;
    FountainEffect fx = null;
    
    /** Constructs the canvas
     */
    public M3GCanvas(int fps)
    {
        // We don't want to capture keys normally
        super(true);
        
        // We want a fullscreen canvas
        setFullScreenMode(true);
        
        // Load our camera
        loadCamera();
        
        // Load our background
        loadBackground();
        
        // Set up graphics 3d
        setUp();
    }
    
    /** Prepares the Graphics3D engine for immediate mode rendering by adding a light */
    private void setUp()
    {
        // Get the instance
        g3d = Graphics3D.getInstance();
        
        // Add a light to our scene, so we can see something
        g3d.addLight(createAmbientLight(), identity);
    }
    
    
    /** Creates a simple ambient light */
    private Light createAmbientLight()
    {
        Light l = new Light();
        l.setMode(Light.AMBIENT);
        l.setIntensity(1.0f);
        return l;
    }

    /** When fullscreen mode is set, some devices will call
     * this method to notify us of the new width/height.
     * However, we don't really care about the width/height
     * in this tutorial so we just let it be
     */
    public void sizeChanged(int newWidth, int newHeight)
    {
        
    }
    
    /** Loads our camera */
    private void loadCamera()
    {
        // Create a new camera
        cam = new Camera();
    }
    
    /** Loads the background */
    private void loadBackground()
    {
        // Create a new background, set bg color to black
        back = new Background();
        back.setColor(0);
    }

    /** Draws to screen
     */    
    private void draw(Graphics g)
    {
        // Envelop all in a try/catch block just in case
        try
        {            
            // Get the Graphics3D context
            g3d = Graphics3D.getInstance();
            
	        // First bind the graphics object. We use our pre-defined rendering hints.
	        g3d.bindTarget(g, true, RENDERING_HINTS);
	        
	        // Clear background
	        g3d.clear(back);
	        
	        // Bind camera at fixed position in origo
	        g3d.setCamera(cam, identity);
	        
	        // Init particles
	        if(ps == null)
	        {
	            fx = new FountainEffect(90);
	            ps = new ParticleSystem(fx, 20);
	        }
	        
	        // Emit the particles
	        ps.emit(g3d);
	        
	        // Check controls for fountain rotation
	        if(key[LEFT])
	            fx.setAngle(fx.getAngle() + 5);
	        if(key[RIGHT])
	            fx.setAngle(fx.getAngle() - 5);
	        
	        // Quit if user presses fire
	        if(key[FIRE])
	            TutorialMidlet.die();
        }
        catch(Exception e)
        {
            reportException(e);
        }
        finally
        {
            // Always remember to release!
            g3d.releaseTarget();
        }
    }

    /** Starts the canvas by firing up a thread
     */
    public void start() {
        Thread myThread = new Thread(this);
        
        // Make sure we know we are running
        running = true;
        done = false;
        
        // Start
        myThread.start();
    }
    
    /** Run, runs the whole thread. Also keeps track of FPS
     */
    public void run() {
        while(running) {
            try {                
                // Call the process method (computes keys)
                process();
                
                // Draw everything
                draw(getGraphics());
                flushGraphics();
                
                // Sleep to prevent starvation
                try{ Thread.sleep(30); } catch(Exception e) {}
            }
            catch(Exception e) {
                reportException(e);
            }
        }
        
        // Notify completion
        done = true;
    }
    
    /**
     * @param e
     */
    private void reportException(Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e);
        e.printStackTrace();
    }

    /** Pauses the game
     */
    public void pause() {}
    
    /** Stops the game
     */
    public void stop() { running = false; }
    
    /** Processes keys
     */
    protected void process()
    {
        int keys = getKeyStates();
        
        if((keys & GameCanvas.FIRE_PRESSED) != 0)
            key[FIRE] = true;
        else
            key[FIRE] = false;
        
        if((keys & GameCanvas.UP_PRESSED) != 0)
            key[UP] = true;
        else
            key[UP] = false;
        
        if((keys & GameCanvas.DOWN_PRESSED) != 0)
            key[DOWN] = true;
        else
            key[DOWN] = false;
        
        if((keys & GameCanvas.LEFT_PRESSED) != 0)
            key[LEFT] = true;
        else
            key[LEFT] = false;
        
        if((keys & GameCanvas.RIGHT_PRESSED) != 0)
            key[RIGHT] = true;
        else
            key[RIGHT] = false;
    }
    
    /** Checks if thread is running
     */
    public boolean isRunning() { return running; }
    
    /** checks if thread has finished its execution completely
     */
    public boolean isDone() { return done; }
}

⌨️ 快捷键说明

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