•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,603 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,509 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1979 | Replies: 7
![]() |
•
•
Join Date: Sep 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hi members,I would like to share knowlege in Java with you.
I have a problem which i request a solution for.
I want to design a GUI in java that displays four roads each having traffic
lights and these traffic lights have four colours(green, orange, and Red).
So I want these traffic lights to change colours every after 10 seconds with green
moving clockwise to all the four roads followed by orange and
finally Red. This code should allow me to click on any these lights and when I do so
the clicked traffic light should light up green all the rest light up red immediately.
I have a problem which i request a solution for.
I want to design a GUI in java that displays four roads each having traffic
lights and these traffic lights have four colours(green, orange, and Red).
So I want these traffic lights to change colours every after 10 seconds with green
moving clockwise to all the four roads followed by orange and
finally Red. This code should allow me to click on any these lights and when I do so
the clicked traffic light should light up green all the rest light up red immediately.
•
•
•
•
Hi members,I would like to share knowlege in Java with you.
I have a problem which i request a solution for.
I want to design a GUI in java that displays four roads each having traffic
lights and these traffic lights have four colours(green, orange, and Red).
So I want these traffic lights to change colours every after 10 seconds with green
moving clockwise to all the four roads followed by orange and
finally Red. This code should allow me to click on any these lights and when I do so
the clicked traffic light should light up green all the rest light up red immediately.
Custom painting in Swing: Performing Custom Painting
Timers:How to Use Swing Timers
Event listeners: Writing Event Listeners
You may want to use icons (in JLabels) for the signs, as it will make some of the click handling easier and you won't have to paint them from scratch.
Work through those and start coding up your interface. If you run into specific troubles or something you don't understand, post back with the code and questions.
•
•
Join Date: Sep 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hi members
that's the code I tried with my friend but we were all confused
please try to show us how we can deal with it.
public class TrafficLight {
int currentState;
// Constructor that makes a red traffic light
public TrafficLight() {
currentState = 1;
}
// Advance the traffic light to the next state
public int advanceState() {
currentState = ++currentState % 3 + 1;
return currentState;
}
// Return the state of the traffic light (as a number from 1 to 3)
public int getState() {
return currentState;
}
// Set the state of the traffic light (as a number from 1 to 3)
// If the integer is out of range, do nothing
public void setState(int newState) {
if ((newState > 0) && (newState <4))
currentState = newState;
}
// Return a string representation of the traffic light
public String toString() {
String[] colours = {"Red", "Yellow", "Green"};
return colours[currentState] + " Traffic Light";
}
}
that's the code I tried with my friend but we were all confused
please try to show us how we can deal with it.
public class TrafficLight {
int currentState;
// Constructor that makes a red traffic light
public TrafficLight() {
currentState = 1;
}
// Advance the traffic light to the next state
public int advanceState() {
currentState = ++currentState % 3 + 1;
return currentState;
}
// Return the state of the traffic light (as a number from 1 to 3)
public int getState() {
return currentState;
}
// Set the state of the traffic light (as a number from 1 to 3)
// If the integer is out of range, do nothing
public void setState(int newState) {
if ((newState > 0) && (newState <4))
currentState = newState;
}
// Return a string representation of the traffic light
public String toString() {
String[] colours = {"Red", "Yellow", "Green"};
return colours[currentState] + " Traffic Light";
}
}
Please use code tags when you post code for review. As for the code, what question do you have about it? The fact that it doesn't return the correct string description? Hint: don't try to use a 1-based state variable backed by a zero-based array. (There's also no reason to re-create that array in every call to "toString()" ).
If you are representing state with integer state codes, you should define those as named constants as well. "RED" makes a lot more sense to read in the code than "1". Using an enum would probably be even better.
If you are representing state with integer state codes, you should define those as named constants as well. "RED" makes a lot more sense to read in the code than "1". Using an enum would probably be even better.
Last edited by Ezzaral : Nov 13th, 2007 at 1:11 pm.
So write it now, please.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code com combo dani daniweb data debugging development dreamweaver dropdownlist gdata google gpl html innovation java key linux microsoft microsoft sdk for java microsystems module net news open openbsd platform product programming reuse rss serial software source sun tags vista web wysiwyg xml
- java code that display cpu ver, cpu speed and ram size. (Java)
- My first code display (C++)
- I want to know about the java code. Help please!!!! (Java)
- whats wrong with my code cant display result (C)
- the idea of using Java for end-user GUI apps (Java)
- Move out java code from jsp (Java)
- Help with Java code (Java)
- Can N E 1 Help!!!! -- Need sample Java code (Java)
- java code newbie (Java)
Other Threads in the Java Forum
- Previous Thread: Creating a "Data" folder in my C: Drive using my Java program
- Next Thread: Read a specific line from a text file - how to ?



Linear Mode