2017-08-07 86 views
-1

我有8個橢圓都設置爲相同的顏色。一秒鐘後,我想第一個橢圓形改變顏色,然後又一秒鐘後,我想第一個橢圓形回到原來的顏色,然後改變第二個橢圓形的顏色。我畫了圈,我已經嘗試實現線程,但我認爲這是不執行......每秒填充一個橢圓

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Circle extends JPanel implements Runnable 
{ 

    Graphics g; 

    Thread t = new Thread(); 

    int[][] fillCircles = new int[8][4]; 


@Override 
public void paintComponent(Graphics g) 
{ 
    this.g = g; 
    super.paintComponent(this.g); 

    this.g.setColor(new java.awt.Color(237, 54, 26)); 

    this.g.drawOval(300, 50, 100, 100); 
    this.g.drawOval(450, 125, 100, 100); 
    this.g.drawOval(500, 250, 100, 100); 
    this.g.drawOval(425, 375, 100, 100); 
    this.g.drawOval(300, 400, 100, 100); 
    this.g.drawOval(175, 350, 100, 100); 
    this.g.drawOval(125, 225, 100, 100); 
    this.g.drawOval(175, 100, 100, 100); 

    this.g.fillOval(300, 50, 100, 100); 
    this.g.fillOval(450, 125, 100, 100); 
    this.g.fillOval(500, 250, 100, 100); 
    this.g.fillOval(425, 375, 100, 100); 
    this.g.fillOval(300, 400, 100, 100); 
    this.g.fillOval(175, 350, 100, 100); 
    this.g.fillOval(125, 225, 100, 100); 
    this.g.fillOval(175, 100, 100, 100); 

    fillCircles[0][0] = 300; 
    fillCircles[0][1] = 50; 
    fillCircles[0][2] = 100; 
    fillCircles[0][3] = 100; 

    fillCircles[1][0] = 450; 
    fillCircles[1][1] = 125; 
    fillCircles[1][2] = 100; 
    fillCircles[1][3] = 100; 

    fillCircles[2][0] = 500; 
    fillCircles[2][1] = 250; 
    fillCircles[2][2] = 100; 
    fillCircles[2][3] = 100; 

    fillCircles[3][0] = 425; 
    fillCircles[3][1] = 375; 
    fillCircles[3][2] = 100; 
    fillCircles[3][3] = 100; 

    fillCircles[4][0] = 300; 
    fillCircles[4][1] = 400; 
    fillCircles[4][2] = 100; 
    fillCircles[4][3] = 100; 

    fillCircles[5][0] = 175; 
    fillCircles[5][1] = 350; 
    fillCircles[5][2] = 100; 
    fillCircles[5][3] = 100; 

    fillCircles[6][0] = 125; 
    fillCircles[6][1] = 225; 
    fillCircles[6][2] = 100; 
    fillCircles[6][3] = 100; 

    fillCircles[7][0] = 175; 
    fillCircles[7][1] = 100; 
    fillCircles[7][2] = 100; 
    fillCircles[7][3] = 100; 


} 

Circle() { 
    t.start(); 
} 


public void run() { 

    int circle = 0; 

    try { 
     for (;;) { 

      Thread.sleep(1000); 

      if (circle > 0) { 
       this.g.setColor(new java.awt.Color(237, 54, 26)); 
       circle--; 
       this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]); 
       circle++; 
      } 
      this.g.setColor(Color.red); 
      this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]); 


      circle++; 

      if (circle == 8) { 
       circle = 0; 
      } 

     } 
    } catch (InterruptedException e) { 
     System.out.println ("Thread Interrupted"); 
    } 

} 


public static void main(String[] args) { 
    Circle c; 

    JFrame application = new JFrame(); 

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    application.add(c=new Circle()); 
    application.setSize(1200, 900); 
    application.setVisible(true); 


} 

}

回答

2

你只是實例化一個Thread沒有給它一個Runnable,所以它的沒有運行你想要的東西。

Thread t = new Thread(); 

應該

Thread t = new Thread(this); 

這是因爲CircleRunnablerun()方法是在其中。 關於顏色變化,我會給你一個想法。 當繪製圓圈,檢查對另一這改變您的代碼的每一個第二colorChangeIndex

if(circleIndex == colorChangeIndex){ 
    // another color 
else 
//the default color 
colorChangeIndex++; (modulo number of cricles to reset it) 
+0

檢查當前圈...... --->我不關注。 – Yeezus

1

的基本結構是錯誤的變量的當前匝數/索引。繪畫只能在paintComponent()方法中完成。你不應該在你的線程中引用Graphics對象。

所以,你可能想要做的是保持一個ArrayList的「circle」對象。此自定義對象將包含要繪製的圓圈及其顏色。然後在paintComponent()方法中,您只需遍歷ArrayList並繪製ArrayList中的所有圓。查看Custom Painting Approches中的DrawOnComponent示例。

過了一秒鐘後,我希望第一個橢圓改變顏色,然後又一秒鐘後,我想第一個橢圓回到原來的顏色,然後改變第二個橢圓的顏色。

所以,現在您需要使用Swing Timer來繪製動畫。定時器啓動時,重置相應圓的顏色,然後重新繪製面板。

+0

謝謝你:) !!!! – Yeezus