Animation Midlet


<strong><span style=”color: #ff0000;”>/*</span></strong>
<strong><span style=”color: #ff0000;”>J2ME in a Nutshell</span></strong>
<strong><span style=”color: #ff0000;”>By Kim Topley</span></strong>
<strong><span style=”color: #ff0000;”>ISBN: 0-596-00253-X</span></strong>

<strong><span style=”color: #ff0000;”>*/</span></strong>

<strong><span style=”color: #ff0000;”>import java.util.Timer;</span></strong>
<strong><span style=”color: #ff0000;”>import java.util.TimerTask;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Canvas;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Command;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.CommandListener;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Display;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Displayable;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Form;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Gauge;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Graphics;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.Item;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.lcdui.ItemStateListener;</span></strong>
<strong><span style=”color: #ff0000;”>import javax.microedition.midlet.MIDlet;</span></strong>

<strong><span style=”color: #ff0000;”>public class AnimationMIDlet extends MIDlet </span></strong>
<strong><span style=”color: #ff0000;”> implements CommandListener, ItemStateListener {</span></strong>

<strong><span style=”color: #ff0000;”>// The MIDlet’s Display object</span></strong>
<strong><span style=”color: #ff0000;”> private Display display;</span></strong>

<strong><span style=”color: #ff0000;”>// Flag indicating first call of startApp</span></strong>
<strong><span style=”color: #ff0000;”> protected boolean started;</span></strong>

<strong><span style=”color: #ff0000;”>// Exit command</span></strong>
<strong><span style=”color: #ff0000;”> private Command exitCommand;</span></strong>

<strong><span style=”color: #ff0000;”>// Setup command</span></strong>
<strong><span style=”color: #ff0000;”> private Command setupCommand;</span></strong>

<strong><span style=”color: #ff0000;”>// Run command</span></strong>
<strong><span style=”color: #ff0000;”> private Command runCommand;</span></strong>

<strong><span style=”color: #ff0000;”>// Configuration form</span></strong>
<strong><span style=”color: #ff0000;”> private Form form;</span></strong>

<strong><span style=”color: #ff0000;”>// Animation canvas</span></strong>
<strong><span style=”color: #ff0000;”> private AnimationCanvas canvas;</span></strong>

<strong><span style=”color: #ff0000;”>// Gauge for block count</span></strong>
<strong><span style=”color: #ff0000;”> private Gauge blockGauge;</span></strong>

<strong><span style=”color: #ff0000;”>// Gauge for frame rate</span></strong>
<strong><span style=”color: #ff0000;”> private Gauge rateGauge;</span></strong>

<strong><span style=”color: #ff0000;”>// Initial frame rate</span></strong>
<strong><span style=”color: #ff0000;”> private static final int FRAME_RATE = 1;</span></strong>

<strong><span style=”color: #ff0000;”>// Initial number of blocks</span></strong>
<strong><span style=”color: #ff0000;”> private static final int BLOCK_COUNT = 1;</span></strong>

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

<strong><span style=”color: #ff0000;”>canvas = createAnimationCanvas();</span></strong>

<strong><span style=”color: #ff0000;”>exitCommand = new Command(”Exit”, Command.EXIT, 0);</span></strong>
<strong><span style=”color: #ff0000;”> setupCommand = new Command(”Setup”, Command.SCREEN, 0);</span></strong>
<strong><span style=”color: #ff0000;”> runCommand = new Command(”Run”, Command.SCREEN, 0);</span></strong>

<strong><span style=”color: #ff0000;”>canvas.addCommand(exitCommand);</span></strong>
<strong><span style=”color: #ff0000;”> canvas.addCommand(setupCommand);</span></strong>
<strong><span style=”color: #ff0000;”> form.addCommand(exitCommand);</span></strong>
<strong><span style=”color: #ff0000;”> form.addCommand(runCommand);</span></strong>

<strong><span style=”color: #ff0000;”>form.setCommandListener(this);</span></strong>
<strong><span style=”color: #ff0000;”> canvas.setCommandListener(this);</span></strong>

<strong><span style=”color: #ff0000;”>display.setCurrent(form);</span></strong>
<strong><span style=”color: #ff0000;”> started = true;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>protected void pauseApp() {</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>protected void destroyApp(boolean unconditional) {</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

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

<strong><span style=”color: #ff0000;”>// Creates the canvas that will draw the block</span></strong>
<strong><span style=”color: #ff0000;”> protected AnimationCanvas createAnimationCanvas() {</span></strong>
<strong><span style=”color: #ff0000;”> return new AnimationCanvas();</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>class AnimationCanvas extends Canvas {</span></strong>

<strong><span style=”color: #ff0000;”>// Size of each block</span></strong>
<strong><span style=”color: #ff0000;”> protected static final int SIZE = 4;</span></strong>

<strong><span style=”color: #ff0000;”>// Initial speeds in the X direction</span></strong>
<strong><span style=”color: #ff0000;”> protected final int[] xSpeeds = { 2, -2, 0, -2 };</span></strong>

<strong><span style=”color: #ff0000;”>// Initial speeds in the Y direction</span></strong>
<strong><span style=”color: #ff0000;”> protected final int[] ySpeeds = { 2, -2, 2, -0 };</span></strong>

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

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

<strong><span style=”color: #ff0000;”>// Width of screen</span></strong>
<strong><span style=”color: #ff0000;”> protected int width = getWidth();</span></strong>

<strong><span style=”color: #ff0000;”>// Height of screen</span></strong>
<strong><span style=”color: #ff0000;”> protected int height = getHeight();</span></strong>

<strong><span style=”color: #ff0000;”>// The screen update rate</span></strong>
<strong><span style=”color: #ff0000;”> protected int frameRate;</span></strong>

<strong><span style=”color: #ff0000;”>// The blocks to draw on the screen</span></strong>
<strong><span style=”color: #ff0000;”> protected Block[] blocks;</span></strong>

<strong><span style=”color: #ff0000;”>// The update timer</span></strong>
<strong><span style=”color: #ff0000;”> protected Timer timer;</span></strong>

<strong><span style=”color: #ff0000;”>// The update timer task</span></strong>
<strong><span style=”color: #ff0000;”> protected TimerTask updateTask;</span></strong>

<strong><span style=”color: #ff0000;”>// Gets the maximum number of blocks</span></strong>
<strong><span style=”color: #ff0000;”> public int getMaxBlocks() {</span></strong>
<strong><span style=”color: #ff0000;”> return blocks.length;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>// Constructs a canvas with default settings</span></strong>
<strong><span style=”color: #ff0000;”> AnimationCanvas() {</span></strong>
<strong><span style=”color: #ff0000;”> setBlockCount(BLOCK_COUNT);</span></strong>
<strong><span style=”color: #ff0000;”> setFrameRate(FRAME_RATE);</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

<strong><span style=”color: #ff0000;”>blocks = new Block[count];</span></strong>
<strong><span style=”color: #ff0000;”> createBlocks();</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>// Gets the number of blocks to draw</span></strong>
<strong><span style=”color: #ff0000;”> public int getBlockCount() {</span></strong>
<strong><span style=”color: #ff0000;”> return blocks.length;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

<strong><span style=”color: #ff0000;”>// Gets the number of updates per second</span></strong>
<strong><span style=”color: #ff0000;”> public int getFrameRate() {</span></strong>
<strong><span style=”color: #ff0000;”> return frameRate;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

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

<strong><span style=”color: #ff0000;”>// Notification that the canvas has been made visible</span></strong>
<strong><span style=”color: #ff0000;”> protected void showNotify() {</span></strong>
<strong><span style=”color: #ff0000;”> // Start the frame timer running</span></strong>
<strong><span style=”color: #ff0000;”> startFrameTimer();</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>// Notification that the canvas is no longer visible</span></strong>
<strong><span style=”color: #ff0000;”> protected void hideNotify() {</span></strong>
<strong><span style=”color: #ff0000;”> // Stop the frame timer </span></strong>
<strong><span style=”color: #ff0000;”> stopFrameTimer();</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

<strong><span style=”color: #ff0000;”>// Starts the frame redraw timer</span></strong>
<strong><span style=”color: #ff0000;”> protected void startFrameTimer() {</span></strong>
<strong><span style=”color: #ff0000;”> timer = new Timer();</span></strong>

<strong><span style=”color: #ff0000;”>updateTask = new TimerTask() {</span></strong>
<strong><span style=”color: #ff0000;”> public void run() {</span></strong>
<strong><span style=”color: #ff0000;”> moveAllBlocks();</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>
<strong><span style=”color: #ff0000;”> };</span></strong>
<strong><span style=”color: #ff0000;”> long interval = 1000/frameRate;</span></strong>
<strong><span style=”color: #ff0000;”> timer.schedule(updateTask, interval, interval);</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>// Stops the frame redraw timer</span></strong>
<strong><span style=”color: #ff0000;”> protected void stopFrameTimer() {</span></strong>
<strong><span style=”color: #ff0000;”> timer.cancel(); </span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

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

<strong><span style=”color: #ff0000;”>// Request a repaint of the screen</span></strong>
<strong><span style=”color: #ff0000;”> repaint(); </span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>// Inner class used to represent a block on the screen</span></strong>
<strong><span style=”color: #ff0000;”> class Block {</span></strong>
<strong><span style=”color: #ff0000;”> int x; // X position</span></strong>
<strong><span style=”color: #ff0000;”> int y; // Y position</span></strong>
<strong><span style=”color: #ff0000;”> int xSpeed; // Speed in the X direction</span></strong>
<strong><span style=”color: #ff0000;”> int ySpeed; // Speed in the Y direction</span></strong>

<strong><span style=”color: #ff0000;”>Block(int x, int y, int xSpeed, int ySpeed) {</span></strong>
<strong><span style=”color: #ff0000;”> this.x = x;</span></strong>
<strong><span style=”color: #ff0000;”> this.y = y;</span></strong>
<strong><span style=”color: #ff0000;”> this.xSpeed = xSpeed;</span></strong>
<strong><span style=”color: #ff0000;”> this.ySpeed = ySpeed;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>void move() {</span></strong>
<strong><span style=”color: #ff0000;”> x += xSpeed;</span></strong>
<strong><span style=”color: #ff0000;”> if (x &lt;= 0 || x + SIZE &gt;= width) {</span></strong>
<strong><span style=”color: #ff0000;”> xSpeed = -xSpeed;</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>

<strong><span style=”color: #ff0000;”>y += ySpeed;</span></strong>
<strong><span style=”color: #ff0000;”> if (y &lt;= 0 || y + SIZE &gt;= height) {</span></strong>
<strong><span style=”color: #ff0000;”> ySpeed = -ySpeed;</span></strong>
<strong><span style=”color: #ff0000;”> } </span></strong>
<strong><span style=”color: #ff0000;”> } </span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>
<strong><span style=”color: #ff0000;”> }</span></strong>
<strong><span style=”color: #ff0000;”>}</span></strong>

Tinggalkan Balasan

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

Logo WordPress.com

You are commenting using your WordPress.com 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

Zoemalang's community

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: