Having Trouble With Drawing To BufferStrategy Java

64 Views Asked by At

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: Game Running

Thank You for your help.

1

There are 1 best solutions below

0
IssiahB On

Okay so I changed my JFrame to a Frame so that I'm not mixing swing components with awt components. Also I was calling g.setBackground(color) which wasn't painting anything to the screen, so instead I simply set the graphics color with g.setColor(color). Then I filled the background with g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()).

Here is my new drawFrame() method:

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.setColor(Color.black); // Set Graphics Color
                g.fillRect(0, 0, screenWidth, screenHeight); // Fill Background


                // Do All Drawing here


            } finally {
                g.dispose();
            }
        } while (strat.contentsRestored());
        
        strat.show();
        Toolkit.getDefaultToolkit().sync(); // Also added this to synchronize graphics state
    } while (strat.contentsLost());
}

This seems to be working for me and now I get this when I run my game: Black Background Game