J
java123
Gast
Hi!
Ich lerne gerade den KeyListener. doch irgendwie scheint nichts zu passieren, wenn ich die tastatur betätige. hoffe ihr wisst was da falsch sein könnte.
danke im voraus!
Ich lerne gerade den KeyListener. doch irgendwie scheint nichts zu passieren, wenn ich die tastatur betätige. hoffe ihr wisst was da falsch sein könnte.
danke im voraus!
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet implements Runnable, MouseListener, KeyListener {
int x_pos = 20;
int y_pos = 100;
int radius = 20;
int appletsize_x = 200;
int appletsize_Y = 300;
int x_speed = 1;
int currentLine;
AudioClip bumb;
private Image dbgImage;
private Graphics dbg;
private Image hintergrund;
public void init() {
setBackground(Color.LIGHT_GRAY);
bumb = getAudioClip(getCodeBase(), "bump.wav");
hintergrund = getImage(getCodeBase(), "hintergrund.jpg");
addMouseListener(this);
addKeyListener(this);
} // ende init()
// Aktionen
public void mouseClicked(MouseEvent e) {
x_speed =- (x_speed);
}
public void keyPressed(KeyEvent en) {
System.out.println("hallo");
if (en.getKeyCode() == KeyEvent.VK_LEFT) {
x_speed = -1;
}
}
public void start() {
Thread th = new Thread(this);
th.start();
} // ende start()
public void stop() {
} // ende stop()
public void destroy() {
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true) {
// wenn rechte seite
if(x_pos > appletsize_x - radius) {
x_speed = -1;
bumb.play();
}
// wenn linke seite
else if(x_pos < radius) {
x_speed = +1;
bumb.play();
}
// Veränderung der x position
x_pos += x_speed;
repaint();
try {
Thread.sleep(20);
}
catch(InterruptedException ex) {
// nix
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
} // ende run()
// Bildschirmflimmern beseitigen (Doppelbufferung)
public void update(Graphics g) {
if(dbgImage == null) {
dbgImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbgImage.getGraphics();
}
// Bildschirm in Hintergrund löschen
dbg.setColor(getBackground());
dbg.fillRect(0,0,this.getSize().width, this.getSize().height);
// Bildschirm in Vordergrund zeichnen
dbg.setColor(getForeground());
paint(dbg);
// Ausgeben
g.drawImage(dbgImage,0,0,this);
} // ende update
public void paint(Graphics g) {
g.drawImage(hintergrund,0,0,this);
g.setColor(Color.red);
// Kugel zeichen
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
} // ende paint()
}