1.23.2009

Animation

Membuat sedikit animasi mugkin akan menambah indah hasil program yang dihasulkan kode berikut adalah salah satu contoh yang mungkin bisa jadi gambaran :

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

public class Animation extends MIDlet
{
private Display display; // The display
private AnimationCanvas canvas; // Canvas
private Timer tm; // Timer
private AnimateTimerTask tt; // Task

public Animation()
{
display = Display.getDisplay(this);
canvas = new AnimationCanvas(this);

// Create task that fires off every 1/10 second
tm = new Timer();
tt = new AnimateTimerTask(canvas);
tm.schedule(tt, 0, 100);
}

protected void startApp()
{
display.setCurrent(canvas);
}

protected void pauseApp()
{ }

protected void destroyApp(boolean unconditional)
{ }

public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
class AnimateTimerTask extends TimerTask
{
private AnimationCanvas canvas;

public AnimateTimerTask(AnimationCanvas canvas)
{
this.canvas = canvas;
}

public final void run()
{
// ****************************************************
if ((canvas.x_loc + canvas.radius + canvas.x_dir > canvas.getWidth()) ||
(canvas.x_loc - canvas.radius + canvas.x_dir < 0))
{
canvas.x_dir = -canvas.x_dir;
canvas.changeColor();
canvas.directionChanged++;
}

// **************************************************
if ((canvas.y_loc + canvas.radius + canvas.y_dir > canvas.getHeight()) ||
(canvas.y_loc - canvas.radius + canvas.y_dir < 0))
{
canvas.y_dir = -canvas.y_dir;
canvas.changeColor();
canvas.directionChanged++;
}

//*********************************
canvas.x_loc += canvas.x_dir;
canvas.y_loc += canvas.y_dir;

canvas.repaint();
}
}

class AnimationCanvas extends Canvas implements CommandListener
{
private Animation midlet; // Main midlet
private Command cmExit; // Exit midlet
private int keyFire, // Reset ball
keyRight, // Increase ball radius
keyLeft; // Decrease ball radius
private boolean clearBackground = false; // Clear background
private Random random; // Random number
int x_loc, // Current x & y locations
y_loc,
radius, // Ball radius
red, // rgb colors
green,
blue,
x_dir, // Next x & y positions of ball
y_dir,
start_x, // Where ball starts
start_y,
directionChanged = 0; // How many times we've hit a wall
private static final int MAX_CHANGES = 50;

/*--------------------------------------------------
* Constructor
*-------------------------------------------------*/
public AnimationCanvas(Animation midlet)
{
// ****************************
this.midlet = midlet;

random = new java.util.Random();

// Determine starting location and direction of ball
init();
radius = 7;

// ******************************************
cmExit = new Command("Exit", Command.EXIT, 1);

keyFire = getKeyCode(FIRE);
keyRight = getKeyCode(RIGHT);
keyLeft = getKeyCode(LEFT);

addCommand(cmExit);
setCommandListener(this);
}


protected void paint(Graphics g)
{
// ********************************
if (directionChanged > MAX_CHANGES)
init();

// the background
if (clearBackground)
{
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
clearBackground = !clearBackground;
}

// Set warna dan gambar
g.setColor(red, green, blue);
g.fillArc( x_loc, y_loc, radius, radius, 0, 360);
}

/*--------------------------------------------------
* Ini Lokasi ball
*-------------------------------------------------*/
private void init()
{
// Start close to the middle
x_loc = getWidth() / 2;
y_loc = getHeight() / 2;

// The direction the ball is heading
x_dir = (random.nextInt() % 10);
if (x_dir == 0) x_dir = 1;

y_dir = (random.nextInt() % 10);
if (y_dir == 0) y_dir = 1;

directionChanged = 0;
clearBackground = true;
changeColor();
}


protected void changeColor()
{
// The shift is to remove any sign (negative) bit
red = (random.nextInt() >>> 1) % 256;
green = (random.nextInt() >>> 1) % 256;
blue = (random.nextInt() >>> 1) % 256;
}

/*--------------------------------------------------
* Event handling
*-------------------------------------------------*/
public void commandAction(Command c, Displayable d)
{
if (c == cmExit)
midlet.exitMIDlet();
}


protected void keyPressed(int keyCode)
{
// Restart
if (keyCode == keyFire)
init();
// Decrease ball size
else if (keyCode == keyLeft)
radius = Math.max(1, --radius);
else if (keyCode == keyRight)
// Increase ball size
radius = Math.min(getWidth() / 4, ++radius);
}
}

1 komentar:

TOM alcore mengatakan...

sulit

Posting Komentar

Template by : kendhin x-template.blogspot.com