开发者

Drawing performance in Java 6 updates 19,20 versus Java 6 update 3?

I'm getting twice the frame rate with the earlier Java 6 u 3, than with the new ones. Very weird. Can anyone give some explanation?

On Core 2 Duo 1.83ghz, integrated video (only one core is used) - 1500 (older java) vs 700 fps On Athlon 64 3500+, discrete video - 120 (older java) vs 55 fps

The app is a simple game with a moving rectangle. I'm using Graphics2D to draw from a loop.

Edit: Some code. The whole thing is big, this are some important parts only.

public class SimpleRenderer extends JFrame{
SimpleGameEngine sge;
Canvas canvas; // Our drawing component
static final int WIDTH = 640;
static final int HEIGHT = 480;
Graphics2D g2d = null;
Graphics graphics = null;   
Color background = Color.BLACK;
BufferedImage bi;
BufferStrategy buffer;

public SimpleRenderer(KeyboardInput keyboard, SimpleGameEngine sge) throws HeadlessException {
    this.sge = sge;
    setIgnoreRepaint( true );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    canvas = new Canvas();
    canvas.setIgnoreRepaint( true );
    canvas.setSize( WIDTH, HEIGHT );
    add( canvas );
    pack();

    // Hookup keyboard polling
    addKeyListener( keyboard );
    canvas.addKeyListener( keyboard );

    canvas.createBufferStrategy( 2 );
    buffer = canvas.getBufferStrategy();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfigurat开发者_如何学运维ion();
    bi = gc.createCompatibleImage( WIDTH, HEIGHT );


    this.setVisible(true);
}


public void draw(int fps) {
    g2d = bi.createGraphics();
    g2d.setColor( background );
    g2d.fillRect( 0, 0, WIDTH, HEIGHT );

    g2d.setColor(  Color.GREEN );
    g2d.drawString( "Use arrow keys to move rect", 100, 20 );
    g2d.drawString( "Press esc to exit", 100, 32 );
    g2d.setColor(  Color.GREEN );
    g2d.drawString( "FPS: "+fps, 20, 20 );
    g2d.drawRect( sge.bob.x, sge.bob.y, sge.bob.w, sge.bob.h );


    graphics = buffer.getDrawGraphics();
    graphics.drawImage( bi, 0, 0, null );
    if( !buffer.contentsLost() )
        buffer.show();
}
...

The game loop:

    ...
    long loop =0;
    long update = 0;
    long start = System.currentTimeMillis();
    long lastIterationTime = System.nanoTime();

    long nanoseccount=0;
    int cyclec = 0;
    int fps=0;
    System.out.println("start");

    while(run) {
        long now = System.nanoTime();
        loop++;
        while(lastIterationTime + StepSize*1000000 <= now && run ==true) {
            Update(StepSize);
            update++;
            lastIterationTime += StepSize*1000000;
        }

        Draw(fps);
        nanoseccount += System.nanoTime()-now;
        cyclec++;

        if (nanoseccount >= 1000*1000000 ) {
            fps = (int)Math.round((double)cyclec/(nanoseccount/1000000000));
            nanoseccount = 0;
            cyclec = 0;             
            continue;
        }

    }
    System.out.println("loop "+ loop +" # update "+ update+ " # u/l " + ((double)update/loop)*100);
    long runtime = (System.currentTimeMillis()-start);
    System.out.println("run time "+ (double)runtime/1000 +"s # loop/s "+ ((double)loop/((double)runtime/1000)));
    System.out.println("updates/s "+ ((double)update/((double)runtime/1000)));
...


Java 6 versions 19,20 and the first versions published by Oracle instead of Sun. They have a gazillion problems in many areas. Web start functionality is almost broken for example.

Here is one issue like this. Google for more if you want.

For the time being, I would suggest that you stick to the older versions, and wait for the next releases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜