简体   繁体   English

如何擦除Graphics对象?

[英]How do I make a erase a Graphics object?

In the code below I am trying to make a ball bounce up and down. 在下面的代码中,我试图让球上下跳动。 The problem is that the ball is painting itself over and over and forming a line rather than a ball moving in a line. 问题在于球一遍又一遍地绘制自己并且形成一条线而不是一条线移动的球。

I'm thinking that I need to erase the ball after it draws itself. 我想我需要在吸球后擦掉球。

 public void paint(Graphics g) {
            if (bouncing) {
                g.setColor(Color.blue);
                g.drawOval(x, y, 10, 10);
                //erase oval here
            }
        }

NOTE: Method paint is being called over and over 注意:反复调用方法绘制

Paint the entire background first ... 先画出整个背景......

public void paint(Graphics g)
{
  g.setColor(Color.BLACK);                    // clear the frame ...
  g.fillRect(0, 0, getWidth(), getHeight());

  if (bouncing) 
  {
    g.setColor(Color.blue);
    g.drawOval(x, y, 10, 10);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM