Animation Midlet

<strong>J2ME in a Nutshell</strong>
<strong>By Kim Topley</strong>
<strong>ISBN: 0-596-00253-X</strong>


<strong>import java.util.Timer;</strong>
<strong>import java.util.TimerTask;</strong>
<strong>import javax.microedition.lcdui.Canvas;</strong>
<strong>import javax.microedition.lcdui.Command;</strong>
<strong>import javax.microedition.lcdui.CommandListener;</strong>
<strong>import javax.microedition.lcdui.Display;</strong>
<strong>import javax.microedition.lcdui.Displayable;</strong>
<strong>import javax.microedition.lcdui.Form;</strong>
<strong>import javax.microedition.lcdui.Gauge;</strong>
<strong>import javax.microedition.lcdui.Graphics;</strong>
<strong>import javax.microedition.lcdui.Item;</strong>
<strong>import javax.microedition.lcdui.ItemStateListener;</strong>
<strong>import javax.microedition.midlet.MIDlet;</strong>

<strong>public class AnimationMIDlet extends MIDlet </strong>
<strong> implements CommandListener, ItemStateListener {</strong>

<strong>// The MIDlet’s Display object</strong>
<strong> private Display display;</strong>

<strong>// Flag indicating first call of startApp</strong>
<strong> protected boolean started;</strong>

<strong>// Exit command</strong>
<strong> private Command exitCommand;</strong>

<strong>// Setup command</strong>
<strong> private Command setupCommand;</strong>

<strong>// Run command</strong>
<strong> private Command runCommand;</strong>

<strong>// Configuration form</strong>
<strong> private Form form;</strong>

<strong>// Animation canvas</strong>
<strong> private AnimationCanvas canvas;</strong>

<strong>// Gauge for block count</strong>
<strong> private Gauge blockGauge;</strong>

<strong>// Gauge for frame rate</strong>
<strong> private Gauge rateGauge;</strong>

<strong>// Initial frame rate</strong>
<strong> private static final int FRAME_RATE = 1;</strong>

<strong>// Initial number of blocks</strong>
<strong> private static final int BLOCK_COUNT = 1;</strong>

<strong>protected void startApp() {</strong>
<strong> if (!started) {</strong>
<strong> display = Display.getDisplay(this);</strong>
<strong> form = new Form(”Animation”);</strong>
<strong> rateGauge = new Gauge(”Frame rate”, true, 10, FRAME_RATE);</strong>
<strong> blockGauge = new Gauge(”Blocks”, true, 4, BLOCK_COUNT);</strong>
<strong> form.append(rateGauge);</strong>
<strong> form.append(blockGauge);</strong>
<strong> form.setItemStateListener(this);</strong>

<strong>canvas = createAnimationCanvas();</strong>

<strong>exitCommand = new Command(”Exit”, Command.EXIT, 0);</strong>
<strong> setupCommand = new Command(”Setup”, Command.SCREEN, 0);</strong>
<strong> runCommand = new Command(”Run”, Command.SCREEN, 0);</strong>

<strong> canvas.addCommand(setupCommand);</strong>
<strong> form.addCommand(exitCommand);</strong>
<strong> form.addCommand(runCommand);</strong>

<strong> canvas.setCommandListener(this);</strong>

<strong> started = true;</strong>
<strong> }</strong>
<strong> }</strong>

<strong>protected void pauseApp() {</strong>
<strong> }</strong>

<strong>protected void destroyApp(boolean unconditional) {</strong>
<strong> }</strong>

<strong>public void commandAction(Command c, Displayable d) {</strong>
<strong> if (c == exitCommand) {</strong>
<strong> // Exit. No need to call destroyApp</strong>
<strong> // because it is empty.</strong>
<strong> notifyDestroyed();</strong>
<strong> } else if (c == runCommand) {</strong>
<strong> display.setCurrent(canvas);</strong>
<strong> } else if (c == setupCommand) {</strong>
<strong> display.setCurrent(form);</strong>
<strong> }</strong>
<strong> }</strong>

<strong>public void itemStateChanged(Item item) {</strong>
<strong> if (item == blockGauge) {</strong>
<strong> int count = blockGauge.getValue();</strong>
<strong> if (count &lt; 1) {</strong>
<strong> count = 1;</strong>
<strong> }</strong>
<strong> canvas.setBlockCount(count);</strong>
<strong> } else if (item == rateGauge) {</strong>
<strong> int count = rateGauge.getValue();</strong>
<strong> if (count &lt; 1) {</strong>
<strong> count = 1;</strong>
<strong> }</strong>
<strong> canvas.setFrameRate(count);</strong>
<strong> } </strong>
<strong> }</strong>

<strong>// Creates the canvas that will draw the block</strong>
<strong> protected AnimationCanvas createAnimationCanvas() {</strong>
<strong> return new AnimationCanvas();</strong>
<strong> }</strong>

<strong>class AnimationCanvas extends Canvas {</strong>

<strong>// Size of each block</strong>
<strong> protected static final int SIZE = 4;</strong>

<strong>// Initial speeds in the X direction</strong>
<strong> protected final int[] xSpeeds = { 2, -2, 0, -2 };</strong>

<strong>// Initial speeds in the Y direction</strong>
<strong> protected final int[] ySpeeds = { 2, -2, 2, -0 };</strong>

<strong>// Background color</strong>
<strong> protected int background = display.isColor() ? 0 : 0xc0c0c0;</strong>

<strong>// Foreground color</strong>
<strong> protected int foreground = display.isColor() ? 0xffff00 : 0;</strong>

<strong>// Width of screen</strong>
<strong> protected int width = getWidth();</strong>

<strong>// Height of screen</strong>
<strong> protected int height = getHeight();</strong>

<strong>// The screen update rate</strong>
<strong> protected int frameRate;</strong>

<strong>// The blocks to draw on the screen</strong>
<strong> protected Block[] blocks;</strong>

<strong>// The update timer</strong>
<strong> protected Timer timer;</strong>

<strong>// The update timer task</strong>
<strong> protected TimerTask updateTask;</strong>

<strong>// Gets the maximum number of blocks</strong>
<strong> public int getMaxBlocks() {</strong>
<strong> return blocks.length;</strong>
<strong> }</strong>

<strong>// Constructs a canvas with default settings</strong>
<strong> AnimationCanvas() {</strong>
<strong> setBlockCount(BLOCK_COUNT);</strong>
<strong> setFrameRate(FRAME_RATE);</strong>
<strong> }</strong>

<strong>// Sets the number of blocks to draw</strong>
<strong> public void setBlockCount(int count) {</strong>
<strong> if (count &gt; xSpeeds.length) {</strong>
<strong> throw new IllegalArgumentException(”Cannot have more than ” </strong>
<strong> + xSpeeds.length + ” blocks”);</strong>
<strong> }</strong>

<strong>blocks = new Block[count];</strong>
<strong> createBlocks();</strong>
<strong> }</strong>

<strong>// Gets the number of blocks to draw</strong>
<strong> public int getBlockCount() {</strong>
<strong> return blocks.length;</strong>
<strong> }</strong>

<strong>// Sets the number of updates per second</strong>
<strong> public void setFrameRate(int frameRate) {</strong>
<strong> if (frameRate &lt; 1 || frameRate &gt; 10) {</strong>
<strong> throw new IllegalArgumentException(”Frame rate must be &gt; 0 and &lt;= 10″);</strong>
<strong> }</strong>
<strong> this.frameRate = frameRate;</strong>
<strong> if (isShown()) {</strong>
<strong> startFrameTimer();</strong>
<strong> }</strong>
<strong> }</strong>

<strong>// Gets the number of updates per second</strong>
<strong> public int getFrameRate() {</strong>
<strong> return frameRate;</strong>
<strong> }</strong>

<strong>// Paint canvas background and all</strong>
<strong> // of the blocks in their correct locations.</strong>
<strong> protected void paint(Graphics g) {</strong>
<strong> // Paint with the background color</strong>
<strong> g.setColor(background);</strong>
<strong> g.fillRect(0, 0, width, height);</strong>

<strong>// Draw all of the blocks</strong>
<strong> g.setColor(foreground);</strong>
<strong> synchronized (this) {</strong>
<strong> for (int i = 0, count = blocks.length; i &lt; count; i++) {</strong>
<strong> g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);</strong>
<strong> }</strong>
<strong> }</strong>
<strong> }</strong>

<strong>// Notification that the canvas has been made visible</strong>
<strong> protected void showNotify() {</strong>
<strong> // Start the frame timer running</strong>
<strong> startFrameTimer();</strong>
<strong> }</strong>

<strong>// Notification that the canvas is no longer visible</strong>
<strong> protected void hideNotify() {</strong>
<strong> // Stop the frame timer </strong>
<strong> stopFrameTimer();</strong>
<strong> }</strong>

<strong>// Creates the blocks to be displayed</strong>
<strong> private void createBlocks() {</strong>
<strong> int startX = (width – SIZE)/2;</strong>
<strong> int startY = (height – SIZE)/2;</strong>
<strong> for (int i = 0, count = blocks.length; i &lt; count; i++) {</strong>
<strong> blocks[i] = new Block(startX, startY, xSpeeds[i], ySpeeds[i]);</strong>
<strong> }</strong>
<strong> }</strong>

<strong>// Starts the frame redraw timer</strong>
<strong> protected void startFrameTimer() {</strong>
<strong> timer = new Timer();</strong>

<strong>updateTask = new TimerTask() {</strong>
<strong> public void run() {</strong>
<strong> moveAllBlocks();</strong>
<strong> }</strong>
<strong> };</strong>
<strong> long interval = 1000/frameRate;</strong>
<strong> timer.schedule(updateTask, interval, interval);</strong>
<strong> }</strong>

<strong>// Stops the frame redraw timer</strong>
<strong> protected void stopFrameTimer() {</strong>
<strong> timer.cancel(); </strong>
<strong> }</strong>

<strong>// Called on expiry of timer.</strong>
<strong> public synchronized void moveAllBlocks() {</strong>
<strong> // Update the positions and speeds</strong>
<strong> // of all of the blocks</strong>
<strong> for (int i = 0, count = blocks.length; i &lt; count; i++) {</strong>
<strong> blocks[i].move();</strong>

<strong>// Request a repaint of the screen</strong>
<strong> repaint(); </strong>
<strong> }</strong>
<strong> }</strong>

<strong>// Inner class used to represent a block on the screen</strong>
<strong> class Block {</strong>
<strong> int x; // X position</strong>
<strong> int y; // Y position</strong>
<strong> int xSpeed; // Speed in the X direction</strong>
<strong> int ySpeed; // Speed in the Y direction</strong>

<strong>Block(int x, int y, int xSpeed, int ySpeed) {</strong>
<strong> this.x = x;</strong>
<strong> this.y = y;</strong>
<strong> this.xSpeed = xSpeed;</strong>
<strong> this.ySpeed = ySpeed;</strong>
<strong> }</strong>

<strong>void move() {</strong>
<strong> x += xSpeed;</strong>
<strong> if (x &lt;= 0 || x + SIZE &gt;= width) {</strong>
<strong> xSpeed = -xSpeed;</strong>
<strong> }</strong>

<strong>y += ySpeed;</strong>
<strong> if (y &lt;= 0 || y + SIZE &gt;= height) {</strong>
<strong> ySpeed = -ySpeed;</strong>
<strong> } </strong>
<strong> } </strong>
<strong> }</strong>
<strong> }</strong>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:


You are commenting using your account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Mari kita bersama meningkatkan kesadaran kita - Mulailah berpikir cerdas, Kita semua beragama !!! Agama bukan identitas...jadikanlah agama untuk keluar dari kebodohan

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI

Zoemalang's community at www.

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: