[my weblog] [my software] [contact me]

h3's weblog

(garbage in, garbage out)

back to today
 

23 december 2007

J2ME rotozoomer

Today I tried to code an oldschool demo effect for my mobile phone (a Nokia 3110 classic)...
I created a simple rotozomeer :)

here is the code (J2ME MIDP 2.0)

package org.postronic.h3.testmidlet;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class RotozoomerMIDlet extends MIDlet {

  public RotozoomerMIDlet() { }

  protected void startApp() throws MIDletStateChangeException {
    RotozoomerCanvas canvas = new RotozoomerCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
    canvas.run();
  }

  protected void pauseApp() { }

  protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }

}



package org.postronic.h3.testmidlet;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class RotozoomerCanvas extends GameCanvas {

  protected RotozoomerCanvas() {
    super(true);
    setFullScreenMode(true);
  }

  public void run() {
    int imgWidth = getWidth();
    int imgHeight = getHeight();
    Graphics g = getGraphics();
    int[] img = new int[imgWidth * imgHeight];

    Image texture = Image.createImage(32, 16);
    Graphics tg = texture.getGraphics();
    tg.setColor(200, 200, 255);
    tg.drawString("h3-r3", 2, 0, Graphics.TOP | Graphics.LEFT);
    tg.setColor(200, 200, 255);
    tg.drawString("h3-r3", 4, 0, Graphics.TOP | Graphics.LEFT);
    tg.setColor(0, 0, 255);
    tg.drawString("h3-r3", 3, 0, Graphics.TOP | Graphics.LEFT);
    int[] txImg = new int[32 * 16];
    texture.getRGB(txImg, 0, 32, 0, 0, 32, 16);
    texture = null; // dispose the texture Image

    float tx, ty;
    float dtx, dty;
    float otx, oty;

    double cont = 0;
    double cos, sin;
    double size;
    int offs;
    for(;;) {
      cos = Math.cos(cont);
      sin = Math.sin(cont);
      tx = (float) cos * 20;
      ty = (float) sin * 20;
      size = cos;
      dtx = (float) (cos * size);
      dty = (float) (sin * size);
      cont += 0.02;
      for (int y = 0; y < imgHeight; y++) {
        otx = tx;
        oty = ty;
        offs = y * imgWidth;
        for (int x = 0; x < imgWidth; x++) {
          img[x + offs] = txImg[((int)(tx) & 31) + (((int)(ty) & 15) * 32)];
          tx += dtx;
          ty += dty;
        }
        tx = otx - dty;
        ty = oty + dtx;
      }
      g.drawRGB(img, 0, imgWidth, 0, 0, imgWidth, imgHeight, false);
      flushGraphics();
    }
  }

}

 
2007-12-23 23:57:09
H3-r3 (URL) wrote:
This little program is also quite useful..
I just start it when I want to empty the cellphone batteries :D

Write here your comment

name:
email:
website:
comment:
code:
(mandatory)
Links:
Syndications:
XML RSS feed
XML RSS comments feed
Klip Klipfolio Klip
Klip Comments Klip

Blog friends: