Clase juegoInicio:
Código:
import javax.swing.JFrame;
public class JuegoInicio extends JFrame
{
public JuegoInicio()
{
add(new Tablero());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setTitle("R-Type");
setResizable(false);
setVisible(true);
}
public void main(String[] args)
{
new JuegoInicio();
}
}
Clase Tablero:
Código:
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import javax.swing.JPanel;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
public class Tablero extends JPanel implements ActionListener
{
private Timer timer;
private Craft craft;
public Tablero()
{
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
craft = new Craft();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
g2d.drawImage(craft.getImage(), craft.getX() ,getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e)
{
craft.move();
repaint(); //vuelve a ejecutar el metodo paint
}
public class TAdapter extends KeyAdapter {
public void KeyReleased(KeyEvent e) {
craft.keyReleased(e);
}
public void KeyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}
}
Clase Craft:
Código:
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import javax.swing.JPanel;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
public class Craft
{
private String craft = "Logo.jpg";//ruta donde esta la imagen que queremos pintar en pantalla
private int dx;//desplazamiento x
private int dy;//desplazamiento y
private int x;
private int y;
private Image image;//objeto que va a contener la imagen
public Craft()
{
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));//extrae la imagen que tenemos guardada
image = ii.getImage();//almacena la imagen en un objeto
x=40;
y=60;
}
public void move()//cuando se detecte una pulsación de tecla se llama a este metodo
{
x += dx;//desplazamiento x
y += dy;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Image getImage()
{
return image;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
dx = -1;
}
if(key == KeyEvent.VK_RIGHT)
{
dy = 1;
}
if(key == KeyEvent.VK_UP)
{
dx = -1;
}
if(key == KeyEvent.VK_DOWN)
{
dy = 1;
}
}
public void keyReleased(KeyEvent e)//para dejar de mover nuestra imagen al soltar la tecla, se hace en 2 metodos separados porque si no al soltar una de las teclas la nave se queda parada.
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
dx=0;
}
if(key == KeyEvent.VK_RIGHT)
{
dy=0;
}
if(key == KeyEvent.VK_UP)
{
dx=0;
}
if(key == KeyEvent.VK_DOWN)
{
dy=0;
}
}
}