Home > Articles > Home & Office Computing > Entertainment/Gaming/Gadgets

  • Print
  • + Share This
Like this article? We recommend

Complete Code of the Example Application

In Listing 5, you can see the complete code of the application. It is quite lengthy, yet a lot simpler than Sun's example. You can practice your MIDP skills by adding movement and logic to this application.

Listing 5. Complete code of the example application

package com.kontio;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.IllegalArgumentException;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;

public class Example3D extends MIDlet implements CommandListener{
 // UserIDs for objects we use in the scene.
 static final int POGOROO  = 554921620;
 static final int CAMERA  = 769302310;
 static final int TRANSFORM = 347178853;
 static final int ROO    = 418071423;

 private Display myDisplay = null;
 private ExampleCanvas myCanvas = null;

 private Timer myRefreshTimer = new Timer();
 private TimerTask myRefreshTask = null;

 private Command exitCommand = new Command("Exit", Command.ITEM, 1);

 Graphics3D myGraphics3D = Graphics3D.getInstance();
 World myWorld = null;

 private AnimationController animRoo = null;
 private Group tRoo = null;
 private Group tCams = null;
 private Group acRoo = null;

 private int animLength = 0;

 int viewport_x;
 int viewport_y;
 int viewport_width;
 int viewport_height;

 public Example3D(){
  super();
  myDisplay = Display.getDisplay(this);
  myCanvas = new ExampleCanvas(this);
  myCanvas.setCommandListener(this);
  myCanvas.addCommand(exitCommand);
 }

 public void startApp() throws MIDletStateChangeException{
  myDisplay.setCurrent(myCanvas);

  try{
   // load the world from the M3D file
   myWorld = (World)Loader.load("/pogoroo.m3g")[0];
   getObjects();
   setupAspectRatio();
  }
  catch(Exception e){
   e.printStackTrace();
  }

  myRefreshTask = new RefreshTask();

  // scehdule a repeating timer to give us a framerate of 20fps.
  myRefreshTimer.schedule(myRefreshTask, 0, 50);
 }

 void setupAspectRatio(){
  viewport_x = 0;
  viewport_y = 0;
  viewport_width = myCanvas.getWidth();
  viewport_height = myCanvas.getHeight();

  Camera cam = myWorld.getActiveCamera();

  float[] params = new float[4];
  int type = cam.getProjection(params);
  if(type != Camera.GENERIC){
   //calculate window aspect ratio
   float waspect=viewport_width/viewport_height;

   if (waspect<params[1]){
    float height = viewport_width/params[1];
    viewport_height=(int)height;
    viewport_y=(myCanvas.getHeight()-viewport_height)/2;
   }
   else{
    float width = viewport_height*params[1];
    viewport_width=(int)width;
    viewport_x=(myCanvas.getWidth()-viewport_width)/2;
   }
  }
 }

 public void getObjects(){
  try{
   tRoo   = (Group) myWorld.find(POGOROO);
   tCams  = (Group) myWorld.find(CAMERA);
   acRoo  = (Group) myWorld.find(TRANSFORM);
   animRoo = (AnimationController) myWorld.find(ROO);

   // get length of animation
   AnimationTrack track = acRoo.getAnimationTrack(0);
   animLength = 1000; // default length, 1 second
   if (track != null){
     KeyframeSequence ks = track.getKeyframeSequence();
    if (ks != null)
     animLength = ks.getDuration();
   }

  }
  catch(Exception e){
   e.printStackTrace();
  }
 }

 public void pauseApp(){
 }

 public void destroyApp(boolean unconditional) throws        
        MIDletStateChangeException{
  myRefreshTimer.cancel();
  myRefreshTimer = null;
  myRefreshTask = null;
 }

 public void paint(Graphics g){
  if(g.getClipWidth() != viewport_width ||
    g.getClipHeight() != viewport_height ||
    g.getClipX() != viewport_x ||
    g.getClipY() != viewport_y){
   g.setColor(0x00);
   g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
  }

  if ((myGraphics3D != null) && (myWorld != null)){
   myGraphics3D.bindTarget(g);
   myGraphics3D.setViewport(viewport_x, viewport_y, 
                 viewport_width, viewport_height);
   myGraphics3D.render(myWorld);
   myGraphics3D.releaseTarget();
  }
 }

 public void commandAction(Command cmd, Displayable disp)
 {
  if (cmd == exitCommand){
   try{
    destroyApp(false);
    notifyDestroyed();
   }
   catch(Exception e){
    e.printStackTrace();
   }
  }
 }

 private class RefreshTask extends TimerTask{
  public void run(){
   if(myCanvas !=null && myGraphics3D != null && myWorld != null{
    int startTime = (int)System.currentTimeMillis();
    int validity = myWorld.animate(startTime);
    myCanvas.repaint(viewport_x, viewport_y, 
               viewport_width, viewport_height);
   }
  }
 }

 class ExampleCanvas extends Canvas{
  Example3D myRooMIDlet;
  int i = 0;

  ExampleCanvas(Example3D Testlet) { myRooMIDlet = Testlet; }

  void init() { }

  void destroy() { }

  protected void paint(Graphics g) { myRooMIDlet.paint(g); }

  protected void keyPressed(int i) { }

  protected void keyReleased(int i) { }

  protected void keyRepeated(int i) { }

  protected void pointerDragged(int x, int y) { }

  protected void pointerPressed(int x, int y) { }

  protected void pointerReleased(int x, int y) { }
 }
}
  • + Share This
  • 🔖 Save To Your Account