This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm putting this in the Swing area because it involves the use of repaint(); however, the underlying problem seems to be more fundamental. I have some big images (128w by 128h) that I'm treating as simple graphics objects rather than JButtons, for complicated reasons. I need to replicate SOME of the behavior of JButtons, including the redrawing of the image when the pseudo-button is pressed. This is handled with a very simple bit of code inside the mousePressed() method of the Mouse Listener:
The value of cButtons is 16, so the loop executes a maximum of 16 times; in this test case, only seven buttons get past the first if-test. faceWidth and faceHeight are equal to 128.
When I run the program inside Eclipse, everything runs just fine, but when I export it to a Java runnable file, with "Package required libraries into generated JAR", and run the result, I get a problem: the buttons blink. That is, the repainting process takes so long that erasing the background takes an entire frame jiffy and drawing the button takes more than one frame jiffies. It gets worse: the length of the blink is visibly concomitant with the position of the button in the array. That is, the first three buttons on the screen are drawn without blinks, the fourth has a very short blink, the fifth a visibly longer blink, and the sixth even longer. In other words, this code is requiring at least 5 milliseconds per loop iteration, and possibly as much as 20 milliseconds per loop iteration. That's ridiculous! This is running on a Mac Pro with two 2.66 GHz dual-core processors and over 3 GB of free RAM according to the Activity Monitor.
Why is it running so slowly as a runnable JAR when it runs so fast inside Eclipse? We'd expect that Eclipse would slow it down, if anything. I fear that my problem lies in some magical incantation I have to perform with Eclipse to tell it to prepare the final code in a manner that runs quickly. I don't know if this is a Java problem or an Eclipse problem, so I'm also posting this problem to the beginner's forum there.