Home > Articles > Programming > Java

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Beginning Mobile Phone Game Programming

Like this article? We recommend
Beginning Mobile Phone Game Programming

Example

We'll develop a very simple 3D application that rotates a polygon. The polygon is a cube and its texture is a photo of an old car. Listing 1 shows the midlet's main class—the central class of the application. It creates the application and sets the timer to run MyCanvas.

Listing 1. MIDletMain class

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class MIDletMain extends MIDlet {
 static MIDletMain midlet;
 MyCanvas d = new MyCanvas();
 Timer iTimer = new Timer();

 public MIDletMain() {
  this.midlet = this;
 }

 public void startApp() {
  Display.getDisplay(this).setCurrent(d);
  iTimer.schedule( new MyTimerTask(), 0, 40 );
 }

 public void pauseApp() {
 }

 public void destroyApp(boolean unconditional) {
 }


 public static void quitApp() {
  midlet.destroyApp(true);
  midlet.notifyDestroyed();
  midlet = null;
 }


 class MyTimerTask extends TimerTask {
public void run() {
   if( d != null ) {
    d.repaint();
   }
  }
 }
}

Listing 2 shows the MyCanvas class, which contains all the graphical logic of the application. In init() method the vertices are created, the texture file is loaded, and the texture is set. The appearance and the background are also set. The paint() method paints and rotates the cube. The Figure 1 shows the actual application running in an emulator.

Listing 2. MyCanvas class

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;

public class MyCanvas extends Canvas {

 private Graphics3D  graphics3d;
 private Camera   camera;
 private Light   light;
 private float   angle = 0.0f;
 private Transform  transform = new Transform();
 private Background  background = new Background();
 private VertexBuffer vbuffer;
 private IndexBuffer  indexbuffer;
 private Appearance  appearance;
 private Material  material = new Material();
 private Image   image;

 public MyCanvas() {
  // set up this Displayable to listen to command events
  setCommandListener(new CommandListener() {
   public void commandAction(Command c, Displayable d) {
    if (c.getCommandType() == Command.EXIT) {
     MIDletMain.quitApp();}}
  });
  try { init();}
  catch(Exception e) { e.printStackTrace();}
 }

 /**
  * Component initialization.
  */
 private void init() throws Exception {
  addCommand(new Command("Exit", Command.EXIT, 1));
  graphics3d = Graphics3D.getInstance();

  camera = new Camera();
  camera.setPerspective( 60.0f,
   (float)getWidth()/ (float)getHeight(),
   1.0f,
   1000.0f );

  light = new Light();
  light.setColor(0xffffff);
  light.setIntensity(1.25f);

  short[] vert = {
   5, 5, 5, -5, 5, 5, 5,-5, 5, -5,-5, 5,
   -5, 5,-5, 5, 5,-5, -5,-5,-5, 5,-5,-5,
   -5, 5, 5, -5, 5,-5, -5,-5, 5, -5,-5,-5,
   5, 5,-5, 5, 5, 5, 5,-5,-5, 5,-5, 5,
   5, 5,-5, -5, 5,-5, 5, 5, 5, -5, 5, 5,
   5,-5, 5, -5,-5, 5, 5,-5,-5, -5,-5,-5 };

  VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
  vertArray.set(0, vert.length/3, vert);

  // The per-vertex normals for the cube
  byte[] norm = {
   0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,
   0, 0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127,
   -127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0,
   127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0,
   0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
   0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127, 0 };

  VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
  normArray.set(0, norm.length/3, norm);

  // per vertex texture coordinates
  short[] tex = {
   1, 0,  0, 0,  1, 1,  0, 1,
   1, 0,  0, 0,  1, 1,  0, 1,
   1, 0,  0, 0,  1, 1,  0, 1,
   1, 0,  0, 0,  1, 1,  0, 1,
   1, 0,  0, 0,  1, 1,  0, 1,
   1, 0,  0, 0,  1, 1,  0, 1 };

  VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
  texArray.set(0, tex.length/2, tex);

  int[] stripLen = { 4, 4, 4, 4, 4, 4 };

  // the VertexBuffer for our object
  VertexBuffer vb = vbuffer = new VertexBuffer();
  vb.setPositions(vertArray, 1.0f, null);
  vb.setNormals(normArray);
  vb.setTexCoords(0, texArray, 1.0f, null);

  indexbuffer = new TriangleStripArray( 0, stripLen );

  // the image for the texture
  image = Image.createImage( "/pic1.png" );
  Image2D image2D = new Image2D( Image2D.RGB, image );
  Texture2D texture = new Texture2D( image2D );
  texture.setFiltering(Texture2D.FILTER_NEAREST,
        Texture2D.FILTER_NEAREST);
  texture.setWrapping(Texture2D.WRAP_CLAMP,
       Texture2D.WRAP_CLAMP);
  texture.setBlending(Texture2D.FUNC_MODULATE);

  // create the appearance
  appearance = new Appearance();
  appearance.setTexture(0, texture);
  appearance.setMaterial(material);
  material.setColor(Material.DIFFUSE, 0xFFFFFFFF);
  material.setColor(Material.SPECULAR, 0xFFFFFFFF);
  material.setShininess(100.0f);

  background.setColor(0xffffcc);
 }

 protected void paint(Graphics g) {
  graphics3d.bindTarget(g, true,
      Graphics3D.DITHER |
      Graphics3D.TRUE_COLOR);
  graphics3d.clear(background);

  // Set the camera
  Transform transform = new Transform();
  transform.postTranslate(0.0f, 0.0f, 30.0f);
  graphics3d.setCamera(camera, transform);

  // Set light
  graphics3d.resetLights();
  graphics3d.addLight(light, transform);

  // set rotation
  angle += 1.0f;
  transform.setIdentity();
  transform.postRotate(angle, 1.0f, 1.0f, 1.0f);

  graphics3d.render(vbuffer, indexbuffer, appearance, transform);
  graphics3d.releaseTarget();
 }
}
Figure 1

Figure 1 The application running in an emulator

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Emily NaveCommunity Tips: Starting a User Group Library
By Emily Nave on August 3, 20102 Comments

The Central Penn Adobe User Group (CPAUG) uses a library program to share books from different publishers with members. A short Q&A with group leader Megan Fister provides some great tips for starting your own.

Keep going with GWT
By Federico Kereki on August 1, 2010 No Comments

I've been using GWT for some years now, and I'm still contented with the easier way for web development. After having written a book on GWT development, doing a blog seemed a good idea for answering questions, and for further expanding topics that didn't get a place in the book.

Emily NaveUser Group Organizations: Finding Support in the Greater IT Community
By Emily Nave on July 29, 20102 Comments

Birds of a feather flock together, right? If you’re already a member of an established user group or looking for other like-minded technology evangelists, connecting with peers is an important part of being an active voice in the IT community.

See All Related Blogs

Informit Network