【问题标题】:program not updating background color程序不更新背景颜色
【发布时间】:2021-02-01 17:57:57
【问题描述】:

我对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 应用程序仍然只有白色背景,但它不应该有红色背景,因为我设置了颜色然后 dispose ?我是否遗漏了问题中的一些代码?

【问题讨论】:

标签: java swing colors jframe buffer


【解决方案1】:

我认为应该是Color.RED。全部大写。我不能发表评论,因为它不会让我,但试试看。确保你的坐标确实填满了整个屏幕,我知道 JFrame 和 JPanel 有时会因为这类东西很奇怪。

【讨论】:

  • “我知道 JFrame 和 JPanel 有时会对这类东西感到奇怪。” 这句话暗示了你为什么被限制给出答案。不要试图玩弄系统。
  • “至少尝试帮助这个人”posted a comment 为 OP 添加了一个minimal reproducible example 并添加了Swing tag 以引起人们的注意能够提供帮助。仍在等待他们对建议采取行动。如果提出问题的人忽略建议,他们不太可能获得帮助。
猜你喜欢
  • 2014-09-10
  • 2020-01-17
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2016-03-13
  • 1970-01-01
  • 2014-04-09
相关资源
最近更新 更多