Applet vs Frame

Hi there

I am new, I hope I dont ask something you already had a dozen times.

I have just been working on some small experiments for Java 2D Games, using Polygons. Once do draw rectangular tiles with height, once to draw Hex fields.

I did one of these tests as Applet, the other as standalone Application with a Frame. And I took performance measures of my paint method with System.nanoTime().

Now the problem I found was, that the drawing in the Applet was extremely fast, while the drawing in the Frame was extremely slow. To verify this, i wrote a performance test. I createt a “load” class which just has 400 4-edged polygons. Then I had Java draw these 400 polys, once in a Frame and once in an Applet. The Applet did the paint method in like 1 ms, while the Frame took up to 400 ms.

I tried everything through. No backbuffer, BufferedImage as backbuffer, VolatileImage as backbuffer, or a BufferStrategy. I usually get a 400 ms performance. When I use a createCompatibleImage Method instead of new BufferedImage i manage to get around 200 ms. When I draw directly on the frame.getGraphics() I get around 100 ms, but it flickers.

In the end, I have 2 questions:

  1. Why is the Applet so much faster in doing the same? Can anyone verify this experience?
  2. What can I do to improve my Performance?

My main PC is a 450 MHz CPU and not really that fast. But I have a Notebook with 2 GHz and I expect good results at least there.

Thanks
-JAW

The code of the load class


import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class Last
{

	Polygon[] polys;
	Color[] col;
	
	Last(int anzahl)
	{
		polys = new Polygon[anzahl];
		col = new Color[anzahl];
		Random rnd = new Random(12345678);
		for( int i = 0; i < anzahl; i++ )
		{
			Polygon p = new Polygon();
			p.addPoint(rnd.nextInt(800),rnd.nextInt(600));
			p.addPoint(rnd.nextInt(800),rnd.nextInt(600));
			p.addPoint(rnd.nextInt(800),rnd.nextInt(600));
			p.addPoint(rnd.nextInt(800),rnd.nextInt(600));
			polys[i] = p;
			col[i] = new Color(0,rnd.nextInt(200)+50,0);
		}
	}
	
	public void zeichne(Graphics2D g2d)
	{
		for( int i = 0; i < polys.length; i++ )
		{
			g2d.setColor(col[i]);
			g2d.fill(polys[i]);
		}
	}

}

I ran the same Applet on another PC and got equal results as with a frame. The difference between Applet and Frame seems
to be small, it looks more like a problem with the timing or so.

Are you sure the applet is running in Sun’s VM, not Microsofts?

I also never soo a big difference, since Mustang its even possible to use OpenGL for applets :wink:

Well I can only produce this results on one PC. Other PCs give me the same timing, Applet or Frame.

I have been looking for JAva2d Performance Improvement Information, but i cannot find much of use.

The point is, I have my superold 450 MHz CPU and no OpenGL Support for Java, when I use the -Dsun.opengl parameter it says OpenGL cant be started or something like that.

On the one hand I was able to draw 20x20 BufferedImages in no time, like 15 ms. On the other hand sometimes my 100 Poygons are extremely slow. Sure, its the filling that takes the time. At least I discovered the createCompatible… Methods, which gave me up to 50% speed gain.

But sometimes I write a test and end up with a rendering time of 100 ms and I just have to drop the idea. I think I must learn JOGL or something somewhen after all.

-JAW