Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java绘制椭圆重复圆_Java - Fatal编程技术网

java绘制椭圆重复圆

java绘制椭圆重复圆,java,Java,重新绘制圆时,窗口不会被清除;新的圆圈将添加到现有内容中 目标是创建三个圆,每种颜色一个 线程调用move函数,该函数绘制具有不同半径的圆 public void run() { try { while(true){ box.removeAll(); move(); box.removeAll(); sleep(500); } }

重新绘制圆时,窗口不会被清除;新的圆圈将添加到现有内容中

目标是创建三个圆,每种颜色一个

线程调用move函数,该函数绘制具有不同半径的圆

public void run() {
    try {
          while(true){
              box.removeAll();
              move();
              box.removeAll();
              sleep(500);
          }
    } catch (InterruptedException e) {
    }
}

public synchronized void move() {
    Graphics g = box.getGraphics();
    g.setXORMode(box.getBackground());

    x1= one.x + ofset;
    y1= one.y + ofset;

    System.out.println("My Point ("+ x1 + " , " + y1 +")" );

    g.setColor(Color.green);
    g.drawOval(pointA.x-(int)distance1, pointA.y-(int)distance1, (int)distance1*2, (int)distance1*2);

    g.setColor(Color.blue);
    g.drawOval(pointB.x-(int)distance2, pointB.y-(int)distance2, (int)distance2*2, (int)distance2*2);

    g.setColor(Color.red);
    g.drawOval(pointC.x-(int)distance3, pointC.y-(int)distance3, (int)distance3*2, (int)distance3*2);

    g.dispose();
}

首先,不推荐您的方法。但是,如果您只想快速而肮脏地修复,则必须在绘制圆之前清除面板

Graphics g = box.getGraphics();
g.clearRect(0, 0, box.getWidth(), box.getHeight()); // this should do it
g.setXORMode(box.getBackground());
否。不要使用getGraphics()。使用该图形对象完成的任何绘制都只是临时的,并且在Swing确定需要重新绘制组件时将被删除

对于自定义绘制,请覆盖JPanel的getPreferredSize()方法:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g); // clears the background

    // add your custom painting here
}

另外,不要忘记覆盖面板的
getPreferredSize()
方法。有关更多信息,请阅读Swing教程中的章节。

什么是
?您正在使用哪个GUI?你的问题缺乏背景。很高兴能为你提供帮助。但我确实建议遵循camickr的建议:只需更新方法中的值,然后在
paintComponent
中绘制所有图形。
@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g); // clears the background

    // add your custom painting here
}