有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

EclipseJava在MacBook和OSX上的性能非常差,而在Windows下在同一个单元上的性能就不那么差了,为什么?

我正在学习Java并开始我的第一课/项目。在这个项目中,导师的像素在屏幕上移动,并在几秒钟内完成它在屏幕上的移动。我的需要几分钟,每5秒只移动1个像素。我原以为MacBook Pro会更好地处理Java图形。为了确定这是我的代码还是单元,我在Windows中启动时从头开始重新构建了项目,性能得到了显著提高。在那里,光标每秒移动1-2个像素;仍然不如讲师,但比OSX处理得更好。我想知道这是正常的还是预期的?OSX处理Java图形的时间真的很糟糕吗?如果有助于回答这个问题,我已经链接了我的代码,以及OSX中移动缓慢的像素和极低帧速率的视频。Fraps显示我在Windows中的平均速度为650 fps,OSX端的代码输出显示速度大约为40-60 fps,这取决于我是否有其他视频处理。在视频中,它大约是45帧,但这是因为屏幕截图使它从平均每秒60帧变慢

OSX中的帧速率示例:https://www.youtube.com/watch?v=PQ0-L4slgP4

屏幕类的代码:http://pastebin.com/3ETKsY8r

游戏类代码:http://pastebin.com/NDreVB60

我在苹果方面使用10.7.5版本下的EclipseJuno,在训练营方面使用Windows7。MacBook拥有4GB内存和2.53 Ghz Intel Core 2 Duo


共 (1) 个答案

  1. # 1 楼答案

    我在我的MacBookPro上运行了你的代码示例,结果比你发布的视频要好得多

    现在,我不是Java 2D图形方面的超级专家,但有一个观察结果是,由于每次迭代都需要一遍又一遍地重新绘制整个画布,以便像素移动,因此渲染过程中涉及的逻辑应该是快速的。此外,由于像素沿对角线移动,因此右侧较大的区域对于您的示例没有任何用处,因此我建议将JFrame设置为正方形,这样可以减少重新绘制的区域

    最后,我对类屏幕的代码做了一些更改,可以让您更快地完成任务

    package com.oblivion.rain.graphics;
    
    public class Screen {
        // Height was removed since there is no use for it
        private int width;
        public int[] pixels;
    
        int time = 0;
        int counter = 0;
    
        // We need this in order to avoid the iteration in the clear method.
        int previousPixel = 0;
    
        public Screen(int width, int height) {
            this.width = width;
            pixels = new int[width * height]; // 50,400
        }
    
        public void clear() {
            // In case the frame is a rectangle, the previousPixel
            // could hold a value that is greater than the array size
            // resulting in ArrayOutOfBoundsException
            if (previousPixel < pixels.length) {
                pixels[previousPixel] = 0;
            }
        }
    
        public void render() {
            counter++;
    
            if (counter % 100 == 0) {
                time++;
            }
    
            // Calculate the previousPixel index for use in the clear method
            previousPixel = time + (time * width);
    
    
            // Make sure we didn't exceed the array length, then set the 
            // array data at the calculated index
            if (previousPixel < pixels.length) {
                pixels[previousPixel] = 0xff00ff;
            }
        }
    }