-1

For no apparent reason, the following code "stops" after printing out Will be drawing.

private void drawMaps() {
    System.out.println("Will be drawing...");
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    //Clear Screens
    g.clearRect(0, 0, 1000, 725);
    //Draw Here!
    System.out.println("Should be drawing");

    for(int i=0; i<toPlay.islands().size(); i++) {
        System.out.println("We're at island " + i);
        int x = 0, y = 0;
        if(i==0) {
            x=50;
            y=10;
        }
        Color toDraw = toPlay.islands().get(0).getTeam().getColor().getColor();
        g.setColor(toDraw);
        g.fillRect(x, y, 50, 50);
    }

    //End Drawing!
    bs.show();
    g.dispose();
}

What I get in the console from this is Will be drawing, then nothing and nothing on my JFrame. I don't know why this doesn't work because this has worked in a different class before.

Mads K
  • 847
  • 2
  • 14
  • 32
  • 1
    It's pretty difficult to debug code without having it in a context. Also, I don't know what JFrame is. You might want to elaborate a bit more on your question as the issue might stem from the framework/tool you are using. – Mads K Feb 17 '18 at 15:52
  • 1
    Why are you drawing this way and not in the paintComponent method of a JPanel? – DontKnowMuchBut Getting Better Feb 17 '18 at 15:56
  • @DontKnowMuchButGettingBetter paintComponent is less flexible and portable. –  Feb 18 '18 at 16:53
  • @TheMagician: sorry but that doesn't make sense. It is just as flexible and just as if not more portable than as you're doing things. I've been using it successfully for years. If you posted a valid [mcve], I could show you as applies to your situation. – DontKnowMuchBut Getting Better Feb 18 '18 at 17:10

1 Answers1

0

Obviously your code do return here

if(bs == null){
    display.getCanvas().createBufferStrategy(3);
    return; // return!
}

A little more explanation:

It looks like you are trying to use a "common technique" to render the canvas in java awt, so you need to run the drawMaps in a cycle, and first time when you run it - the buffer strategy is not yet initilized, so you run the initialization of that strategy and on the next call it will be ready so you code will go further. Somewhere you need to have the code like

while (running)
{
    ...
    drawMaps();
    ..
}
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46