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

📄 mainclass.java

📁 J2ME手机游戏开发技术详解随书光盘
💻 JAVA
字号:
import javax.microedition.midlet.*;import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;import javax.microedition.io.*;import javax.microedition.media.*;import javax.microedition.media.control.*;import java.io.*;public class MainClass extends MIDlet{    private Display d;        public MainClass(){        d = Display.getDisplay(this);    }        public void startApp() {                d.setCurrent(new jsrCanvas(this));    }    public void pauseApp() {            }        public void destroyApp(boolean unconditional) {            }}class jsrCanvas extends GameCanvas implements Runnable{    private World world;    private Graphics3D g3d = null;    private Camera camera;    private Mesh mesh;    private boolean distance = false;    private MIDlet midlet;        public jsrCanvas(MIDlet m){        super(false);        midlet = m;                setFullScreenMode(true);        g3d = Graphics3D.getInstance();        world = new World();                camera = new Camera();        world.addChild(camera);        float w = getWidth();        float h = getHeight();        camera.setPerspective(60.0f, w / h, 0.1f, 1000f);        mesh = MeshCreator.createMesh();        mesh.translate(0.0f, 0.0f, -5.0f);        world.addChild(mesh);        world.setActiveCamera(camera);        Thread t = new Thread(this);        t.start();    }        public void keyPressed(int key){        switch(key){            case -6:                midlet.notifyDestroyed();                break;            case -7:                                distance ^= true;                if(distance){                    mesh.setTranslation(0.0f, 0.0f, -15.0f);                 }else{                    mesh.setTranslation(0.0f, 0.0f, -5.0f);                 }                                break;            case -11:                break;        }    }        public void run(){        Graphics g = getGraphics();        FPS fps = new FPS();                while(true){                      mesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);                        fps.calculateFps();            draw3D(g);            g.setColor(0xFF0000);            g.drawString(fps.fps_string, 0, 0, 0);            flushGraphics();        }    }    public void draw3D(Graphics g){        try{            g3d.bindTarget(g);            g3d.render(world);        }catch(Exception e){                    }finally{            g3d.releaseTarget();        }    }        }class MeshCreator{        public static Mesh createMesh(){        byte []coordinates = new byte[] {-1, 3, -1, //1        short []textures = new short[44];        int offset = 255/10;        int x = 0, y=0;       for(int i=0;i<44; i+=2){           x = x==0?255:0;           if(i>0 && x==255)               y+=offset;                      textures[i] = (short)x;           textures[i+1] = (short)y;                  }        byte []normals = new byte[] {-32, 96, -32,                                                                   int []stripLengths = new int[] {22};        int []strip = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};        VertexArray va = new VertexArray(coordinates.length/3, 3, 1);        va.set(0, coordinates.length/3, coordinates);        VertexArray no = new VertexArray(normals.length/3, 3, 1);        no.set(0, normals.length/3, normals);        VertexArray tx = new VertexArray(textures.length/2, 2, 2);        tx.set(0, textures.length/2, textures);                VertexBuffer vb = new VertexBuffer();        vb.setPositions(va, 1.0f, null);        vb.setNormals(no);        vb.setTexCoords(0, tx, (1.0f/255.0f), null);        TriangleStripArray tsa = new TriangleStripArray(strip, stripLengths);        PolygonMode pm = new PolygonMode();        pm.setShading(PolygonMode.SHADE_SMOOTH);        pm.setCulling(PolygonMode.CULL_NONE);        Material material = new Material();        material.setColor(Material.AMBIENT, 0xFFFF0000);        Appearance app = new Appearance();        app.setPolygonMode(pm);       app.setMaterial(material);        try{            Image texImg = Image.createImage("/texture.png"); // load the image            Texture2D texture = new Texture2D(new Image2D(Image2D.RGB, texImg)); // create texture from loaded image.            texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT); // repeat texture on surface            texture.setBlending(Texture2D.FUNC_DECAL); // Blend mode to use.            texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST); // Use nearest for performance, linear for quality            app.setTexture(0, texture);        }catch(Exception e){            System.out.println("Failed to create texture");        }                Mesh m;        m = new Mesh(vb, tsa, app);        m.setAppearance(0, app);        return m;    }}class FPS{     public static int fps = 10;    private int count_fps;    public String fps_string;    private long last;     public FPS(){        fps = 10;        count_fps = 0;    }     public void calculateFps(){        count_fps++;         long n = System.currentTimeMillis() / 1000;         if (n != last) {            fps = count_fps;            count_fps = 0;            fps_string = "Fps:" + fps;            last = n;        }    }}

⌨️ 快捷键说明

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