I'm having a issue where I have a timer class that will change the light on my GUI program. But I already used the public void method line so I'm struggling now to add the paint graphics G in.
/*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();
}
}
I have a error saying it can't find the back symbol. Any help would be great!
EDIT:
I made some changes and tried adding paint (Graphics back); into the class. Some error messages come up (illegal start of expression)but am I on the right lines?
First, you shouldn't have a separated class that implements ActionListner
because that will make everything way much more complicated. Secondly, like SLaks said you need to take a look at how painting works. Check this link here .
Now, the way I would do it, it's something like this:
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();
}
}
If you have problems understanding the code please feel free to ask.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.