I'm trying to launch a Game launcher and it was working fine until yesterday (I had most of the code written already) and then it just starts throwing Illegal state exceptions all over the place like theres no tomorrow.
And I for the life of me cannot figure out the solution :(
So this is my code:
package com.MHafi.Pandora.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import com.MHafi.Pandora.Configuration;
import com.MHafi.Pandora.Display;
import com.MHafi.Pandora.RunGame;
import com.MHafi.Pandora.input.InputHandler;
public class Launcher extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
protected JPanel window = new JPanel();
private JFrame frame = new JFrame();
Configuration config = new Configuration();
private int width = 800;
private int height = 390;
protected int button_width = 80;
protected int button_height = 39;
boolean running = false;
Thread thread;
private BufferedImage img;
public Launcher(int id) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
frame.setUndecorated(true);
frame.setTitle(Display.TITLE + " Launcher");
frame.setSize(new Dimension(width, height));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().add(window);
//frame.add(this);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
window.setLayout(null);
System.out.println("YAY");
InputHandler input = new InputHandler();
addKeyListener(input);
addFocusListener(input);
addMouseListener(input);
addMouseMotionListener(input);
startMenu();
frame.repaint();
}
public void updateFrame() {
if (InputHandler.dragged) {
Point p = frame.getLocation();
int x = frame.getX();
int y = frame.getY();
frame.setLocation (x + InputHandler.MouseDX - InputHandler.MousePX, y + InputHandler.MouseDY - InputHandler.MousePY);
}
}
public void startMenu() {
running = true;
thread = new Thread(this, "menu");
thread.start();
}
public void stopMenu() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
requestFocus();
while (running) {
renderMenu();
updateFrame();
}
}
private void renderMenu() throws IllegalStateException {
System.out.println("1");
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
System.out.println("2");
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 390);
System.out.println("3");
try {
g.drawImage(ImageIO.read(Launcher.class.getResource("/Launcher.png")), 0, 0, 800, 390, null);
System.out.println("4");
if (InputHandler.MouseX > 50 && InputHandler.MouseX < 50 + 60 && InputHandler.MouseY > 110 && InputHandler.MouseY < 110 + 25) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/play_on.png")), 50, 110, 60, 30, null);
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Pointer.png")), 115, 118, 30, 14, null);
if (InputHandler.MouseButton == 1) {
config.loadConfiguration("res/config/config.xml");
dispose();
new RunGame();
}
} else {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/play_off.png")), 50, 110, 60, 30, null);
}
if (InputHandler.MouseX > 50 && InputHandler.MouseX < 50 + 60 && InputHandler.MouseY > 140 && InputHandler.MouseY < 140 + 25) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Options_on.png")), 50, 141, 100, 30, null);
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Pointer.png")), 155, 149, 30, 14, null);
if (InputHandler.MouseButton == 1) {
System.out.println("Options");
}
} else {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Options_off.png")), 50, 140, 100, 30, null);
}
if (InputHandler.MouseX > 50 && InputHandler.MouseX < 50 + 60 && InputHandler.MouseY > 175 && InputHandler.MouseY < 175 + 25) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Help_on.png")), 50, 175, 60, 30, null);
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Pointer.png")), 115, 183, 30, 14, null);
} else {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Help_off.png")), 50, 175, 60, 30, null);
}
if (InputHandler.MouseX > 50 && InputHandler.MouseX < 50 + 60 && InputHandler.MouseY > 210 && InputHandler.MouseY < 210 + 25) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Quit_on.png")), 50, 210, 60, 30, null);
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Pointer.png")), 115, 218, 30, 14, null);
if (InputHandler.MouseButton == 1) {
System.exit(0);
}
} else {
g.drawImage(ImageIO.read(Launcher.class.getResource("/menu/Quit_off.png")), 50, 210, 60, 30, null);
}
} catch (IOException e) {
e.printStackTrace();
}
g.dispose();
bs.show();
}
}
And here is the error thrown:
YAY
1
Exception in thread "menu" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Window.createBufferStrategy(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Window.createBufferStrategy(Unknown Source)
So what's going on?
Why can't I create a BufferStrategy?
Well, a problem I see here is this call
createBufferStrategy(3)
in therenderMenu
method. The methodcreateBufferStrategy
can cause such exceptions if theJFrame
(or to be more general, theWindow
) wasn't set visible before (or added to a component that is visible) as you can see in the JavaDoc.I see two ways to fix it:
private JFrame frame
and if you should usethis
instead, sinceLauncher
is aJFrame
itself (you're settingframe
as visible, not theLauncher
instance)this.getBufferStrategy()
andcreateBufferStrategy(3)
toframe.getBufferStrategy()
andframe.createBufferStrategy(3)
If you used the second idea, then also think about removing
JFrame
as the parent class forLauncher
, because it creates an ownJFrame
instance, instead of using "himself" as theJFrame
.