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);
}
}
Terjemahkan
Kategori
- J2ME (2)
- Java Swing (3)
- JFrame (1)
- Pemograman Java (2)
1.23.2009
Animation
Diposting oleh
Nanin Ernis Nurjaman
di
16.49
1 komentar
Label: J2ME
Contoh Text-Box Midlet
Lansung pada pokoknya hehe.. kita akan buat contoh program text Box Midlet... ketikan aja kode dibawah ini ya....!
import java.io.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class TextBoxMIDlet extends TextBoxMIDlet implements CommandListener {
// Exit command
private static final Command EXIT_COMMAND =
new Command("Exit", Command.EXIT, 0);
// OK command
private static final Command OK_COMMAND =
new Command("OK", Command.OK, 0);
// Clear text box content
private static final Command CLEAR_COMMAND =
new Command("Clear", Command.SCREEN, 1);
// Reverse the content of the text box
private static final Command REVERSE_COMMAND =
new Command("Reverse", Command.SCREEN, 1);
protected void startApp() {
boolean firstTime = !started;
super.startApp();
// If this is the first execution
// of startApp, install commands
if (firstTime) {
textBox.addCommand(OK_COMMAND);
textBox.addCommand(EXIT_COMMAND);
textBox.addCommand(CLEAR_COMMAND);
textBox.addCommand(REVERSE_COMMAND);
textBox.setCommandListener(this);
}
}
// Command implementations.
public void commandAction(Command c, Displayable d) {
if (c == EXIT_COMMAND) {
destroyApp(true);
notifyDestroyed();
} else if (c == OK_COMMAND) {
System.out.println("OK pressed");
} else if (c == CLEAR_COMMAND) {
textBox.setString(null);
} else if (c == REVERSE_COMMAND) {
String str = textBox.getString();
if (str != null) {
StringBuffer sb = new StringBuffer(str);
textBox.setString(sb.reverse().toString());
}
}
}
}
class TextBoxMIDlet extends MIDlet {
// Maximum size of the text in the TextBox
private static final int MAX_TEXT_SIZE = 64;
// The TextBox
protected TextBox textBox;
// The MIDlet's Display object
protected Display display;
// Flag indicating first call of startApp
protected boolean started;
protected void startApp() {
if (!started) {
// First time through - initialize
// Get the text to be displayed
String str = null;
try {
InputStream is = getClass().getResourceAsStream("test.txt");
InputStreamReader r = new InputStreamReader(is);
char[] buffer = new char[32];
StringBuffer sb = new StringBuffer();
int count;
while ((count = r.read(buffer, 0, buffer.length)) > -1) {
sb.append(buffer, 0, count);
}
str = sb.toString();
} catch (IOException ex) {
str = "Failed to load text";
}
// Create the TextBox
textBox = new TextBox("TextBox Example", str,
MAX_TEXT_SIZE, TextField.ANY);
// Create a ticker and install it
Ticker ticker = new Ticker("This is a ticker...");
textBox.setTicker(ticker);
// Install the TextBox as the current screen
display = Display.getDisplay(this);
display.setCurrent(textBox);
started = true;
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
Diposting oleh
Nanin Ernis Nurjaman
di
16.45
1 komentar
Label: J2ME