Java/Netbeans Assistance Desired (Not Homework)

For my Introduction to Programming class, we’ve been studying Java through the Netbeans 3.6 (I know there is a more recent version available, but it was released after the class began, and the instructors wanted to use a consistent version. So we’re also using Java 1.4.6). This particular assignment was to use threading to make a ball bounce. Thus, I created an applet with one thread being the GUI part, that decides when to repaint it, and a class for the ball, which dictates when and where the ball moves.

All my code seems to work properly. Each thread is doing exactly what I told it to, and through console output, you can see the position of the ball moving more or less how I told it to (there are some minor possible glitches with the upper and lower limits that I haven’t quite worked out, but they should be comparitively trivial. Likewise, a better model of gravity should also be fairly easy to add once I finish this).

Unfortunately, when I actually run my applet, either in Netbeans or on a web page, nothing whatsoever gets painted. I’m using java.awt.Graphics, and have done so in the past, so I’m somewhat familiar with it. Moreover, I know the “paint” part of my applet itself is being ran, it just won’t run the parts that actually draw anything.

You can find my code at www.willamette.edu/~btrautma/BallBouncer.zip . Be careful regarding stability, as it’s running two active threads the entire time, which could slow down some computers. If anyone can help me, please do so! Note that my instructor looked at my code and program at length, and could not figure out what was going on. Note also that if I use the built in “repaint()” method rather than calling paint() directly, it never actually runs the method at all, which is further compounding my immense confusion.

Please help!

Netbrian

I am not sure if it is relevant (I don’t have the time right now to look at your code) but it is worth noting that the AWT and Swing classes are not thread safe. That means that all AWT code must be run from a single event-dispatching thread and you must not make an AWT call, including paint() from another thread. I think you can call repaint() because all it does is arrange for paint() to be called in the event-dispatching thread.

Just had a quick look at your code. I think the problem may be the interaction between paintLoop() and the GravityBall thread. You do not want to have your ThreadApplet and GravityBall both call sleep(…). Try removing paintLoop() and having run() in GravityBall call repaint() (not paint()) on the ThreadApplet