Welcome to Dream.In.Code
Become a Java Expert!

Join 150,051 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,701 people online right now. Registration is fast and FREE... Join Now!




Help modifying this GUI

 
Reply to this topicStart new topic

Help modifying this GUI

mogstronaut_
4 Jun, 2008 - 01:40 AM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 47

Hey guys, just working on a fairly simple piece of GUI code.
This is what I have:
CODE
import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JFrame implements ActionListener {
    private static JButton btn1 = null;
    private static JButton btn2 = null;
    private static JButton btn3 = null;
    
    private static TrafficSignal green = new TrafficSignal(Color.green);
    private static TrafficSignal yellow = new TrafficSignal(Color.yellow);
    private static TrafficSignal red = new TrafficSignal(Color.red);
    
    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);        
        
        green.turnOn(true);
        yellow.turnOn(false);
        red.turnOn(false);
        
        JPanel lights = new JPanel( new FlowLayout() );
        lights.add( green );
        lights.add( yellow );
        lights.add( red );
        JPanel btnPane = new JPanel(new FlowLayout());
        btnPane.add(btn1);
        btnPane.add(btn2);
        btnPane.add(btn3);
        
        getContentPane().add(lights);
        getContentPane().add(btnPane);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            
    }
    
    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();        
        tl.setVisible(true);
    }

    
    public void actionPerformed(ActionEvent e){        
        if (e.getSource() == btn1){
            green.turnOn(true);            
            yellow.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn2){
            yellow.turnOn(true);            
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn3){
            red.turnOn(true);            
            yellow.turnOn(false);
            green.turnOn(false);
        }
    }
}    
    
class TrafficSignal extends JPanel {
    
    Color on;
    int radius = 75;
    int border = 10;
    boolean active;
    
    TrafficSignal(Color color){
        on = color;
        active = true;
    }
    
    public void turnOn(boolean a) {
        active = a;
        repaint();        
    }
    
    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }
    
    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());
    
        if (active){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}


And I just want to change it so that when the button is clicked it cycles through each of the lights.

Thanks.
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Help Modifying This GUI
4 Jun, 2008 - 01:49 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 322



Thanked: 17 times
Dream Kudos: 225
My Contributions
You can use threads or timers for that
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Modifying This GUI
4 Jun, 2008 - 09:15 AM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions

CODE

    Thread t = new Switch();
    t.start();

....
}

class Switch extends Thread {
    public void run() {
        for(;;) {
            green.turnOn(true);            
            yellow.turnOn(false);
            red.turnOn(false);
            repaint();
            try {
                Thread.sleep(1000L);
            }
            catch (Exception e) {}
            yellow.turnOn(true);            
            green.turnOn(false);        
                     red.turnOn(false);
            repaint();
            try {
                Thread.sleep(1000L);
            }
            catch (Exception e) {}

            red.turnOn(true);            
            yellow.turnOn(false);
            green.turnOn(false);
            repaint();
            try {
                Thread.sleep(1000L);
            }
            catch (Exception e) {}
        }
    }

User is online!Profile CardPM
+Quote Post

gl3thr0
RE: Help Modifying This GUI
4 Jun, 2008 - 10:09 AM
Post #4

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
if pbl's example is a little beyond wt u need you can simply use the Thread.sleep(long); method 2 add a pause in the cycle.. though its not as pretty as pbl's tongue.gif it is a lot easier



java

// in this example TrafficSignal ts = {green,yellow,red};
// also btn4 = the btn for cycling.
if (e.getSource() == btn4){
for(int i=0, x=2,y=1;i<3;i++,x++,y++){
if(x>2){
x=0;
}
if(y>2){
y=0;
}
ts[i].turnOn(true);
ts[x].turnOn(false);
ts[y].turnOn(false);

try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

}
}
}


This post has been edited by gl3thr0: 4 Jun, 2008 - 10:11 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Modifying This GUI
4 Jun, 2008 - 01:39 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(gl3thr0 @ 4 Jun, 2008 - 11:09 AM) *

java

// in this example TrafficSignal ts = {green,yellow,red};
// also btn4 = the btn for cycling.
if (e.getSource() == btn4){
for(int i=0, x=2,y=1;i<3;i++,x++,y++){
if(x>2){
x=0;
}
if(y>2){
y=0;
}
ts[i].turnOn(true);
ts[x].turnOn(false);
ts[y].turnOn(false);

try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

}
}
}



Sorry gl3thr0 NOT a good idea at ALL to Thread.sleep() in an actionPerformed() method or any other AWT event
method that can really screw up any GUI
Your idea is not good at all....
Don't really know the details behind but imagine paint() is called in the same Thread. Your repaint() won't be executed until the Thread ends of its sleep

Brief:

Thread.sleep() in any AWT event method is a NO NO NO !!!!
These must be as short as possible

This post has been edited by pbl: 4 Jun, 2008 - 02:30 PM
User is online!Profile CardPM
+Quote Post

mogstronaut_
RE: Help Modifying This GUI
4 Jun, 2008 - 04:49 PM
Post #6

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 47

Thanks for the replies guys. Haha, pbl, I was just playing around with sleep() but I guess I'll stay away from it now.

Thanks!
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Modifying This GUI
4 Jun, 2008 - 05:01 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mogstronaut_ @ 4 Jun, 2008 - 05:49 PM) *

Thanks for the replies guys. Haha, pbl, I was just playing around with sleep() but I guess I'll stay away from it now.

Thanks!

Stay away from it in an AWT event called method like actionPerformed()
In your own Thread (like the one I wrote for you) you can use it as you want
User is online!Profile CardPM
+Quote Post

fsloke
RE: Help Modifying This GUI
5 Jun, 2008 - 02:46 AM
Post #8

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
Great JOB .... This post really interesting....

I learn new stuff from you mogstronaut , pbl and gl3thr0...

But Can I ask about the Thread problem?

Can we stop or destroy a Thread? If can how Can I do that?

In JDK1.6.turn.gif4, discard the function t.destroy() , t.stop() and t.resume().... sad.gif

Thank

This post has been edited by fsloke: 5 Jun, 2008 - 03:55 AM
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Help Modifying This GUI
5 Jun, 2008 - 03:57 AM
Post #9

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(fsloke @ 5 Jun, 2008 - 03:46 AM) *

Great JOB .... This post really interesting....

I learn new stuff from you mogstronaut , pbl and gl3thr0...

But Can I ask about the Thread problem?

Can we stop or destroy a Thread? If can how Can I do that?

In JDK1.6.turn.gif4, discard the function t.destroy() , t.stop() and t.resume().... sad.gif

Thank



hello.. I'm using t.interrupt(); to stop a thread.. blink.gif

User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Modifying This GUI
5 Jun, 2008 - 06:42 AM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(fsloke @ 5 Jun, 2008 - 03:46 AM) *

Great JOB .... This post really interesting....

I learn new stuff from you mogstronaut , pbl and gl3thr0...

But Can I ask about the Thread problem?

Can we stop or destroy a Thread? If can how Can I do that?

In JDK1.6.turn.gif4, discard the function t.destroy() , t.stop() and t.resume().... sad.gif

Thank


The better way to stop a thread is to have the threat to check for a boolean periodically

CODE

class XXX extends Thread {
   boolean hasToStop;
   public void run() {
       ...
       ...
       if(hasToStop) return;
       ...
       Thread.sleep(10L);
       if(hasToStop) return;
       ....

and the guy who created the Thread can
CODE

     Thread t = new XXX();
     t.start();
     ...
     ...
     t.hasToStop = true;
     ...


This way you are sure you are not interrupting the thread in the middle of an operation (like a write) that can generate strange situations or data corruption.
Doing a Thread.stop() or interrupt() on a Thread that is writting in a Vector is not a good idea at all you can end up with a really corrupted vector. Having the thread to check for hasToStop you are sure it won't be in the middle of some other operation.
User is online!Profile CardPM
+Quote Post

gl3thr0
RE: Help Modifying This GUI
5 Jun, 2008 - 07:39 AM
Post #11

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
QUOTE(gl3thr0 @ 4 Jun, 2008 - 11:09 AM) *

Sorry gl3thr0 NOT a good idea at ALL to Thread.sleep() in an actionPerformed() method or any other AWT event
method that can really screw up any GUI
Your idea is not good at all....
Don't really know the details behind but imagine paint() is called in the same Thread. Your repaint() won't be executed until the Thread ends of its sleep


yes your right of course when the button is clicked it will look like its still pushed-in until the loop is finished.

i posted the code knowing this but hoping that mogstronaut would take both of our code and put it together.
if i did it id probably create an inner class that extended (Thread) and then override the run() method with the logic from my code BUT OH WELL

This is the code from my inner class im just using simple print statments to demonstrate the concept
java

public class Cycle extends Thread{ //this would be the inner class of TrafficLight
public void run(){
for(int i=0, x=2,y=1;i<3;i++,x++,y++){ //just like before to move through the array
if(x>2){
x=0;
}
if(y>2){
y=0;
}
System.out.println("i ="+i); //this is were u would turn lights on or off.
System.out.println("x ="+x);
System.out.println("y= "+y);
try {
this.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}

then from the actionPerformed() method you would do the same tht pbl did
Cycle c = new Cylce(); ----create the thread
c.start(); ----- start the thread

This post has been edited by gl3thr0: 5 Jun, 2008 - 07:52 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month