Java code to display a GUI

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 15
Reputation: TransKim is an unknown quantity at this point 
Solved Threads: 0
TransKim TransKim is offline Offline
Newbie Poster

Java code to display a GUI

 
0
  #1
Nov 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 55
Reputation: mickinator is an unknown quantity at this point 
Solved Threads: 5
mickinator mickinator is offline Offline
Junior Poster in Training

Re: Java code to display a GUI

 
0
  #2
Nov 7th, 2007
Well show us what code you've got! Noone's gonna do it for ya.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 56
Reputation: staneja is an unknown quantity at this point 
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

Re: Java code to display a GUI

 
0
  #3
Nov 7th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java code to display a GUI

 
0
  #4
Nov 7th, 2007
Originally Posted by TransKim View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 15
Reputation: TransKim is an unknown quantity at this point 
Solved Threads: 0
TransKim TransKim is offline Offline
Newbie Poster

Re: Java code to display a GUI

 
0
  #5
Nov 12th, 2007
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";
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java code to display a GUI

 
0
  #6
Nov 13th, 2007
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.
Last edited by Ezzaral; Nov 13th, 2007 at 1:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: naslicer is an unknown quantity at this point 
Solved Threads: 0
naslicer naslicer is offline Offline
Newbie Poster

Re: Java code to display a GUI

 
0
  #7
Nov 19th, 2007
i wanted the real code now please!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java code to display a GUI

 
0
  #8
Nov 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: mesfinanegagrie is an unknown quantity at this point 
Solved Threads: 0
mesfinanegagrie mesfinanegagrie is offline Offline
Newbie Poster

just for help

 
-2
  #9
Oct 19th, 2009
i went to develop the web page throuth java script
but how?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
1
  #10
Oct 19th, 2009
By starting your own thread in the JavaScript forum, maybe?
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC