Java - keyDown

 
Vista:

keyDown

Publicado por oscar m (1 intervención) el 11/02/2006 00:41:20
Que tiene mal este código de applet?? no responde al teclado . . .

import java.applet.*;
import java.awt.*;
public class BallMove extends Applet implements Runnable
{
int x_pos = 30; // posicion x inicial del balon
int y_pos =30; // posicion y inicial del balon
int x_move = 1; // direccion del balon
int y_move = 0;
int radius = 10; // radio del balon
int appletsize_x = 300;
int appletsize_y = 300;

public void init()
{
setBackground(Color.blue);
}
public void start ()
{
Thread th = new Thread(this);
th.start();
}
public boolean KeyDown (Event e, int key)
{
if (key == Event.LEFT) //cursor izquierda
{
x_move = -1;
}
else if (key == Event.RIGHT) //cursor derecha
{
x_move = 1;
}
if (key == Event.UP) //cursor izquierda
{
y_move = -1;
}
else if (key == Event.DOWN) //cursor derecha
{
y_move = 1;
}
else if (key == 32) // barra espaciadora
{
x_move = 0;
}
return true;
}
public void run ()
{
while (true)
{
if (x_pos > appletsize_x - radius)
{
x_move = -1;
}
else if (x_pos < radius)
{
x_move = +1;
}
else if (y_pos >appletsize_y - radius)
{
y_move = -1;
}
else if (y_pos < radius)
{
y_move = +1;
}
x_pos += x_move;
y_pos += y_move;
repaint();
try {
Thread.sleep (10);
}
catch (InterruptedException ex){}
}
}
public void paint (Graphics g) {
g.setColor(Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder