Foros del Web » Programación para mayores de 30 ;) » Java »

Problemas con keypressed y menu de panel con boton

Estas en el tema de Problemas con keypressed y menu de panel con boton en el foro de Java en Foros del Web. Hola a todos, estoy ya casi finalizando un juego de naves en Java y tengo un gran problema. Tengo creado una clase Tadapter para capturarme ...
  #1 (permalink)  
Antiguo 05/05/2013, 06:06
 
Fecha de Ingreso: mayo-2013
Mensajes: 5
Antigüedad: 11 años
Puntos: 0
Problemas con keypressed y menu de panel con boton

Hola a todos,

estoy ya casi finalizando un juego de naves en Java y tengo un gran problema.

Tengo creado una clase Tadapter para capturarme las teclas de movimiento y disparo de la nave y funciona correctamente.

public class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent f) {

nave1.keyReleased(f);

}

public void keyPressed(KeyEvent f) {
nave1.keyPressed(f);
System.out.println("Funciona keyPressed");
}

Por otro lado tengo una metodo con cuatro Radio Button para elegir el nivel de dificultad y un boton para aceptar la eleccion.


El tema es que ejecuto el programa y me sale el menu del juego con el selector de dificultad y con el Systen.out.println veo que me captura el teclado, pero cuando marco cualquier Radio Button o el boton ya no me responde el teclado. Con lo que la nave ni se mueve ni dispara.

Antes de ponerle el menu del juego me iba perfectamente.

Seguro que es algo facil, pero estoy un poco verde.

Puedo enviaros el codigo si quereis verlo.

Muchas gracias por adelantado.



private void selectorDificultad() {

GrupoDificultad = new ButtonGroup();
jPanel1 = new JPanel();
jRadioButton1 = new JRadioButton();
jRadioButton2 = new JRadioButton();
jRadioButton3 = new JRadioButton();
jRadioButton4 = new JRadioButton();
jButton1 = new JButton();
jLabel1 = new JLabel();
jRadioButton1.setSelected(true);


jPanel1.setBackground(new java.awt.Color(0, 0, 0));

jRadioButton1.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton1);
jRadioButton1.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton1.setText("Facil");
jRadioButton1.setName("JRButtonFacil");
jRadioButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton1.isSelected())
jRadioButton1.setSelected(true);
nivel_dificultad = 5;
velo_alien = 4;
updateUI();
}
});


jRadioButton2.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton2);
jRadioButton2.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton2.setText("Normal");
jRadioButton2.setName("JRButtonNormal");
jRadioButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton2.isSelected())
jRadioButton2.setSelected(true);
nivel_dificultad = 7;
velo_alien = 6;
updateUI();
}
});


jRadioButton3.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton3);
jRadioButton3.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton3.setText("Complicado");
jRadioButton3.setName("JRButtonComplicado");
jRadioButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton3.isSelected())
jRadioButton3.setSelected(true);
nivel_dificultad = 10;
velo_alien = 10;
updateUI();
}
});

jRadioButton4.setBackground(new java.awt.Color(0, 0, 0));
GrupoDificultad.add(jRadioButton4);
jRadioButton4.setForeground(new java.awt.Color(255, 255, 255));
jRadioButton4.setText("Imposible");
jRadioButton4.setName("JRButtonImposible");
jRadioButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jRadioButton4.isSelected())
jRadioButton4.setSelected(true);
nivel_dificultad = 15;
velo_alien = 14;
updateUI();
}
});

jButton1.setBackground(new java.awt.Color(0, 0, 0));
jButton1.setText("Comienzo");
jButton1.setName("JButtonStart");
jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

alien1Activos = getAlien1();
alien2Activos = getAlien2();
InGame = true;
nave1.visible = true;

EliminarPanel();
}
});

jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Elegir nivel dificultad");
jLabel1.setName("JLabelElegirNivel");

GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(
jRadioButton3)
.addComponent(
jRadioButton2)
.addComponent(
jRadioButton4)
.addComponent(jLabel1)
.addComponent(
jRadioButton1))
.addContainerGap(
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(
GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(120, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(62, 62, 62)));
jPanel1Layout
.setVerticalGroup(jPanel1Layout
.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jLabel1,
GroupLayout.PREFERRED_SIZE,
25,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton1)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
jRadioButton3,
GroupLayout.PREFERRED_SIZE,
23,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton4)
.addPreferredGap(
LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addContainerGap(
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));

GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGro up(
javax.swing.GroupLayout.Alignment.LEADING).addGrou p(
layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(499, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup (
GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(400, Short.MAX_VALUE)));
}
  #2 (permalink)  
Antiguo 05/05/2013, 06:33
 
Fecha de Ingreso: mayo-2013
Mensajes: 5
Antigüedad: 11 años
Puntos: 0
Respuesta: Problemas con keypressed y menu de panel con boton

Pego el codigo en 2 mensajes.

Código:
package Pantallas;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle;
import javax.swing.Timer;
import Elementos.Alien1;
import Elementos.Alien2;
import Elementos.Misil;
import Elementos.Nave1;
import java.awt.Rectangle;

public class Intro extends JPanel implements ActionListener {

	public Image image;
	public int x;
	public int y;
	public int nivel_dificultad;
	public int velo_alien;
	public boolean InGame;
	
	
	public Timer timer;
	
	public Nave1 nave1;
	public Alien1 alien1;
	public Alien2 alien2;
	public ArrayList<Alien1> alien1Activos;
	public ArrayList<Alien2> alien2Activos;
	
	
	public ButtonGroup GrupoDificultad;
	public JButton jButton1;
	public JLabel jLabel1;
	public JPanel jPanel1;
	public JRadioButton jRadioButton1;
	public JRadioButton jRadioButton2;
	public JRadioButton jRadioButton3;
	public JRadioButton jRadioButton4;
		

	public Intro() {

		addKeyListener(new TAdapter());
		
		setFocusable(true);
		setBackground(Color.white);
		setDoubleBuffered(true);
		
		InGame = false;

		nave1 = new Nave1();
		alien1 = new Alien1();
		alien2 = new Alien2();

		alien1Activos = new ArrayList<Alien1>();
		alien2Activos = new ArrayList<Alien2>();
		
		//Ponemos por defecto estos valores, ya que Radio Button esta apretado por defecto en esta dificultad
		nivel_dificultad = 5;
		velo_alien = 2;
		
		x = 0;
		y = 0;
		
		timer = new Timer(5, this); // cada 5 ms llama actionPerformed
		timer.start();
		
	}

	
	public Image getPantallaImage() {
		return image;
	}
	
	public int getPantallaX() {
		return x;
	}

	public int getPantallaY() {
		return y;
	}

	public int getDificultad() {
		return nivel_dificultad;
	}

	public boolean getInGame() {
		return InGame;
	}
	
	
	public ArrayList<Alien1> getAlien1() {
		
	for (int i = 0; i < nivel_dificultad; i++) {
			alien1Activos.add(new Alien1());
		}
		return alien1Activos;
	}

	public ArrayList<Alien2> getAlien2() {
		
		for (int i = 0; i < nivel_dificultad; i++) {
			alien2Activos.add(new Alien2());
		}
		return alien2Activos;
	}

	public void CrearMenuPrincipal(){
		ImageIcon ii = new ImageIcon(this.getClass().getResource("../PantallaInicial_V2.jpg"));
		image = ii.getImage();
		selectorDificultad();
		
		
	}
	
	public void CrearStage1(){
		ImageIcon ii = new ImageIcon(this.getClass().getResource("../stage1.jpg"));
		image = ii.getImage();

	}
	
	
	public void FindeJuego(){
		ImageIcon ii = new ImageIcon(this.getClass().getResource("../GameOver.jpg"));
		image = ii.getImage();
		InGame=false;

	}
	
	public void EliminarPanel(){
		jPanel1.setVisible(false);
		jPanel1.setEnabled(false);
		jPanel1.removeAll();
		jButton1.setVisible(false);
		jButton1.removeAll();
		jButton1.setEnabled(false);
		jRadioButton1.setVisible(false);
		jRadioButton1.setEnabled(false);
		jRadioButton1.removeAll();
		jRadioButton2.setVisible(false);
		jRadioButton2.setEnabled(false);
		jRadioButton2.removeAll();
		jRadioButton3.setVisible(false);
		jRadioButton3.setEnabled(false);
		jRadioButton3.removeAll();
		jRadioButton4.setVisible(false);
		jRadioButton4.setEnabled(false);
		jRadioButton4.removeAll();
		jLabel1.setVisible(false);
		jLabel1.removeAll();
		GrupoDificultad.remove(null);
		updateUI();
	}
	
	
	public void checkCollisions() {

			// chequear colisiones de la nave con los alien1

			Rectangle r1 = nave1.ObtenerLimites();

			if (nave1.esVisible() || (InGame = true))
				for (int i = 0; i < alien1Activos.size(); i++) {
					Alien1 alien1x = (Alien1) alien1Activos.get(i);
					Rectangle r2 = alien1x.ObtenerLimites();

					if (r1.intersects(r2)) {
						nave1.visible = false;
						alien1x.visible = false;
						InGame = false;
					}
				}

			// chequear colisiones de la nave con los alien2

			if (nave1.esVisible() || (InGame = true))
				for (int i = 0; i < alien2Activos.size(); i++) {
					Alien2 alien2x = (Alien2) alien2Activos.get(i);
					Rectangle r3 = alien2x.ObtenerLimites();

					if (r1.intersects(r3)) {
						nave1.visible = false;
						alien2x.visible = false;
						InGame = false;
					}
				}

			// chequear colisiones de los misiles con alien1
			ArrayList<Misil> misiles = nave1.getMissiles();
			
			if (nave1.esVisible() || (InGame = true))
			    for (int i = 0; i < misiles.size(); i++) {
				   Misil misilx = (Misil) misiles.get(i);
                   Rectangle r4 = misilx.ObtenerLimites();

				for (int j = 0; j < alien1Activos.size(); j++) {
					Alien1 alien1x = (Alien1) alien1Activos.get(j);
					Rectangle r2 = alien1x.ObtenerLimites();

					if (r4.intersects(r2)) {
						alien1x.visible = false;
						misilx.visible = false;
					}
				}
			}

			// chequear colisiones de los misiles con alien2
			if (nave1.esVisible() || (InGame = true))
			for (int i = 0; i < misiles.size(); i++) {
				Misil misilx = (Misil) misiles.get(i);

				Rectangle r4 = misilx.ObtenerLimites();

				for (int j = 0; j < alien2Activos.size(); j++) {
					Alien2 alien2x = (Alien2) alien2Activos.get(j);
					Rectangle r3 = alien2x.ObtenerLimites();

					if (r4.intersects(r3)) {
						misilx.visible = false;
						alien2x.visible = false;
					}

				}
			}

		}
  #3 (permalink)  
Antiguo 05/05/2013, 06:35
 
Fecha de Ingreso: mayo-2013
Mensajes: 5
Antigüedad: 11 años
Puntos: 0
Respuesta: Problemas con keypressed y menu de panel con boton

0000000000

Última edición por chisbi; 09/05/2013 a las 05:12

Etiquetas: boton, clase, metodo, panel, programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:13.