与早期的Java 6 u 3相比,我得到的帧频是以前的两倍。很奇怪。谁能解释一下?

在Core 2 Duo 1.83ghz上,集成视频(仅使用一个内核)-1500(旧版Java)与700 fps
在Athlon 64 3500+上,离散视频-120(旧Java)对55 fps

该应用程序是一个带有移动矩形的简单游戏。我正在使用Graphics2D从循环中绘制。

编辑:一些代码。整个事情很大,这仅仅是一些重要的部分。

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.getDefaultConfiguration();
    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();
}
...


游戏循环:

    ...
    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版本19,20和Oracle代替Sun发布的第一个版本。他们在许多领域都面临着巨大的问题。例如,Web启动功能几乎被破坏了。

这是一个issue like this。如果您愿意,可以通过Google获得更多信息。

就目前而言,我建议您坚持使用较旧的版本,然后等待下一个版本。

09-30 18:38