Hello there Java Gurus! ;D
This is my first post to this forum, I have about 6 months of experience of Java in general, and about 1 month of Java GUI programming (Swing), so consider me as a Java newbie.
I started to develop a simple 2D shooter game with Java swing as a hobby project. I am using Java 1.6.0_65 with Mac OS X 10.6.8, Macbook Pro 2.4 GHz Intel Core 2 Duo (Spring 2010 model), the display card is NVIDIA GeForce 320M.
In my paint method, I draw about 30-50 rectangles with Swing drawRect() method, sizes of 32 * 32 pixels, few are of 7 * 7 pixels of size, the smaller ones are filled with fillRect. I call the paintComponent() method 50 times/sec from a timer callback.
To get some kind of idea of how fast/slow the painting is, I added some measurement code:
public void paintComponent(Graphics g)
{
long startTime = System.nanoTime();
//
// call the drawing methods here
//
// for debugging paint speed
long endTime = System.nanoTime();
if (paintDurationBufferIdx < DURATION_BUFFER_LEN)
paintDurationBuffer[paintDurationBufferIdx++] = (int)(endTime - startTime) / 1000;
else
{
long avg = 0;
for (int ii = 0; ii < DURATION_BUFFER_LEN; ii++)
avg += paintDurationBuffer[ii];
avg /= DURATION_BUFFER_LEN;
System.out.println("Paint avg. duration: " + avg + " us");
paintDurationBufferIdx = 0;
}
Average execution time per single paintComponent() seems to be about 3000 usec.
I also run same code in couple for Windows 7 PCs (Sony Vaio laptop, AMD dual core 2.x GHz and HP laptop, Intel quad core, 2.x GHz, donβt remember all details now). In Windows machines, average execution time was only about 100 us, that is about 30 x faster!
What can explain such a big difference? Can System.nanoTime() work with different resolutions between Windows 7 & OS X, or is painting really that slow in OS X?
On the other hand, I also measured average execution time of the non-GUI related part of my game engine (moving of objects, collision detections etc.), and it was between 50 - 100 us on both system, so maybe System.nanoTime() is reliable, and painting really is that slow in OS X?