在Java中使用动画线程

在Java中使用动画线程,java,multithreading,paint,Java,Multithreading,Paint,我创建了一个包含两个形状的类,即两个椭圆形。我在这里画 import ...; public class Drawings extends JPanel{ public double degrees = 0; public void paintComponent(Graphics g){ super.paintComponent(g); int xcen = getWidth() / 2; int ycen = getHei

我创建了一个包含两个形状的类,即两个椭圆形。我在这里画

import ...;

public class Drawings extends JPanel{
    public double degrees = 0;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int xcen =  getWidth() / 2;
        int ycen =  getHeight()/ 2;

        int radius = 10;
        degrees++;

        double radians = Math.toRadians(degrees);
        int posx = (int)(100.getDistance() * Math.cos(radians));
        int posy = (int)(100.getDistance() * Math.sin(radians));

        g.setColor(Color.BLUE);
        g.FillOval(xcen + posx, ycen + posy, 20, 20);

        g.setColor(Color.GREEN);
        g.drawOval(xcen + posx, ycen + posy, 100,100)

   }
} 
现在我在一个main中实现它

import ....;

public class Animate extends JFrame{
  public static void main(String [] args)
  {

   JFrame window = new JFrame();
   window.add(new Drawings());
   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   window.setSize(500,500);
   window.setLocationRelativeTo(null); 
   window.setVisible(true);

   //now I implement the thread to animate the two shapes
   Thread paintThread = new Thread(new Runnable(){
   @Override
        public void run(){
              while(true)
              {
                   window.repaint();
                   try{
                        Thread.sleep(25);//determines how slow the ovals will move
                   }catch(InterruptedException e){
                       e.printStackTrace();
                   }
                 }
               }
             });

              paintThread.start();//start the animation

            }  
         }
当程序运行时,两个椭圆在屏幕上旋转。但这两个椭圆的旋转速度和我预期的一样,但我希望这两个椭圆以不同的速度移动

我尝试过用一种方法以不同的速度移动它们,但没有成功。
如何使两个椭圆以不同的速度移动?

制作一个类来表示一个椭圆。举两个例子。给这两个例子不同的角速度。目前,因为每25毫秒增加1.0度,所以角速度固定在每秒40度。如果每个椭圆都有自己的度数字段,并且您以不同的量增加这两个度数,则椭圆将以不同的速率旋转。

最简单的方法是:

import ...;

public class Drawings extends JPanel{
public double degrees = 0;

private int firstOvalSpeed;
private int secondOvalSpeed;

public void paintComponent(Graphics g){
    super.paintComponent(g);
    int xcen =  getWidth() / 2;
    int ycen =  getHeight()/ 2;

    int radius = 10;
    degrees++;

    double radians = Math.toRadians(degrees);
    int posx = (int)(100.getDistance() * Math.cos(radians));
    int posy = (int)(100.getDistance() * Math.sin(radians));

    g.setColor(Color.BLUE);
    g.FillOval(xcen + posx*firstOvalSpeed, ycen + posy*firstOvalSpeed, 20, 20);

    g.setColor(Color.GREEN);
    g.drawOval(xcen + posx*secondOvalSpeed, ycen + posy*secondOvalSpeed, 100,100)

 }
public void setFirstOvalSpeed(int firstOvalSpeed) {
    this.firstOvalSpeed = firstOvalSpeed;
}

public void setSecondOvalSpeed(int secondOvalSpeed) {
    this.secondOvalSpeed = secondOvalSpeed;
}

} 




public class Animate extends JFrame {
public static void main(String[] args) {

    final JFrame window = new JFrame();
    Drawings drawings = new Drawings();
    drawings.setFirstOvalSpeed(1);
    drawings.setSecondOvalSpeed(2);

    window.add(drawings);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(500, 500);
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    // now I implement the thread to animate the two shapes
    Thread paintThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                window.repaint();
                try {
                    Thread.sleep(25);// determines how slow the ovals will
                                        // move
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    paintThread.start();// start the animation

}

}

您到底尝试了什么?创建一种方法来设置每个椭圆移动的速度,但这种方法不起作用,而且不符合逻辑。您是否尝试过在每个椭圆上画自己的线?你可以让它们睡不同的时间。是的,我睡过了,但它给我一个奇怪的显示,椭圆并没有显示出来,只是作为对使用线程进行动画的一般评论:使用计时器通常不那么危险。在应用程序中,与javax.swing.Timer相比,使用线程没有什么优势作为对动画制作的一般性评论。我建议将模型与视图分开。特别是,我会保持画画没有副作用。我想你想要的是速度乘以角度,而不是位置。即,
double radians0=度*180.0/数学π*firstOvalSpeed;int posx0=(int)(100.getDistance()*Math.cos(radians0))等。然后对第一个椭圆使用
(posx0,posy0)
,对第二个椭圆使用
(posx1,posy1)
。我还建议
firstOvalSpeed
secondOvalSpeed
be
double
s。