I'm stuck on the GameDev Java Game Programming 2 link. I figured out that I was getting an error because the author forgot to import java.awt.*;, but now it appears that my repaint() function isn't working correctly. The count for i is not increasing and it just stays stuck at i = 0.

You should have started a new thread instead of hijacking an old thread.

But to answer your question, try putting the word "volatile" before the declaration of "int i;"

This happens because usually the CPU caches data that is in use often, instead of accessing RAM repeatedly. This causes variables to not be in sync, especially here since you have a thread updating "i" and another thread, the EDT, repainting the screen. Setting a variable as "volatile" makes sure it is updated among all threads.

Right but it is not created, it is a queue that dispatches events, hence its called the Event Dispatching Thread, EDT.And also remove that call to "i = 0" that will set "i" back to 0 after the tread starts incrementing it.

publicvoidstop() { //Stopping a thread is important, and destroy() is deprecated so we use this insteadth = null; }

@Overridepublicvoidrun() {ThreadthisThread = Thread.currentThread();

while (th == thisThread) {//just makes sense there, if th is null (say, if we wanted it to stop by using stop(), //or if it was null because you closed the application straight up) then it wont equal thisThread //and it will stop without giving you an errori++;repaint();

try{Thread.sleep(1000); } catch (InterruptedExceptione) {break; //why didn't you have this there in the first place? } } }

publicvoidpaint(Graphicsg) {g.drawString("i = "+i, 10, 20); }}

If that doesn't work it's gotta be something wrong with the HTML or something else...

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org