04/05/2013, 20:24
|
| | Fecha de Ingreso: mayo-2013 Ubicación: Madrid
Mensajes: 4
Antigüedad: 11 años, 8 meses Puntos: 0 | |
Respuesta: KeyListener deja de funcionar
Código:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Image;
import java.util.Iterator;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
* Write a description of class MenuInicial2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ventana extends JPanel implements ActionListener
{
/*****************************
* Declaration Variable Zone *
*****************************/
private int level = 0;
private int counter = 0;
/***************************
* Declaration Object Zone *
***************************/
Background background;
Spacecraft spacecraft;
ArrayList<MartianGroup1> martianGroup1;
ArrayList<MartianGroup2> martianGroup2;
ArrayList<MartianGroup3> martianGroup3;
ArrayList<MartianGroup4> martianGroup4;
/********************
* Auxiliar Objects *
********************/
Timer timer;
public Ventana(int level)
{
this.level = level;
addKeyListener(new KeyboardInterface());
setFocusable(true);
setDoubleBuffered(true);
initializeMartianGroup1();
initializeMartianGroup2();
initializeMartianGroup3();
initializeMartianGroup4();
background = new Background();
spacecraft = new Spacecraft();
timer = new Timer(5, this);
timer.start();
}
public void initializeMartianGroup1()
{
martianGroup1 = new ArrayList<MartianGroup1>();
if (martianGroup1.isEmpty() == false)
{
martianGroup1.clear();
}
for (int position=0; position < 5; position++)
{
martianGroup1.add(position, new MartianGroup1(position));
}
}
public void paint (Graphics g)
{
super.paint(g);
g.drawImage(background.getImage(), background.getX(), background.getY(), this);
switch(counter)
{
case 0: paintGroup1(g); break;
case 1: paintGroup2(g); break;
case 2: paintGroup3(g); break;
case 3: paintGroup4(g); break;
default: break;
}
paintShots(g);
g.drawImage(spacecraft.getImage(), spacecraft.getX(), spacecraft.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e)
{
switch(counter)
{
case 0: moveGroup1(); checkCollisionsGroup1(); break;
case 1: moveGroup2(); checkCollisionsGroup2(); break;
case 2: moveGroup3(); checkCollisionsGroup3(); break;
case 3: moveGroup4(); checkCollisionsGroup4(); break;
default: break;
}
ArrayList<Shot> activeShots = spacecraft.getShots();
for (int i = 0; i < activeShots.size(); i++)
{
Shot shot = activeShots.get(i);
if (shot.esVisible())
{
shot.move();
}
else { activeShots.remove(i); }
}
spacecraft.move();
repaint();
}
public void paintGroup1(Graphics g)
{
MartianGroup1 martianInUse;
Iterator<MartianGroup1> martianIterator = martianGroup1.iterator();
while(martianIterator.hasNext())
{
martianInUse = martianIterator.next();
if (martianInUse.getVisible() == true)
{
g.drawImage(martianInUse.getImage(), martianInUse.getX(), martianInUse.getY(), this);
}
}
}
public void paintShots(Graphics g)
{
ArrayList<Shot> activeShots = spacecraft.getShots();
for (int i = 0; i < activeShots.size(); i++)
{
Shot shot = activeShots.get(i);
if (shot.esVisible())
{
g.drawImage(shot.getImage(), shot.getX(), shot.getY(), this);
}
}
}
public void moveGroup1()
{
int martianCounter = 0;
boolean visible = false;
MartianGroup1 martianInUse;
Iterator<MartianGroup1> martianIterator = martianGroup1.iterator();
while(martianIterator.hasNext())
{
martianInUse = martianIterator.next();
martianInUse.move(martianCounter);
if (martianIterator.hasNext() == false && martianInUse.getX() == (-60))
{
int position = 0;
Iterator<MartianGroup1> martianIterator2 = martianGroup1.iterator();
while(martianIterator2.hasNext())
{
martianIterator2.next().restart(position);
position++;
}
counter++;
}
if (martianInUse.getVisible()) { visible = true; }
martianCounter++;
}
if (!visible) { counter++; }
}
public void checkCollisionsGroup1()
{
Rectangle r3 = spacecraft.getBounds();
for (int i = 0; i < martianGroup1.size(); i++)
{
Rectangle r2 = martianGroup1.get(i).getBounds();
if (r3.intersects(r2))
{
spacecraft.setVisible(false);
}
}
ArrayList<Shot> activeShots = spacecraft.getShots();
for (int j = 0; j < activeShots.size(); j++)
{
Shot shot = activeShots.get(j);
Rectangle r1 = shot.getBounds();
for (int i = 0; i < martianGroup1.size(); i++)
{
if(martianGroup1.get(i).getVisible())
{
Rectangle r2 = martianGroup1.get(i).getBounds();
if (r1.intersects(r2))
{
shot.setVisible(false);
martianGroup1.get(i).setVisible(false);
}
}
}
}
}
public class KeyboardInterface extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
spacecraft.keyPressed(e);
}
public void keyReleased(KeyEvent e)
{
spacecraft.keyReleased(e);
}
}
}
|