New Posts  All Forums:Forum Nav:

Java: Traffic Light

post #1 of 7
Thread Starter 
I have an assignment for AP Java to create a switching traffic light app. The Traffic Light is suppose to be drawn then there is a button to change the light. It is a relatively simple program and it is working for the most part. I have one problem....when the light is suppose to be yellow it isn't. I cannot see any problems with my code and have tried different approaches.
Code:
//taken out..noobs in class stealing code....gari
Looking at the control method..it calls repaint when state is 1 which means a light that is yellow should be drawn. I had a different version of control using a for loop which produced the same result. it entered an if statement for yellow evidenced by a println I put in. However, paintComponent was never called because I have a println there. Repaint seems to get called it just doesn't...enter the if statement for drawing the yellow light. I am probably doing a poor job explaining this. Maybe someone can look at my code for me and figure it out because I have looked at it for hours and tried different things.

The rest of this works. I click the button it goes from green to red....click again red to green ect.

Thanks in advance

~jrs
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
post #2 of 7
I'm thinking that the repaint() does not immediately call paintComponent, but rather puts it in the queue to be called later. I've never messed with Swing, but my googles have indicated that there is danger in using Thread.sleep inside the Event Dispatch Thread; you're not giving Swing a chance to actually process your repaint request because you're starving the EDT. Try creating a new Thread inside Traf that does the actual sleep, state change, and repaint. Do a search on the net for SwingUtilities.invokeLater for examples.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #3 of 7
Quote:
Originally Posted by Scriptorum View Post
I'm thinking that the repaint() does not immediately call paintComponent, but rather puts it in the queue to be called later. I've never messed with Swing, but my googles have indicated that there is danger in using Thread.sleep inside the Event Dispatch Thread; you're not giving Swing a chance to actually process your repaint request because you're starving the EDT. Try creating a new Thread inside Traf that does the actual sleep, state change, and repaint. Do a search on the net for SwingUtilities.invokeLater for examples.
I always have 1 underlying frame to keep components encapsulated in the main form. To refresh, I used

mainPanel.paintImmediately(mainPanel.getBounds());


That avoids the whole queue problem that Scriptorum mentioned. I've also included a code of mine that I used to create a player marker for a Monopoly game I did. Basically it just redraws and clears a circle on the screen. You could just use the setColor method and put 3 of these player marks on your panel/form.

Code:
public class PlayerMarker extends javax.swing.JPanel{
    /**
     * Description.
     * @param var    description of variable
     * @return  description of returned value
     */
    private int x=401;
    private int y=411;
    private java.awt.Color col;
    private boolean dispose=false;
    
    public PlayerMarker() {
        super();
    }
    
    public PlayerMarker(java.awt.Color c) {
        super();
        col = c;
    }
    
    public void paint(java.awt.Graphics g){
        java.awt.Graphics Gr = this.getGraphics();
        if(Gr != null)
            Gr.dispose();
        if(dispose)
            g.setClip(0,0,1,1);
        g.setColor(col);
        g.fillOval(x,y,21,21);
        g.setColor(java.awt.Color.BLACK);
        g.drawOval(x,y,20,20);
    }
    
    public void setColor(java.awt.Color c){
        col = c;
    }
    
    public void setCoord(int xloc, int yloc){
        x = xloc;
        y = yloc;
        repaint();
    }
    
    public void dispose(){
        dispose = true;
    }
    
}
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #4 of 7
Thread Starter 
Thanks guys, I got it working

Scriptorum I know what you mean about the EDT but of course I didnt think of it. My teacher doesn't really teach us much. There are only 8 kids in my high school that take Java.

C-Bro thanks for the example and the paintImmediately. I switched myPanel to a this reference and replaced all the repaint calls with it. It works well now
Code:
this.paintImmediately(this.getBounds());
Now I can move on to cutting down the size of the control method.

Thanks

~jrs
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
post #5 of 7
Hmm, did you take a look at the GridWorld case study yet? We finished up with that a month back or so Nice coding
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
Burning Phoenix
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Q9550 Gigabyte EP45-UD3P eVGA GTX 260 Core 216 SuperClocked 4GB G.Skill DDR2-1000 5-5-5-15 
Hard DriveOptical DriveOSMonitor
3 WD 80GB RAID0, Seagate 500GB, WD 1TB Caviar BLCK Lite-On LH-20A1S Windoze 7 Professional Dual Dell E207WFP 20.1" Widescreen LCD's 
KeyboardPowerCaseMouse
Logitech G15 Silverstone OP750 Lian-Li PC-A10B Logitech G5 
Mouse Pad
Harley Davidson 
  hide details  
Reply
post #6 of 7
Thread Starter 
Quote:
Originally Posted by Burn View Post
Hmm, did you take a look at the GridWorld case study yet? We finished up with that a month back or so Nice coding
Noooope. Haha I should really get going on that. We started the year slow and didn't get much done. The past month has been nothing but programing, short answer, MC's and whatnot(lots at home too). Our book is terrible(Java Software Solutions for Computer Science A). All the graphical stuff is spread out, and the text beats around the bush. We are just finishing up recursion now.....so from now until the beginning of may is review time. Our teacher bought a class set of the Barron's books, we'll see how that goes.

~jrs
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
AM3
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 740 unlocked to quad Biostar 880GB+ Built-in 4250 Corsair DDR3 (2 x 2gb) 
Hard DriveOptical DriveMonitorPower
320gb Maxtor Asus DVD-RW Samsung t260hd PC P&C 500W 
Case
HEC 6t 10BB 
  hide details  
Reply
post #7 of 7
what the hell jeff i was using that
    
CPUMotherboardGraphicsRAM
Intel Pentium D 945 3.4 GHz ASUS P5N-SLi nForce 570 SLi 7600 GT KO 3GB Dual Channel OCZ DDR2 
Hard DriveOSMonitorKeyboard
300GB Maxtor Diamondback, 150GB Maxtor Diamondback Windows XP Pro HANNS-G 19" LCD 5ms Logitech G15 
PowerCaseMouseMouse Pad
Ultra X2 550W Homemade Wooden Box Logitech G7 Hardwood Desk 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Pentium D 945 3.4 GHz ASUS P5N-SLi nForce 570 SLi 7600 GT KO 3GB Dual Channel OCZ DDR2 
Hard DriveOSMonitorKeyboard
300GB Maxtor Diamondback, 150GB Maxtor Diamondback Windows XP Pro HANNS-G 19" LCD 5ms Logitech G15 
PowerCaseMouseMouse Pad
Ultra X2 550W Homemade Wooden Box Logitech G7 Hardwood Desk 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming