2013-10-28 45 views
0

我有一個問題,我有一個計時器班,將改變我的GUI程序上的燈。不過,我已經使用的public void方法行,所以我現在努力增加繪製的圖形的G。如何將油漆(圖形g)添加到另一個類中?

/*public void paint(Graphics back) 
{ 
    back.setColor(Color.black); 
    back.fillRect(30,30,330,900);   //make traffic light for car 
    back.fillRect(440,140,330,720);   //make traffic light for peds 
    back.setColor(Color.red);    //creates red circle 
    back.fillOval(45,45, 300, 280); 
    back.fillOval(455,155, 300, 280);  //creates red ped circle 
    back.setColor(Color.yellow);   //creates yellow circle 
    back.fillOval(45, 335, 300,280); 
    back.setColor(Color.green); 
    back.fillOval(45,625, 300, 280); 
    back.fillOval(455,555, 300, 280);  //creates green ped circle 
}*/ 

class LightChange implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     paint (Graphics back); 
     seconds++; 
     if (seconds == 0 & seconds <= 5) 
     { 
      back.setColor(Color.red);    //creates red circle 
      back.fillOval(45,45, 300, 280); 
     } 
     else 
     { 
     back.setColor(Color.yellow);    //creates yellow circle 
     back.fillOval(45, 335, 300,280); 
     } 
     repaint(); 
    } 
} 

我有一個錯誤說,它不能找到後退符號。任何幫助將是偉大的!

編輯:

我做了一些改變,並試圖加入油漆(圖形回);進入課堂。一些錯誤消息出現(表達式的非法開始),但我在正確的線?

+2

這不是繪畫的作品。只要組件想要重繪,就需要繪製所有內容。 – SLaks

+0

也許這篇文章可能會解釋更清楚的事情 - http://java-articles.info/articles/?p=38 –

回答

1

首先,你不應該有一個分離的類implements ActionListner,因爲這將使一切方式更加複雜。其次,像SLaks說,你需要看看繪畫是如何工作的。檢查鏈接here。現在

,我會做的方式,它是這樣的:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JPanel; 
import javax.swing.Timer; 

public class Draw extends JPanel implements ActionListener { 
private Color topColorTraffic = Color.red; 
private Color middleColorTraffic = Color.gray; 
private Color bottomColorTraffic = Color.gray; 

private Color topColorPeds = Color.gray; 
private Color bottomColorPeds = Color.green; 

// WE INITIALIZED THE UPPER VARIABLE LIKE THIS BECAUSE WE PRESUMED THAT 
// IS RED FOR THE CARS AND GREEN FOR THE PEDS 

private long startTime; // This will get the time when we've started the "animation" 

private long targetTimeTraffic; // This will be the time we want the have 
           // between red changes to 
           // yellow in the traffic 
private long targetTimeYellowLight; //This will be the time we want the yellow 
            //light to be displayed 
private Timer t; 

public Draw() { 
    startTime = System.currentTimeMillis(); 
    targetTimeTraffic = 3000; // 3 sec 
    targetTimeYellowLight = 1200; //1.2 sec 
    t = new Timer(5, this); // this will call actionPerformed every 5 miliseconds 
    t.start(); 
} 


public void paintComponent(Graphics back) { 
    super.paintComponent(back); 

    back.setColor(Color.black); 
    back.fillRect(30, 30, 330, 900); // make traffic light for car 
    back.fillRect(440, 140, 330, 720); // make traffic light for peds 

    back.setColor(topColorTraffic); // creates the top traffic circle 
    back.fillOval(45, 45, 300, 280); 
    back.setColor(topColorPeds); // creates the top peds circle 
    back.fillOval(455, 155, 300, 280); 

    back.setColor(middleColorTraffic); // creates the middle traffic circle 
    back.fillOval(45, 335, 300, 280); 

    back.setColor(bottomColorTraffic); // creates the bottom traffic circle 
    back.fillOval(45, 625, 300, 280); 
    back.setColor(bottomColorPeds); // creates the bottom peds circle 
    back.fillOval(455, 555, 300, 280); 
} 

public void actionPerformed(ActionEvent arg0) { 
    // changing from red to yellow in traffic 
    long currentTime = System.currentTimeMillis(); 
    if (targetTimeTraffic <= currentTime - startTime) { //1st delay 3 sec 
     topColorTraffic = Color.gray; 
     middleColorTraffic = Color.yellow; 
     topColorPeds = Color.red; 
     bottomColorPeds = Color.gray; 
    } 

    //changing from yellow to green 
    if (targetTimeTraffic <= currentTime - startTime - targetTimeYellowLight) { //2nd delay 3 sec + another 1.2 sec 
                       //but 3 sec were already for the 1st delay 
                       // so we get only 1.2 sec delay 
     topColorTraffic = Color.gray; 
     middleColorTraffic = Color.yellow; 
     bottomColorTraffic = Color.green; 
     middleColorTraffic = Color.gray; 
     //startTime = System.currentTimeMillis(); 
    } 

    //changing from green to yellow 
    if (targetTimeTraffic <= currentTime - startTime - targetTimeYellowLight - targetTimeTraffic) { 
     topColorTraffic = Color.gray; 
     bottomColorTraffic = Color.gray; 
     middleColorTraffic = Color.yellow; 
    } 

    //changing from yellow to red 
    if (targetTimeTraffic <= currentTime - startTime - 2* targetTimeYellowLight - targetTimeTraffic) { 
     topColorTraffic = Color.red; 
     middleColorTraffic = Color.gray; 
     topColorPeds = Color.gray; 
     bottomColorPeds = Color.green; 
     startTime = System.currentTimeMillis(); //reinitialize the variable 
    } 

    repaint(); 
    } 
} 

如果你理解代碼,請隨時提出問題。

+0

感謝您的回覆,幫助了很多!我想問你如何讓定時器的參數「this」連接到public void actionPerformed(ActionEvent arg0) – lecardo

+0

'Timer'有2個參數:1st是延遲(意思是在什麼時間間隔以毫秒爲單位表示你想' actionPerformed'來觸發),第二個是ActionListener接口。由於我們的類實現了該接口,因此它自動也希望執行的操作方法也得到實現。所以當我說'this' Java知道我指的是我已經實現的ActionListener接口(頂行)並且因此連接。查看更多關於接口和多繼承的信息[here](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)。 – Andy

相關問題