import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Razona extends Canvas implements KeyListener
{
boolean Inicio = true;
int x_a, x_n, y, Movimiento = 0;
Transporta B, Z, P, M;
String LETRA;
public Razona()
{
addKeyListener(this);
this.requestFocusInWindow();
}
void SecuenciaDeMovimiento()
{
// (Objeto_1, Objeto_2, Direccion)
// true = derecha, false = izquierda
B.CORRIENDO = true; Z.CORRIENDO = true;
P.CORRIENDO = true; M.CORRIENDO = true;
switch(Movimiento)
{
case 1:
B.Positivo = true; new Thread(B).start();
P.Positivo = true; new Thread(P).start();
break;
case 2:
B.Positivo = false; new Thread(B).start();
break;
case 3:
B.Positivo = true; new Thread(B).start();
Z.Positivo = true; new Thread(Z).start();
break;
case 4:
B.Positivo = false; new Thread(B).start();
P.Positivo = false; new Thread(P).start();
break;
case 5:
B.Positivo = true; new Thread(B).start();
M.Positivo = true; new Thread(M).start();
break;
case 6:
B.Positivo = false; new Thread(B).start();
break;
case 7:
B.Positivo = true; new Thread(B).start();
P.Positivo = true; new Thread(P).start();
break;
default:
ALTO();
}
}
public void paint(Graphics G)
{
if (Inicio)
{
G.setColor(Color.black);
G.fillRect(0, 0, 570, 310);
G.setColor(Color.yellow);
G.drawLine(150, 10, 150, 280);
G.drawLine(400, 10, 400, 280);
G.drawString("B", 155, 30);
G.drawString("Z", 155, 70);
G.drawString("P", 155, 110);
G.drawString("M", 155, 150);
Inicio = false;
}
else
{
G.setColor(Color.black); G.drawString(LETRA, x_a, y);
G.setColor(Color.yellow); G.drawString(LETRA, x_n, y);
}
}
public void update(Graphics G) { paint(G); }
public void Imprime(int x_a, int x_n, int y, String LETRA)
{
this.x_a = x_a;
this.x_n = x_n;
this.y = y;
this.LETRA = LETRA;
repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e)
{
String Tecla = e.getKeyText(e.getKeyCode());
if (Tecla.equals("Introduzca")) // Es el enter
{
if (B == null && Z == null && P == null && M == null)
{
// Transporta (pos_y, Objeto, referencia)
B = new Transporta( 30, "B", this);
Z = new Transporta( 70, "Z", this);
P = new Transporta(110, "P", this);
M = new Transporta(150, "M", this);
Movimiento = 0;
}
Movimiento++;
SecuenciaDeMovimiento();
}
}
void ALTO()
{
if (B!= null) { B.CORRIENDO = false; B = null; }
if (Z!= null) { Z.CORRIENDO = false; Z = null; }
if (P!= null) { P.CORRIENDO = false; P = null; }
if (M!= null) { M.CORRIENDO = false; M = null; }
this.setVisible(false);
Inicio = true;
}
}
class Transporta extends Thread
{
int xi = 155, xf = 390, xa, xn, y; //
Razona RAZONA;
boolean CORRIENDO = false, Positivo = true;
String Letra;
public Transporta(int y, String Letra, Razona RAZONA)
{
this.y = y;
this.Letra = Letra;
this.RAZONA = RAZONA;
this.xn = xi;
}
public void run( )
{
while(CORRIENDO)
{
try { Thread.sleep(50); }
catch(InterruptedException r){}
xa = xn;
xn = (Positivo) ? xn+2 : xn-2;
if (Positivo && xn>xf || !Positivo && xn<xi)
CORRIENDO = false;
RAZONA.Imprime(xa, xn, y, Letra);
}
}
}