954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java code to display a GUI

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.

TransKim
Newbie Poster
15 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Well show us what code you've got! Noone's gonna do it for ya.

mickinator
Junior Poster in Training
55 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 

Hi TransKim

Write some code ...get an error and ask us ...we are here for you in that case

But if your teacher ask you to do something and you are simple coming to us to do that.....then we are not here to help you

so come up with some code

staneja
Junior Poster in Training
64 posts since Dec 2006
Reputation Points: 10
Solved Threads: 2
 
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.


Ok, so to accomplish this you will need to understand the following:
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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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";
}
}

TransKim
Newbie Poster
15 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

i wanted the real code now please!

naslicer
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

So write it now, please.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

i went to develop the web page throuth java script
but how?

mesfinanegagrie
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

By starting your own thread in the JavaScript forum, maybe?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Guys i need to do the same t

ella_xhin
Newbie Poster
2 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Closing this zombie now.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You