MainClass.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.midlet.*;
  2. import javax.microedition.midlet.MIDlet;
  3. import javax.microedition.lcdui.*;
  4. import javax.microedition.lcdui.game.*;
  5. import javax.microedition.m3g.*;
  6. import javax.microedition.io.*;
  7. import javax.microedition.media.*;
  8. import javax.microedition.media.control.*;
  9. import java.io.*;
  10. public class MainClass extends MIDlet{
  11.     private Display d;
  12.     
  13.     public MainClass(){
  14.         d = Display.getDisplay(this);
  15.     }
  16.     
  17.     public void startApp() {
  18.         
  19.         d.setCurrent(new jsrCanvas(this));
  20.     }
  21.     public void pauseApp() {
  22.         
  23.     }
  24.     
  25.     public void destroyApp(boolean unconditional) {
  26.         
  27.     }
  28. }
  29. class jsrCanvas extends GameCanvas implements Runnable{
  30.     private World world;
  31.     private Graphics3D g3d = null;
  32.     private Camera camera;
  33.     private Mesh mesh;
  34.     private boolean distance = false;
  35.     private MIDlet midlet;
  36.     
  37.     public jsrCanvas(MIDlet m){
  38.         super(false);
  39.         midlet = m;
  40.         
  41.         setFullScreenMode(true);
  42.         g3d = Graphics3D.getInstance();
  43.         world = new World();
  44.         
  45.         camera = new Camera();
  46.         world.addChild(camera);
  47.         float w = getWidth();
  48.         float h = getHeight();
  49.         camera.setPerspective(60.0f, w / h, 0.1f, 1000f);
  50.         mesh = MeshCreator.createMesh();
  51.         mesh.translate(0.0f, 0.0f, -5.0f);
  52.         world.addChild(mesh);
  53.         world.setActiveCamera(camera);
  54.         Thread t = new Thread(this);
  55.         t.start();
  56.     }
  57.     
  58.     public void keyPressed(int key){
  59.         switch(key){
  60.             case -6:
  61.                 midlet.notifyDestroyed();
  62.                 break;
  63.             case -7:
  64.                 
  65.                 distance ^= true;
  66.                 if(distance){
  67.                     mesh.setTranslation(0.0f, 0.0f, -15.0f); 
  68.                 }else{
  69.                     mesh.setTranslation(0.0f, 0.0f, -5.0f); 
  70.                 }
  71.                 
  72.                 break;
  73.             case -11:
  74.                 break;
  75.         }
  76.     }
  77.     
  78.     public void run(){
  79.         Graphics g = getGraphics();
  80.         FPS fps = new FPS();
  81.         
  82.         while(true){
  83.           
  84.             mesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);
  85.             
  86.             fps.calculateFps();
  87.             draw3D(g);
  88.             g.setColor(0xFF0000);
  89.             g.drawString(fps.fps_string, 0, 0, 0);
  90.             flushGraphics();
  91.         }
  92.     }
  93.     public void draw3D(Graphics g){
  94.         try{
  95.             g3d.bindTarget(g);
  96.             g3d.render(world);
  97.         }catch(Exception e){
  98.             
  99.         }finally{
  100.             g3d.releaseTarget();
  101.         }
  102.     }    
  103.     
  104. }
  105. class MeshCreator{
  106.     
  107.     public static Mesh createMesh(){
  108.         byte []coordinates = new byte[] {-1, 3, -1, //1
  109.         short []textures = new short[44];
  110.         int offset = 255/10;
  111.         int x = 0, y=0;
  112.        for(int i=0;i<44; i+=2){
  113.            x = x==0?255:0;
  114.            if(i>0 && x==255)
  115.                y+=offset;
  116.            
  117.            textures[i] = (short)x;
  118.            textures[i+1] = (short)y;
  119.            
  120.        }
  121.         byte []normals = new byte[] {-32, 96, -32,
  122.                                                            
  123.         int []stripLengths = new int[] {22};
  124.         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};
  125.         VertexArray va = new VertexArray(coordinates.length/3, 3, 1);
  126.         va.set(0, coordinates.length/3, coordinates);
  127.         VertexArray no = new VertexArray(normals.length/3, 3, 1);
  128.         no.set(0, normals.length/3, normals);
  129.         VertexArray tx = new VertexArray(textures.length/2, 2, 2);
  130.         tx.set(0, textures.length/2, textures);
  131.         
  132.         VertexBuffer vb = new VertexBuffer();
  133.         vb.setPositions(va, 1.0f, null);
  134.         vb.setNormals(no);
  135.         vb.setTexCoords(0, tx, (1.0f/255.0f), null);
  136.         TriangleStripArray tsa = new TriangleStripArray(strip, stripLengths);
  137.         PolygonMode pm = new PolygonMode();
  138.         pm.setShading(PolygonMode.SHADE_SMOOTH);
  139.         pm.setCulling(PolygonMode.CULL_NONE);
  140.         Material material = new Material();
  141.         material.setColor(Material.AMBIENT, 0xFFFF0000);
  142.         Appearance app = new Appearance();
  143.         app.setPolygonMode(pm);
  144.        app.setMaterial(material);
  145.         try{
  146.             Image texImg = Image.createImage("/texture.png"); // load the image
  147.             Texture2D texture = new Texture2D(new Image2D(Image2D.RGB, texImg)); // create texture from loaded image.
  148.             texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT); // repeat texture on surface
  149.             texture.setBlending(Texture2D.FUNC_DECAL); // Blend mode to use.
  150.             texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST); // Use nearest for performance, linear for quality
  151.             app.setTexture(0, texture);
  152.         }catch(Exception e){
  153.             System.out.println("Failed to create texture");
  154.         }
  155.         
  156.         Mesh m;
  157.         m = new Mesh(vb, tsa, app);
  158.         m.setAppearance(0, app);
  159.         return m;
  160.     }
  161. }
  162. class FPS{
  163.  
  164.     public static int fps = 10;
  165.     private int count_fps;
  166.     public String fps_string;
  167.     private long last;
  168.  
  169.     public FPS(){
  170.         fps = 10;
  171.         count_fps = 0;
  172.     }
  173.  
  174.     public void calculateFps(){
  175.         count_fps++;
  176.  
  177.         long n = System.currentTimeMillis() / 1000;
  178.  
  179.         if (n != last) {
  180.             fps = count_fps;
  181.             count_fps = 0;
  182.             fps_string = "Fps:" + fps;
  183.             last = n;
  184.         }
  185.     }
  186. }