Ok maybe you guys can help, heres my code. Apparently something is wrong with my buffer strategys when I create the buffer. Any ideas?? This code has worked fine in my games…!
Heres the main Window class
package userInterface;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MenuBar;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.border.Border;
public class Window {
//window areas
private JFrame mainFrame;
private JPanel canvasPanel;
private JPanel tileLibraryPanel;
//drawing canvases
private TileLibraryCanvas tileLibraryCanvas;
private MapCanvas mapCanvas;
//menubar
private JMenuBar menuBar;
private JMenu fileButton;
//borders
Border tileLibraryBorder;
Border canvasAreaBorder;
public Window() {
//initializng the frame and inner panels
mainFrame = new JFrame("Map Editor");
canvasPanel = new JPanel();
tileLibraryPanel = new JPanel();
//initializing the canvases
tileLibraryCanvas = new TileLibraryCanvas();
mapCanvas = new MapCanvas();
//initializing menu bar and items
menuBar = new JMenuBar();
fileButton = new JMenu("File");
//adding the menubar to the frame
mainFrame.setJMenuBar(menuBar);
//adding the filebutton to the menu bar
menuBar.add(fileButton);
//creating borders
Border tileLibraryBorder = BorderFactory.createTitledBorder("Tile Library");
Border canvasAreaBorder = BorderFactory.createTitledBorder("Map");
//setting panel sizes
canvasPanel.setPreferredSize(new Dimension(600, 600));
tileLibraryPanel.setPreferredSize(new Dimension(200, 600));
//adding things to panels
canvasPanel.setBorder(canvasAreaBorder);
tileLibraryPanel.setBorder(tileLibraryBorder);
canvasPanel.add(mapCanvas);
tileLibraryPanel.add(tileLibraryCanvas);
//setting layout and adding panels to frame
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.getContentPane().add(canvasPanel, BorderLayout.CENTER);
mainFrame.getContentPane().add(tileLibraryPanel, BorderLayout.WEST);
//setting various attributes
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setPreferredSize(new Dimension(800, 600));
mainFrame.setResizable(false);
mainFrame.pack();
mainFrame.setVisible(true);
}
}
And here are the two canvases, they are identical basically
package userInterface;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
public class MapCanvas extends Canvas {
private static final long serialVersionUID = 5075462132756421808L;
BufferStrategy bufferStrategy;
public MapCanvas() {
setVisible(true);
setIgnoreRepaint(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
}
public void render() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, 90, 50);
}
}
package userInterface;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
public class TileLibraryCanvas extends Canvas {
BufferStrategy bufferStrategy;
public TileLibraryCanvas() {
setVisible(true);
setIgnoreRepaint(true);
createBufferStrategy(2);
bufferStrategy = getBufferStrategy();
}
public void render() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, 90, 50);
}
}
Heres the error:
Exception in thread “main” java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
at java.awt.Component$FlipBufferStrategy.(Unknown Source)
at java.awt.Component$FlipSubRegionBufferStrategy.(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at java.awt.Component.createBufferStrategy(Unknown Source)
at java.awt.Canvas.createBufferStrategy(Unknown Source)
at userInterface.TileLibraryCanvas.(TileLibraryCanvas.java:19)
at userInterface.Window.(Window.java:48)
at userInterface.Driver.main(Driver.java:7)
I want to note that if I pass 1 to createBufferStrategy() it compiles and runs, but the canvases are not painted so Im not even sure if theyre there.