程序未更新背景色

时间:2020-10-18 23:05:46

标签: java swing colors jframe buffer

我对Java很陌生,如果这是一个愚蠢的问题,我深表歉意。下面是我的代码。

private void draw(){
    //this is where the background is going to be made
    //paddles gonna be made here as well plus the score and the ball
    //will dispose here

    BufferStrategy Buffer = this.getBufferStrategy();

    if(Buffer==null){
        this.createBufferStrategy(3);
        return;
    }
    Graphics graphics = Buffer.getDrawGraphics();

    //drawing the background
    drawbg(graphics);

    //disposing everything. disposing means to actually show on the screen
    graphics.dispose();
    Buffer.show();
    }


private void drawbg(Graphics graphics){
    //black bg
    graphics.setColor(Color.red);
    graphics.fillRect(0,0,W,H); //fills the whole screen

当我运行此代码时,我的Jframe应用程序仍然仅具有白色背景,但不应该具有红色背景,因为我先设置了颜色然后进行处理?我是否在该问题中缺少一些代码?

1 个答案:

答案 0 :(得分:-1)

我认为应该为Color.RED。大写。我不能评论,因为它不会让我,但请尝试一下。确保您的坐标确实填满了整个屏幕,我知道JFrame和JPanel有时可能对这种东西很奇怪。

相关问题