I decided to continue my Java graphics programming with a simple program to draw a sprial. This should have been a cake walk. I kept in mind that the Math functions took angles in radians. My mind thinks of angles in degrees. The Math package can convert between the two units. For some reason, my graphics were not showing up on the screen. This was frustrating. A good developer know how to debug these things.
I output my coordinates to the console. Everything looked fine. Coordinates were getting further and further away from the center. Looked like the drawLine funciton was just not producing any output. I did all my drawing in a loop contained in the paintComponent function. I tried sticking some hard coded coordinates in my drawLine call. No change.
Then I moved over to the Graphics2D world. The draw function there did not work either. I did eventually make a breakthrough. Calls to drawLine outside my loop worked. WTF? Loops should not matter. And the output was good. I saw it on the console. Finally it hit me. I was initializing all my variables in the JPanel constructor. The loop stopped drawing when the lines went off the screen. My problem was that paintComponent was getting called more than once.
The first call to paintComponent worked fine. It also output the correct coordinates to the screen. However the subsequent call(s) to the function erased the screen. By then the variables had incremented past the point where they could be viewed on the screen. I guess I need to learn how to run the code in real time in a debugger. This is a little tricky with graphics programming, as I would need to see the graphics being drawn in real time as well. Sounds like a need for some type of graphics debugger.
Free Laundry
-
Apparently a lot of apartment buildings have coin operated laundry machines
in the basement. And guess what? You can order a key to unlock the payment
me...
5 years ago