|
|
|
#1 (permalink) | |||||||||||||
|
New to Overclock.net
|
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 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
Last edited by jrs : 04-21-08 at 08:21 AM. |
|||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||
|
Chiefly Ignorant
|
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.
__________________
|
||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
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;
}
}
__________________
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
New to Overclock.net
|
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()); Thanks ~jrs
__________________
Watercooling via aqua 1300, bonnie heatercore, and Apogee
Last edited by jrs : 03-24-08 at 08:22 PM. |
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Original Gangsta
![]() |
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:
--Randy Pausch
|
||||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||||
|
New to Overclock.net
|
Quote:
. 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
|
||||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
New to Overclock.net
|
what the hell jeff i was using that
__________________
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|