0

我创建了一个具有两种形状的类,即两个椭圆形。我在这里画它们。

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.js 中实现它。

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

            }  
         }

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

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

4

2 回答 2

1

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

于 2013-04-13T22:12:20.553 回答
0

最简单的方法是:

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

}

}

于 2013-04-13T22:41:40.463 回答