package moverConTeclas;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Mover extends JFrame implements KeyListener{
private Kabuto kabuto = new Kabuto(); //JLabel que implementa Runnable
private PanelBotones pnBotones = new PanelBotones();
public Mover() {
JPanel superficie = new JPanel();
superficie.setLayout(null);
superficie.setBackground(Color.WHITE);
superficie.setBorder(BorderFactory.createLoweredSoftBevelBorder());
superficie.add(kabuto);
setLayout(new BorderLayout());
add(superficie, BorderLayout.CENTER);
add(pnBotones, BorderLayout.SOUTH);
addKeyListener(this);
setTitle("Mover con teclas");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
//Creamos hilo paralelo con el JLabel
Thread hilo = new Thread(kabuto);
hilo.start();
}
/*
* Clase JLabel que implementa Runnable para poder iniciar
* un hilo paralelo al programa principal.
* Este JLabel se desplazará en las direcciones que indiquen
* los distintos atributos booleans que posee.
* Los valores de estos booleans, cambiarán cuando ocurran
* eventos KeyPressed y KeyReleased
*/
private class Kabuto extends JLabel implements Runnable{
private int x = 0;
private int y = 0;
private ImageIcon imagen;
public boolean moverArriba = false;
public boolean moverAbajo = false;
public boolean moverIzquierda = false;
public boolean moverDerecha = false;
public Kabuto() {
imagen = new ImageIcon(Mover.class.getClassLoader().getResource("moverConTeclas/kabuto.jpeg"));
setIcon(imagen);
setBounds(x, y, imagen.getIconWidth(), imagen.getIconHeight());
}
private void subir() {
if (y > 0) {
y--;
setLocation(x, y);
}
}
private void bajar() {
if (y < 500) {
y++;
setLocation(x, y);
}
}
private void derecha() {
if (x < 500) {
x++;
setLocation(x, y);
}
}
private void izquierda() {
if (x > 0) {
x--;
setLocation(x, y);
}
}
@Override
public void run() {
while(true) {
System.out.println();
if (moverArriba) {
subir();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (moverAbajo) {
bajar();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (moverIzquierda) {
izquierda();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (moverDerecha) {
derecha();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private class PanelBotones extends JPanel {
private JButton btW;
private JButton btS;
private JButton btA;
private JButton btD;
public PanelBotones() {
btW = new JButton("W");
btW.setEnabled(false);
btW.setBackground(Color.WHITE);
btS = new JButton("S");
btS.setEnabled(false);
btS.setBackground(Color.WHITE);
btA = new JButton("A");
btA.setEnabled(false);
btA.setBackground(Color.WHITE);
btD = new JButton("D");
btD.setEnabled(false);
btD.setBackground(Color.WHITE);
JPanel arriba = new JPanel();
arriba.add(btW);
JPanel medio = new JPanel();
medio.add(btA);
medio.add(btD);
JPanel abajo = new JPanel();
abajo.add(btS);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(arriba);
add(medio);
add(abajo);
}
public void pulsarW(boolean pulsar) {
if (pulsar) {
btW.setBackground(Color.YELLOW);
}
else {
btW.setBackground(Color.WHITE);
}
}
public void pulsarS(boolean pulsar) {
if (pulsar) {
btS.setBackground(Color.YELLOW);
}
else {
btS.setBackground(Color.WHITE);
}
}
public void pulsarA(boolean pulsar) {
if (pulsar) {
btA.setBackground(Color.YELLOW);
}
else {
btA.setBackground(Color.WHITE);
}
}
public void pulsarD(boolean pulsar) {
if (pulsar) {
btD.setBackground(Color.YELLOW);
}
else {
btD.setBackground(Color.WHITE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Mover();
}
});
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
kabuto.moverArriba = true;
pnBotones.pulsarW(true);
break;
case KeyEvent.VK_S:
kabuto.moverAbajo = true;
pnBotones.pulsarS(true);
break;
case KeyEvent.VK_A:
kabuto.moverIzquierda = true;
pnBotones.pulsarA(true);
break;
case KeyEvent.VK_D:
kabuto.moverDerecha = true;
pnBotones.pulsarD(true);
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
kabuto.moverArriba = false;
pnBotones.pulsarW(false);
break;
case KeyEvent.VK_S:
kabuto.moverAbajo = false;
pnBotones.pulsarS(false);
break;
case KeyEvent.VK_A:
kabuto.moverIzquierda = false;
pnBotones.pulsarA(false);
break;
case KeyEvent.VK_D:
kabuto.moverDerecha = false;
pnBotones.pulsarD(false);
}
}
}