2013-04-13 23 views
0

我創建了一個類,即兩個橢圓。在這裏,我畫他們。在Java動畫中使用線程

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) 

    } 
} 

現在我在主要實現它。

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 

      } 
     } 

當程序運行時,兩個橢圓在屏幕上旋轉。但是這兩個橢圓的旋轉速度與我所期望的相同,但我希望兩個橢圓以不同的速度移動。

我試過用一種方法將它們以不同的速度移動但沒有成功。 我將如何獲得以不同速度移動的兩個橢圓形?

+0

究竟你有試過嗎? –

+1

創建一個方法來設置每個橢圓正在移動的速度,但不起作用,它不合邏輯有效。 – Jonathan

+0

你有沒有嘗試在自己的線程中繪製每個橢圓形?你可以睡不同的時間。 –

回答

1

做一個類來表示一個橢圓。製作兩個實例。給兩個實例不同的角速度。目前,因爲您每25毫秒將度數增加1.0,所以角速度固定爲每秒40度。如果每個橢圓都有其自己的度數場,並且你用不同的數量遞增它們,橢圓會以不同的速率旋轉。

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 

} 

}

+0

我想你想乘以角度的速度,而不是位置。即'double radians0 = degrees * 180.0/Math.PI * firstOvalSpeed; int posx0 =(int)(100.getDistance()* Math.cos(radians0));'等等。然後使用'(posx0,posy0)'作爲第一個橢圓,''(posx1,posy1)'第二個。另外我建議'firstOvalSpeed'和'secondOvalSpeed'是'double's。 –