0

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.

IssiahB
  • 52
  • 8
  • *When I run my game all I get is a frame with a blank screen:* - what do you expect to get. You don't do any painting. Also, not sure why you are using a BufferedStratgy. Swing is double buffered by default. Just do [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). – camickr Nov 03 '21 at 14:13
  • @camickr Thank you for the respond, and I expect a black background, also I have tried drawing other things and nothing appears. I am also not using swing, I'm using a Canvas, and just switched the JFrame to a Frame to see it that would work, but it's the same results. – IssiahB Nov 03 '21 at 14:40
  • Keep in mind that drawing is happening in EDT (in some `pain(Graphics g)` method. And there cannot be any blocking loop inside. Your main game loop should be designed in such way - call `repaint()` after you change the state of the game, and then then `paint(...)` should draw the current state of the game. – krzydyn Nov 03 '21 at 14:53
  • *I expect a black background* well you don't have any code that paints a background. Setting the color of the Graphics object doesn't cause the background to be painted. *I am also not using swing, I'm using a Canvas,* - that was my point. You were using a JFrame, which is a Swing component. So you should be using a JPanel to do custom painting with Swing. It is easier than trying to use a Canvas with a BufferStrategy. – camickr Nov 03 '21 at 16:32

1 Answers1

0

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

IssiahB
  • 52
  • 8