• Java swing/threading issue
    8 replies, posted
[CODE]import java.awt.Color; public class bullet { public bullet(int x, int y, boolean up) { int start = up?15-y:Math.abs(y-15); int cNt = 0; for(int i=start;15>start;start++) { try { engineMenu.staticSGC(x,cNt,Color.RED); Thread.sleep(300); } catch (InterruptedException e) {} engineMenu.staticSGC(x,cNt,Color.WHITE); cNt += 1; } } } [/CODE] engineMenu.staticSGC colors a JPanel (swing) What is happening is its going through the loop when I add a print statement in it where engineMenu.staticSGC is called it calls it fine but it waits til after everything and draws a white line. What I want to happen is it make a red line after 300 ms turn into a white line and repeat.
According to that code, it should be red for 4.5 seconds and then turn white-- this is because it's turning red again immediately after it turns white. This is what's happening in the loop: turn white. turn red. wait 300 milliseconds. But clearly that's not what you're actually seeing, right? [editline]11th September 2011[/editline] Try this: [code] for(int i=start;15>start;start++) { try { engineMenu.staticSGC(x,cNt,Color.RED); Thread.sleep(150); engineMenu.staticSGC(x,cNt,Color.WHITE); Thread.sleep(150); } catch (InterruptedException e) {} cNt += 1; } [/code] This should alternate between red and white for 4 and a half seconds. EDIT: Crap, no, I see what's wrong: [code] for(int i=start;15>start;start++) [/code] It should be this: [code] for(int i=start;i<15;i++) [/code] EDIT2: Or maybe it's right... I'm probably just very confused. The way you have it, i isn't being used for anything.
Is there an exception being thrown by the sleep? That would cause it to instantly turn white.
[QUOTE=zzaacckk;32233720][CODE]import java.awt.Color; public class bullet { public bullet(int x, int y, boolean up) { int start = up?15-y:Math.abs(y-15); int cNt = 0; for(int i=start;15>start;start++) { try { engineMenu.staticSGC(x,cNt,Color.RED); Thread.sleep(300); } catch (InterruptedException e) {} engineMenu.staticSGC(x,cNt,Color.WHITE); cNt += 1; } } } [/CODE] engineMenu.staticSGC colors a JPanel (swing) What is happening is its going through the loop when I add a print statement in it where engineMenu.staticSGC is called it calls it fine but it waits til after everything and draws a white line. What I want to happen is it make a red line after 300 ms turn into a white line and repeat.[/QUOTE] Instead of a for loop, in your rendering loop do something like this: [code] if(System.currentTimeMillis()>500) engineMenu.staticSGC(x,cNt,Color.RED); else engineMenu.staticSGC(x,cNt,Color.WHITE); [/code]
Neither worked and no exception is called. Whats really confusing me is it turns white after the loop has been completed, if I add a print function to engineMenu.staticSGC it gets printed but nothing happens til the loop is done.
Perhaps there is a problem with your "staticSGC" method? The code you've posted doesn't seem like it'd cause any problems on its own.
wat do u want it 2 do? like smashmastrer said, ure code makes it red, wait 300ms, then go white then INSTANT back 2 red.. what is happening when u run it? is it instant going white, never red?
[QUOTE=zzaacckk;32239626]Neither worked and no exception is called. Whats really confusing me is it turns white after the loop has been completed, if I add a print function to engineMenu.staticSGC it gets printed but nothing happens til the loop is done.[/QUOTE] Put the red coloring outside the try statement [editline]12th September 2011[/editline] [QUOTE=IZAC;32247404]wat do u want it 2 do? like smashmastrer said, ure code makes it red, wait 300ms, then go white then INSTANT back 2 red.. what is happening when u run it? is it instant going white, never red?[/QUOTE] Please type with at least some grammar, it makes it much easier to read.
ye ye i do i use comma my other q, is r u using java2D? cos if so, depends on ure staticSGC method, but ure graph obj mayb set 2 white sumwher, dunno tho. mayb active/pass rendering cum in, u use ignoreRepaint()?
Sorry, you need to Log In to post a reply to this thread.