I am creating a Tetris clone as a personal project. In order to switch between the menus, I am using a CardLayout
, but I have come across a problem. When I switch from one menu to the other, the focus does not transfer to the other JPanel
. How can I fix this?
Here is my JFrame:
package com.cgp.tetris;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TetrisFrame extends JFrame {
private static final long serialVersionUID = 1L;
TetrisMenu tm = new TetrisMenu();
static SinglePlayerMenu spm = new SinglePlayerMenu();
public static CardLayout cl = new CardLayout();
static JPanel parentComponent = new JPanel(cl);
public static void main(String[] args) {
new TetrisFrame();
}
public TetrisFrame() {
setTitle("Tetris");
setSize(new Dimension(646, 604));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width / 2) - 323, (d.height / 2) - 302);
parentComponent.add(tm, "tm");
parentComponent.add(spm, "spm");
cl.show(parentComponent, "tm");
add(parentComponent);
}
public static void replace() {
cl.show(parentComponent, "spm");
spm.startClip();
}
}
Here is my first JPanel/menu:
package com.cgp.tetris;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class TetrisMenu extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private BufferedImage titletop, titlebottom, selector;
private boolean left = true, right = false;
private AudioInputStream themestream;
private Clip clip;
public TetrisMenu() {
super();
}
public void run() {
requestFocus(true);
loadImages();
sound();
bind();
while (true) {
repaint();
}
}
private void sound() {
File theme = new File("res/theme.wav");
try {
themestream = AudioSystem.getAudioInputStream(theme);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
clip = null;
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
try {
clip.open(themestream);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
clip.start();
}
private void bind() {
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke("LEFT"), "left");
am.put("left", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
left = true;
right = false;
}
});
im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
am.put("right", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
System.out.println("2");
right = true;
left = false;
}
});
im.put(KeyStroke.getKeyStroke("SPACE"), "space");
am.put("space", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (left) {
TetrisFrame.replace();
clip.stop();
thread.stop();a
}
}
});
}
private void loadImages() {
try {
titletop = ImageIO.read(new File("res/tetrispic.png"));
titlebottom = ImageIO.read(new File("res/titlebottom.png"));
selector = ImageIO.read(new File("res/selector.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(titletop, 0, 0, 640, 440, null);
g.drawImage(titlebottom, 0, 440, 640, 136, null);
if (left) {
g.drawImage(selector, 36, 452, 16, 24, null);
} else if (right) {
g.drawImage(selector, 356, 452, 16, 24, null);
}
}
}
Here is my second JPanel/menu:
package com.cgp.tetris;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class SinglePlayerMenu extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private AudioInputStream themestream;
private Clip clip;
private BufferedImage base, bi1, bi2, bi3, bi4, bi1s, bi2s, bi3s, bi4s;
private boolean typeselected = false, b1 = true, b2 = false, b3 = true, b4 = false, b5 = false, b6 = false;
public SinglePlayerMenu() {
super();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(base, 0, 0, 640, 576, null);
if (b1) {
g.drawImage(bi1s, 80, 156, 240, 40, null);
} else if (!b1) {
g.drawImage(bi1, 80, 156, 240, 40, null);
}
if (b2) {
g.drawImage(bi2s, 324, 156, 236, 40, null);
} else if (!b2) {
g.drawImage(bi2, 324, 156, 236, 40, null);
}
}
public void run() {
sound();
loadImages();
bind();
while (true) {
repaint();
}
}
private void bind() {
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke("LEFT"), "left");
am.put("left", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (!typeselected) {
b1 = true;
b2 = false;
}
}
});
im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
am.put("right", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (!typeselected) {
b2 = true;
b1 = false;
}
}
});
}
private void loadImages() {
try {
base = ImageIO.read(new File("res/base.png"));
bi1 = ImageIO.read(new File("res/1.png"));
bi2 = ImageIO.read(new File("res/2.png"));
bi3 = ImageIO.read(new File("res/3.png"));
bi4 = ImageIO.read(new File("res/4.png"));
bi1s = ImageIO.read(new File("res/1s.png"));
bi2s = ImageIO.read(new File("res/2s.png"));
bi3s = ImageIO.read(new File("res/3s.png"));
bi4s = ImageIO.read(new File("res/4s.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
private void sound() {
File theme = new File("res/playtheme.wav");
try {
themestream = AudioSystem.getAudioInputStream(theme);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
clip = null;
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
try {
clip.open(themestream);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void startClip() {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
Thanks in advance!