21.05.04 Benutzereingaben auf Low-Level Ebene
Zusammenfassung
Movable Interface
package de.jbb.j2me.ll;
public interface Movable {
void movePoint(int keyCode);
}
RepeatHelper
package de.jbb.j2me.ll;
public class RepeatHelper implements Runnable {
private int keyCode = -1;
private int waitingTime;
private boolean repeat;
private Movable movable;
public RepeatHelper(Movable movable, int waitingTime) {
this.movable = movable;
this.waitingTime = waitingTime;
}
public void run() {
while (true) {
if (repeat) {
movable.movePoint(keyCode);
}
try {
Thread.sleep(waitingTime);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
public void setKeyCode(int keyCode) {
this.keyCode = keyCode;
repeat = true;
}
public void stopRepeating() {
repeat = false;
}
}
ActionCanvas
package de.jbb.j2me.ll;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class ActionCanvas extends Canvas implements Movable {
private boolean drawRed = true;
private int pointSize = 10;
private int x = 0;
private int y = 0;
private int pointStep = 5;
private RepeatHelper repeater;
public ActionCanvas() {
repeater = new RepeatHelper(this, 40);
new Thread(repeater).start();
}
protected void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
if (drawRed) {
g.setColor(255, 0, 0);
}
else {
g.setColor(255, 255, 255);
}
g.fillArc(x, y, pointSize, pointSize, 0, 360);
}
public void movePoint(int keyCode) {
switch (getGameAction(keyCode)) {
case LEFT:
x -= pointStep;
break;
case RIGHT:
x += pointStep;
break;
case UP:
y -= pointStep;
break;
case DOWN:
y += pointStep;
break;
case FIRE:
drawRed = !drawRed;
break;
default:
switch (keyCode) {
case KEY_NUM2:
y -= pointStep;
break;
case KEY_NUM4:
x -= pointStep;
break;
case KEY_NUM6:
x += pointStep;
break;
case KEY_NUM8:
y += pointStep;
break;
case KEY_NUM5:
drawRed = !drawRed;
}
}
if (x < 0) {
x = 0;
}
else if (x + pointSize > getWidth()) {
x = getWidth() - pointSize;
}
else if (y < 0) {
y = 0;
}
else if (y + pointSize > getHeight()) {
y = getHeight() - pointSize;
}
repaint();
}
private void setBallTo(int x, int y) {
if (x < 0) {
x = 0;
}
if (x + pointSize > getWidth()) {
x = getWidth() - pointSize;
}
if (y < 0) {
y = 0;
}
if (y + pointSize > getHeight()) {
y = getHeight() - pointSize;
}
this.x = x;
this.y = y;
repaint();
}
protected void pointerDragged(int x, int y) {
setBallTo(x - pointSize / 2, y - pointSize / 2);
}
protected void pointerPressed(int x, int y) {
setBallTo(x - pointSize / 2, y - pointSize / 2);
}
protected void keyPressed(int keyCode) {
movePoint(keyCode);
if (getGameAction(keyCode) != FIRE && keyCode != KEY_NUM5) {
repeater.setKeyCode(keyCode);
}
}
protected void keyReleased(int keyCode) {
repeater.stopRepeating();
}
}
ActionMIDlet
package de.jbb.j2me.ll;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
public class ActionMIDlet extends MIDlet implements CommandListener {
private boolean first = true;
private Command exit;
private Display display;
private ActionCanvas canvas;
public void commandAction(Command c, Displayable d) {
if (exit.equals(c)) {
destroyApp(false);
}
}
protected void startApp() {
if (first) {
canvas = new ActionCanvas();
exit = new Command("Beenden", Command.EXIT, 0);
display = Display.getDisplay(this);
canvas.setTitle("Action Canvas");
canvas.addCommand(exit);
canvas.setCommandListener(this);
}
display.setCurrent(canvas);
}
protected void destroyApp(boolean unconditional) {
notifyDestroyed();
}
protected void pauseApp() {}
}
Pages: 1 2
Hallo, ich bin Anfänger und programmiere Java über Eclipse. Wie kann ich bei „Graphics“ eine Eingabe einer ganzen Zahl programmieren?
Danke für eine Hilfe.
Mit vielen Grüßen
P- Prüfer
Hallo Petra,
erst einmal die Frage (da Sie Anfängerin sind), ob Sie wissen, dass Sie sich im Java ME – also Java für mobile Endgeräte wie Handys – Kapitel befinden, und nicht im Java SE (für ganz normale Computer)?
Grüße
Stefan