Reputation: 3
I have made a pacman game and I wanted to display a JFrame
as a bootload to the game itself.
So Iv'e made a class launcher.java extends JFrame
containing a single JPanel
to present the image of the game.
In a different class called Game.java which also extends JFrame
Iv'e set a timer and build the game in GUI with setVisible(false)
.
When activating the game the launcher shows for three seconds and then the timer is triggered and the game runs, although the keylisteners doesn't respond.
Please please help me understand why.
The Launcher Class
package PacMan;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Launcher extends JFrame {
private static final long serialVersionUID = -7949539876450449197L;
private JPanel launchImage;
public Launcher() {
launchImage = new JPanel();
launchImage.add(new JLabel(new ImageIcon("main.png")));
launchImage.setVisible(true);
setUndecorated(true);
setSize(560,620);
setLocationRelativeTo(null);
getContentPane().add(launchImage);
setVisible(true);
}
}
package PacMan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Game extends JFrame implements MouseListener, KeyListener, ActionListener{
private static final long serialVersionUID = -7132848196338970451L;
int posX=0,posY=0;
private Controller upper;
private JLabel[] lives;
public static Timer timer;
private int livesCounter, lifeIndex;
private JPanel livesPanel;
private double secondsSaver;
private Launcher launcher;
public Game() {
timer = new Timer(50,this);
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
setUndecorated(true);
setSize(560,620);
setLocationRelativeTo(null);
this.upper = new Controller();
this.addKeyListener(upper);
upper.setBorder(BorderFactory.createEmptyBorder(-5, -5, -5, -5));
upper.setBounds(-5, -5, -5, -5);
JPanel lower = new JPanel();
lower.setBackground(Color.BLACK);
lower.setLayout(new GridLayout(1,4));
lower.setSize(560, 100);
JLabel livesLabel = new JLabel(new ImageIcon("lives.png"));
livesLabel.setHorizontalTextPosition(new Integer(0));
lower.add(livesLabel);
livesPanel = new JPanel();
livesPanel.setLayout(new GridLayout(1,3));
lives = new JLabel[3];
for(int i=0; i<3; i++){
lives[i] = new JLabel(new ImageIcon("life.png"));
livesPanel.add(lives[i]);
}
livesPanel.setOpaque(false);
lower.add(livesPanel);
lifeIndex = 2;
livesCounter=3;
JButton restart = new JButton(new ImageIcon("Restart.png"));
restart.setBackground(Color.black);
restart.setBorderPainted(false);
lower.add(restart);
restart.addActionListener(new ActionListener() { // action for the quit button
public void actionPerformed(ActionEvent arg0) {
getContentPane().remove(upper);
upper = new Controller();
}
});
JButton quit = new JButton(new ImageIcon("quit.png"));
quit.setBackground(Color.black);
quit.setBorderPainted(false);
lower.add(quit);
quit.addActionListener(new ActionListener() { // action for the quit button
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
upper.getComponentListeners();
gamePanel.add(upper, BorderLayout.NORTH);
gamePanel.add(lower, BorderLayout.SOUTH);
getContentPane().add(gamePanel);
pack();
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
posX=e.getX();
posY=e.getY();
}
});
this.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent evt) {
//sets frame position when mouse dragged
setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);
}
});
timer.start();
launcher = new Launcher();
}
public Controller getController(){
return upper;
}
public static void main(String args[]) {
new Game();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (secondsSaver==3) {
this.setVisible(true);
launcher.setVisible(false);
}
if ((livesCounter>upper.getLives()) && (secondsSaver>=3)) {
lives[lifeIndex].setVisible(false);
lifeIndex--;
}
this.livesCounter=upper.getLives();
secondsSaver+=0.05;
}
}
Upvotes: 0
Views: 164
Reputation: 347314
I'd recommend against using KeyListener. It has a particular issue in that the component it is registered to must not only be focusable, but must have focus before key events will be triggered.
Instead, I'd suggest using the Key Bindings API
I'd also recommend that you make a call to requetsFocusInWindow
in the game frame within your actionListener
when you close the boot screen
Upvotes: 2