I am creating a game and want to draw to a Canvas using it's BufferStrategy. For some reason it doesn't seem to want to display anything on the screen. I've looked at the Java Documentation and read several Stackoverflow questions that others have asked, but can't get it to work. Just wanting to display a black background so that I know it is working.
Here is where I create the Canvas:
public static void createDisplay() {
if (frame != null)
return;
URL path = Display.class.getResource(ICON_PATH);
Image icon = Toolkit.getDefaultToolkit().getImage(path);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration config = env.getDefaultScreenDevice().getDefaultConfiguration();
Display.frame = new Frame(FRAME_TITLE, config);
Display.canvas = new Canvas(config);
frame.setMinimumSize(MIMIMUM_FRAME_SIZE);
frame.setIconImage(icon);
frame.addWindowListener(this); // For closing frame
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.add(canvas);
frame.setVisible(true);
}
And here is where I try to display the black background:
public void drawFrame() {
Canvas canvas = Display.getCanvas();
BufferStrategy strat = canvas.getBufferStrategy();
if (strat == null) {
canvas.createBufferStrategy(3);
return;
}
do {
do {
Graphics2D g = null;
int screenWidth = canvas.getWidth();
int screenHeight = canvas.getHeight();
try {
g = (Graphics2D) strat.getDrawGraphics();
g.clearRect(0, 0, screenWidth, screenHeight);
//BufferedImage map = loader.loadWorld();
g.setBackground(Color.black);
//g.drawImage(map, 0, 0, canvas);
} finally {
g.dispose();
}
} while (strat.contentsRestored());
strat.show();
} while (strat.contentsLost());
}
drawFrame() is called multiple times from my game loop and createDisplay() is called once before the game loop is started. When I run my game all I get is a frame with a blank screen:
Thank You for your help.