I have now made a couple of games using java2d and they work pretty well. However i was wondering why simply filling rectangles with a color took extremely long time to do, so i made a quick test. In this test i found that my images were never accelerated and getAvailableAcceleratedMemory() on GraphicsDevice always returned 0. I tested this on two machines, one running Windows Vista sp 1 and another running Windows Xp sp 2.
I did a couple of more tests to be sure. And i came up with this test case:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tester extends JFrame{
protected Image img;
public Tester(){
Canvas c = new Canvas(){
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
};
c.setSize(512,512);
add(c);
System.out.println("test!");
setResizable(false);
validate();
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
img = createVolatileImage(512,512);//createImage(512,512);
Graphics gra = img.getGraphics();
gra.setColor(Color.BLUE);
gra.drawLine(0,0,512-1,512-1);
gra.dispose();
int delay = 1000;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
GraphicsConfiguration gc = getGraphicsConfiguration();
System.out.println("img.isAccelerated(): "+img.getCapabilities(gc).isAccelerated());
System.out.println("img.isTrueVolatile(): "+img.getCapabilities(gc).isTrueVolatile());
}
};
new Timer(delay, taskPerformer).start();
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
new Tester();
}
});
}
}
When running with -Dsun.java2d.trace=count on my Windows Vista machine for a few seconds i get this result:
test!
img.isAccelerated(): false
img.isTrueVolatile(): false
img.isAccelerated(): false
img.isTrueVolatile(): false
img.isAccelerated(): false
img.isTrueVolatile(): false
2 calls to sun.java2d.windows.GDIBlitLoops::Blit(IntRgb, SrcNoEa, "GDI")
5 calls to sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt)
1 call to GDIFillRect
1 call to sun.java2d.loops.DrawLine::DrawLine(AnyColor, SrcNoEa, AnyInt)
9 total calls to 4 different primitives
When running with -Dsun.java2d.opengl=true aswell, i get a window with a blank white canvas in the middle.
The console output after a few seconds is however:
test!
img.isAccelerated(): true
img.isTrueVolatile(): true
img.isAccelerated(): true
img.isTrueVolatile(): true
6 calls to OGLFillRect
1 call to OGLDrawLine
2 calls to sun.java2d.opengl.OGLRTTSurfaceToSurfaceBlit::Blit("OpenGL Surface (r
ender-to-texture)", AnyAlpha, "OpenGL Surface")
9 total calls to 3 different primitives
Please help me understand these results…
You will probably need more info on my system, but i didn’t know what was needed or how to get this info, so please ask if more info would help
- Scarzzurs