// Studscanvas.java import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class Studscanvas extends GameCanvas { int ball_radius; double ball_x, ball_y, ball_dx, ball_dy; Timer the_timer; Graphics g; // Key repeat rate in milliseconds public static final int KEY_REPEAT_RATE = 250; // Key constants public static final int MY_UP_KEY = 0; public static final int MY_LEFT_KEY = 1; public static final int MY_DOWN_KEY = 2; public static final int MY_RIGHT_KEY = 3; public static final int MY_FIRE_KEY = 4; // Key states for up, left, down, right, and fire key private boolean[] key_is_down = { false, false, false, false, false }; // The last time each key changed state private long[] last_key_time = { 0, 0, 0, 0, 0 }; // Lookup table for key constants private int[] key_value = { GameCanvas.UP_PRESSED, GameCanvas.LEFT_PRESSED, GameCanvas.DOWN_PRESSED, GameCanvas.RIGHT_PRESSED, GameCanvas.FIRE_PRESSED }; public Studscanvas() { super(false); int screen_width = getWidth(); int screen_height = getHeight(); ball_radius = 5; ball_x = screen_width / 2; ball_y = screen_height / 2; ball_dx = 1; ball_dy = -1; } public void showNotify() { the_timer = new Timer(); g = getGraphics(); TimerTask task = new TimerTask() { public void run() { process_keys(); update_game_state(); render(); flushGraphics(); } }; the_timer.schedule(task, 0, 20); } public void hideNotify() { if (the_timer != null) { the_timer.cancel(); the_timer = null; } } private void process_keys() { int keys = getKeyStates(); long current_time = System.currentTimeMillis(); // Loop through the keys for (int i = 0; i < 5; i++){ // By default, the key is not pressed key_is_down[i] = false; // Is the user pressing this key? if ((keys & key_value[i]) != 0) { long elapsed_time = current_time - last_key_time[i]; // Is it time to toggle the key state? if (elapsed_time >= KEY_REPEAT_RATE) { // Save the current time last_key_time[i] = current_time; // Toggle the state to "down" key_is_down[i] = true; } } } if (key_is_down[MY_LEFT_KEY]) { System.out.println("Pressed left!"); ball_dx -= 1; } if (key_is_down[MY_RIGHT_KEY]) { System.out.println("Pressed right!"); ball_dx += 1; } if (key_is_down[MY_UP_KEY]) { System.out.println("Pressed up!"); ball_dy -= 1; } if (key_is_down[MY_DOWN_KEY]) { System.out.println("Pressed down!"); ball_dy += 1; } if (key_is_down[MY_FIRE_KEY]) { System.out.println("Pressed FIRE!"); ball_dx = 0; ball_dy = 0; } } private void update_game_state() { int screen_width = getWidth(); int screen_height = getHeight(); ball_x += ball_dx; ball_y += ball_dy; if (ball_x + ball_radius >= screen_width) { ball_dx *= -1; } else if (ball_x - ball_radius <= 0) { ball_dx *= -1; } if (ball_y - ball_radius <= 0) { ball_dy *= -1; } else if (ball_y + ball_radius >= screen_height) { ball_dy *= -1; } // System.out.println("ball_x = " + ball_x + ", ball_y = " + ball_y + // ", ball_dx = " + ball_dx + " + ball_dy = " + ball_dy); } public void render() { g.setColor(0xffffff); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0x000000); g.drawString("dx = " + ball_dx + ", dy = " + ball_dy, 0, 0, Graphics.TOP | Graphics.LEFT); g.fillArc((int)(ball_x - ball_radius), (int)(ball_y - ball_radius), ball_radius * 2, ball_radius * 2, 0, 360); } } // class Studscanvas