Download La clase Nave para crear los objetos nave

Document related concepts
no text concepts found
Transcript
//La clase Nave para crear los objetos nave
import java.awt.event.*;
import javax.swing.*;
class Nave extends JLabel{
int x;
int y;
int dx;
int dy;
ImageIcon imagen;
public Nave(int x, int y) {
this.x = x;
this.y = y;
imagen = new ImageIcon("nave.gif");
}
public void mover(int x,int y)
{
this.x=x;
this.y=y;
}
public void dibujaNave()
{
this.setIcon(imagen);
this.setBounds(this.x,this.y,this.imagen.getIconWidth(),this.imagen.getIconHeig
ht());
}
}
//Principal para llamar a un objeto y moverlo en pantalla
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PrincipalNave extends JFrame implements KeyListener
{
int x=10;
int y=10;
Nave nave;
public PrincipalNave()
{
Container con = getContentPane();
con.setLayout(null);
nave = new Nave(10,10);
nave.dibujaNave();
con.add(nave);
addKeyListener(this);
setSize(300,300);
setVisible(true);
}
public void keyPressed(KeyEvent e){
int tecla = e.getKeyCode();
if(tecla == 37) // mover para la izquierda
nave.mover(x--,y);
if(tecla == 39) // mover para la derecha
nave.mover(x++,y);
if(tecla == 38) // mover para arriba
nave.mover(x,y--);
if(tecla == 40) // mover para abajo
nave.mover(x,y++);
nave.dibujaNave();
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public static void main (String[] args) {
new PrincipalNave();
}
}
Related documents