I am coding a small game and want to have a start screen that switches to the game. I am able to switch JPanels, but after I do, the keyListener I have implemented in said new JPanel is not doing anything. I know that the new JPanel works alone because I have tested it. Here is my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class ADGame extends JPanel implements ActionListener, KeyListener{
Timer t = new Timer(2, this);
private static ImageIcon rocket;
private ImageIcon asteroid;
private ImageIcon bullet;
private static JLabel rocketlabel;
private JLabel ast1;
private JLabel ast2;
private JLabel ast3;
private JLabel bulletLabel;
public static int y=90,dy=0,bulletX=110,bulletY,i=0,score=0;
public static JLabel altitude = new JLabel();
public static JLabel canFire = new JLabel("Blaster Charged: 100%");
static boolean bulletFired=false, playing= false;
static JFrame f = new JFrame();
ADGame(){
this.setBackground(Color.black);
rocket = new ImageIcon(getClass().getResource("rocketFinal.png"));
rocketlabel= new JLabel(rocket);
this.add(rocketlabel);
asteroid = new ImageIcon(getClass().getResource("asteroid.png"));
ast1=new JLabel(asteroid);
ast2=new JLabel(asteroid);
ast3=new JLabel(asteroid);
bullet = new ImageIcon(getClass().getResource("bulletReal.png"));
bulletLabel = new JLabel(bullet);
bulletLabel.setVisible(false);
altitude.setBounds(270,285,200,20);
altitude.setVisible(true);
altitude.setForeground(Color.yellow);
canFire.setBounds(350,285,200,20);
canFire.setVisible(true);
canFire.setForeground(Color.green);
this.add(altitude);this.add(canFire);
this.add(ast1);this.add(ast2);this.add(ast3);this.add(bulletLabel);
f.addKeyListener(this);
this.setLayout(null);
this.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode== KeyEvent.VK_UP) {
dy=-1;
}
if(keyCode== KeyEvent.VK_DOWN) {
dy=1;
}
if(keyCode== KeyEvent.VK_SPACE) {
if(bulletFired==false) {
bulletFired = true;
bulletY = y;
bulletX=110;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_UP: dy=0; break;
case KeyEvent.VK_DOWN: dy=0; break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static class StartScreen extends JPanel{
public final Color screen = new Color(252,3,102);
Font myfont = new Font("Helvetica", 1, 15);
JButton start;
JLabel AD, score;
ImageIcon title,begin;
StartScreen(){
ImageIcon title = new ImageIcon(getClass().getResource("ADtitle.png"));
JLabel AD = new JLabel(title);
AD.setForeground(Color.yellow);
AD.setBounds(0,-20,500,375);
JButton start = new JButton("Start",title);
start.setHorizontalTextPosition(JButton.CENTER);
start.setVerticalTextPosition(JButton.CENTER);
start.setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));
start.setBounds(216,155,68,15);
start.setFont(myfont);
start.setBackground(this.getBackground());;
start.setForeground(screen);
start.addActionListener(new startListener());
this.add(start);
this.add(AD);
this.setBackground(screen);
this.setVisible(true);
this.setLayout(null);
}
public class startListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent a) {
ADGame gamePanel = new ADGame();
StartScreen StartScreen = new StartScreen();
f.getContentPane().remove(StartScreen);
f.getContentPane().add(gamePanel);
f.validate();
f.repaint();
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
rocketlabel.setBounds(45,y,rocket.getIconWidth(),80);
fireBullet();
paintStars(g);
t.start();
}
public void paintStars(Graphics g) {
g.setColor(Color.yellow);
for(int i=0; i<5;i++) {
Random rand = new Random();
int o = rand.nextInt(500);
int p = rand.nextInt(300);
g.fillOval(o, p, 3, 3);
}
}
public void actionPerformed(ActionEvent e) {
if(y==-20) y=249;
if(y==250)y=-20;
y+=dy;
if(bulletFired==true) {
bulletX++;
if(bulletX>455) {bulletFired=false;bulletLabel.setVisible(false);canFire.setText("Blaster Charged: 100%");canFire.setForeground(Color.green);}
else {canFire.setText("Charging Blaster: "+String.valueOf(100*bulletX/445)+"%"); canFire.setForeground(Color.yellow);}
}
altitude.setText("Altitude: "+String.valueOf(335-y));
repaint();
}
public void fireBullet(){
if(bulletFired==true) {
bulletLabel.setVisible(true);
bulletLabel.setBounds(bulletX,bulletY+25,bullet.getIconHeight(),bullet.getIconWidth());
}
}
public static void main(String[] args) {
String filepath = "SpaceGameMusic.wav";
MusicLooper musicPlayer = new MusicLooper();
musicPlayer.playMusic(filepath);
ADGame gamePanel = new ADGame();
StartScreen startt = new StartScreen();
f.setTitle("Asteroid Defender");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,335);
f.setVisible(true);
f.setResizable(false);
f.getContentPane().add(startt);
}
}
I know it is messy, I started and just kept adding.