Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-23-08   #1 (permalink)
jrs
New to Overclock.net
 
jrs's Avatar
 
amd nvidia

Join Date: Jun 2006
Posts: 277

Rep: 39 jrs is acknowledged by some
Unique Rep: 31
Trader Rating: 0
Default Java: Traffic Light

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
__________________
Watercooling
via aqua 1300, bonnie heatercore, and Apogee

System: My System
CPU
165 CCBBE 0617FPMW
Motherboard
DFI Ultra-D
Memory
kingston value
Graphics Card
7600gt 730/1500
Hard Drive
maxtor 300gb SATA
Sound Card
sound blaster live
Power Supply
ultra x2 550w
Case
chieftec dragon
CPU cooling
apogee
GPU cooling
stock
OS
2003 server
Monitor
20" sony crt

Last edited by jrs : 04-21-08 at 08:21 AM.
jrs is offline   Reply With Quote
Old 03-23-08   #2 (permalink)
Chiefly Ignorant
 
Scriptorum's Avatar
 
intel nvidia

Join Date: Jan 2008
Location: Atlanta, GA
Posts: 54
Blog Entries: 12

Rep: 13 Scriptorum Unknown
Unique Rep: 10
Trader Rating: 0
Default

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.
__________________
Quote:
Originally Posted by The Bartender Paradox View Post
crazy?...nah. when you cool with a fire extinguisher then your crazy.

System: Flaming Moe's MacBook Pro
CPU
Core 2 Duo
Motherboard
MacBook Pro
Memory
4GB DDR2-667
Graphics Card
512Mb NVIDIA GeForce 8600M GT
Hard Drive
200Gb 7200RPM
OS
OSX 10.5.2 / XP Pro / Boot Camp + Parallels
Monitor
17" Matte 1920x1200
Scriptorum is offline Overclocked Account   Reply With Quote
Old 03-24-08   #3 (permalink)
110100001101001111000
 
C-bro's Avatar
 
intel nvidia

Join Date: Jan 2006
Location: Hamilton, ON
Posts: 1,773

Rep: 276 C-bro is a proven memberC-bro is a proven memberC-bro is a proven member
Unique Rep: 209
FAQs Submitted: 6
Folding Team Rank: 209
Hardware Reviews: 9
Trader Rating: 1
Default

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

System: RAID0R
CPU
Intel E2180 3.33GHz
Motherboard
Asus P5K-E/WIFI-AP vMod
Memory
2GB Kingmax DDR2-1066
Graphics Card
EVGA 8800GT
Hard Drive
2x250GB WD+500GB 7200.11
Sound Card
SB Audigy 2
Power Supply
Corsair CMPSU-550VX
CPU cooling
Arctic Cooling Freezer 7 Pro
GPU cooling
Zalman VF900-Cu
OS
Windows Vista Business 32-Bit
Monitor
HP F2105 21" & Samsung 712N
C-bro is offline I fold for Overclock.net Overclocked Account C-bro's Gallery   Reply With Quote
Old 03-24-08   #4 (permalink)
jrs
New to Overclock.net
 
jrs's Avatar
 
amd nvidia

Join Date: Jun 2006
Posts: 277

Rep: 39 jrs is acknowledged by some
Unique Rep: 31
Trader Rating: 0
Default

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
__________________
Watercooling
via aqua 1300, bonnie heatercore, and Apogee

System: My System
CPU
165 CCBBE 0617FPMW
Motherboard
DFI Ultra-D
Memory
kingston value
Graphics Card
7600gt 730/1500
Hard Drive
maxtor 300gb SATA
Sound Card
sound blaster live
Power Supply
ultra x2 550w
Case
chieftec dragon
CPU cooling
apogee
GPU cooling
stock
OS
2003 server
Monitor
20" sony crt

Last edited by jrs : 03-24-08 at 08:22 PM.
jrs is offline   Reply With Quote
Old 03-24-08   #5 (permalink)
Original Gangsta
 
Burn's Avatar
 
intel nvidia

Join Date: Sep 2004
Location: Massachusetts
Posts: 12,856
Blog Entries: 2

FAQs Submitted: 14
Folding Team Rank: 73
Team Name: CPU Burners
Hardware Reviews: 5
Trader Rating: 7
Default

Hmm, did you take a look at the GridWorld case study yet? We finished up with that a month back or so Nice coding
__________________
My CNC Mill Project
Quote:
Originally Posted by CyberDruid
Burn + Electricty + Water =
But the man has the patience of a saint...
We cannot change the cards we are dealt, just how we play the hand.
--Randy Pausch

System: Burning Phoenix
CPU
Intel E6600
Motherboard
Abit AB9 QuadGT
Memory
2GB Team Xtreem DDR2 1000 5-5-5-10
Graphics Card
eVGA 8800GTS 640MB
Hard Drive
4 Western Digital 80GB SATAII RAID 10
Sound Card
Creative Sound Blaster Audigy 2 ZS
Power Supply
Silverstone OP750
Case
Lian-Li PC-A10B
CPU cooling
D-Tek Fuzion
GPU cooling
Swiftech Stealth Rev. 2
OS
Windoze Vista Ultimate SP1
Monitor
Dell E207WFP 20.1" Widescreen LCD
Burn is offline I fold for Overclock.net Overclocked Account Burn's Gallery   Reply With Quote
Old 03-24-08   #6 (permalink)
jrs
New to Overclock.net
 
jrs's Avatar
 
amd nvidia

Join Date: Jun 2006
Posts: 277

Rep: 39 jrs is acknowledged by some
Unique Rep: 31
Trader Rating: 0
Default

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
__________________
Watercooling
via aqua 1300, bonnie heatercore, and Apogee

System: My System
CPU
165 CCBBE 0617FPMW
Motherboard
DFI Ultra-D
Memory
kingston value
Graphics Card
7600gt 730/1500
Hard Drive
maxtor 300gb SATA
Sound Card
sound blaster live
Power Supply
ultra x2 550w
Case
chieftec dragon
CPU cooling
apogee
GPU cooling
stock
OS
2003 server
Monitor
20" sony crt
jrs is offline   Reply With Quote
Old 04-21-08   #7 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Apr 2008
Posts: 1

Rep: 0 Gari Unknown
Unique Rep: 0
Trader Rating: 0
Default

what the hell jeff i was using that
__________________
System: Super Leet Killah PC
CPU
Intel Pentium D 945 3.4 GHz
Motherboard
ASUS P5N-SLi nForce 570
Memory
3GB Dual Channel OCZ DDR2
Graphics Card
SLi 7600 GT KO
Hard Drive
300GB Maxtor Diamondback, 150GB Maxtor Diamondback
Sound Card
HD Audio Onboard
Power Supply
Ultra X2 550W
Case
Homemade Wooden Box
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Pro
Monitor
HANNS-G 19" LCD 5ms
Gari is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 12:02 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.19876 seconds with 9 queries