File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Code executes fast inside Eclipse, slow in runnable JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Code executes fast inside Eclipse, slow in runnable JAR" Watch "Code executes fast inside Eclipse, slow in runnable JAR" New topic
Author

Code executes fast inside Eclipse, slow in runnable JAR

Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 96

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.

Any suggestions as to what I'm doing wrong?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I need to replicate SOME of the behavior of JButtons, including the redrawing of the image when the pseudo-button is pressed.

have you tried using JLabels and setting the icons to normal/pressed (label.setIcon(...) will do most of the work for you)
Chris Crawford
Ranch Hand

Joined: Jun 22, 2011
Posts: 96

I had to eschew use of Swing components for reasons too complicated to get into. It is likely that I simply don't know how to make Swing dance to my tune.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Code executes fast inside Eclipse, slow in runnable JAR