【问题标题】:erasing a shape in java在java中擦除形状
【发布时间】:2011-06-02 23:59:02
【问题描述】:

我正在尝试绘制每秒出现的圆圈,我能够做到,但如何让旧形状消失?

 public void paint(Graphics g) {
  try {
    while (true) {
        Shape circle = new Ellipse2D.Double(500*Math.random(),500*Math.random(), 50.0f, 50.0f);
        Graphics2D ga = (Graphics2D)g;
        ga.draw(circle);
        ga.setPaint(Color.green);
        ga.fill(circle);
        ga.setPaint(Color.red);
        Thread.sleep(1000);

    }
} catch (InterruptedException e) {
    e.printStackTrace();
} 

}

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发布SSCCE 2) paint(Graphics) 在这个千年不要编码 AWT。如果在 Swing 组件中自定义绘画,请使用paintComponent(Graphics) 3) while (true) 不要在绘画方法中启动无限循环。 4) Thread.sleep(1000); 不要在paint方法中等待

标签: java drawing jpanel erase


【解决方案1】:

只需获取背景色并用背景色圆圈覆盖旧圆圈即可。

【讨论】:

  • 我只是用白色填充重新绘制了圆圈谢谢
  • 如果您的背景颜色不是白色,您可以使用ga.getBackground() 获取它。
【解决方案2】:

尝试在循环中首先调用super.paint(g);

【讨论】:

    【解决方案3】:

    这里最简单的方法是在 paint() 方法的顶部调用 g.clearRect(WIDTH, HEIGHT);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2011-09-12
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多