简体   繁体   English

减慢Java中的渲染图形

[英]Slow down rendering graphics in java

When I run the code below the beginning position of x and y and the final position of x and y render. 当我在x和y的起始位置以及x和y的最终位置下方运行代码时,将渲染。 I want each position of x and y to render as the tick method runs and changes the x and y values by the speed. 我希望当tick方法运行并通过速度更改x和y值时呈现x和y的每个位置。 Basically I want to be able to control the speed of an object as it moves. 基本上,我希望能够控制对象移动的速度。

public void render(Graphics g) {
           g.drawImage(JamesTexture.james, x, y, width, height, null);
    }

    public void tick() {

        if (run) {
            if (!stop) {

                    int z = Maze.fx.size() - 1;
                    int i = 1;// speed
                    xx = getPathX(z);
                    yy = getPathY(z);

                    while (z > 0) {

                        if (xx > (getPathX(z - 1))) {
                            xx = xx - i;
                            x = xx;
                            if (xx < (getPathX(z - 1))) {
                                xx = (getPathX(z - 1));
                            }
                        } else if (xx < (getPathX(z - 1))) {
                            xx = xx + i;
                            x = xx;
                            if (xx > (getPathX(z - 1))) {
                                xx = (getPathX(z - 1));
                            }
                        } else if (yy > (getPathY(z - 1))) {
                            yy = yy - i;
                            y = yy;
                            if (yy < (getPathY(z - 1))) {
                                yy = (getPathY(z - 1));
                            }
                        } else if (yy < (getPathY(z - 1))) {
                            yy = yy + i;
                            y = yy;
                            if (yy < (getPathY(z - 1))) {
                                yy = (getPathY(z - 1));
                            }
                        } else {

                            z--;
                            stop = true;                        }
                    }
                }
            }
        }
    }

    int getPathX(int z) {
        List<Integer> fx = Maze.fx;
        z = fx.get(z);
        return z * 32;
    }

    int getPathY(int z) {
        List<Integer> fy = Maze.fy;
        z = fy.get(z);
        return z * 32;
    }

Just a suggestion as I cannot comment yet, but it might be that your while loop finishes the entire maze in one call. 我还不能发表评论,这只是一个建议,但这可能是您的while循环在一个调用中完成了整个迷宫。 I think we'd need more of your code to be sure, but try to add a break statement at the end of each if statement in your while loop like so: 我认为我们需要更多的代码来确定,但是尝试在while循环的每个 if语句的末尾添加一个break语句,如下所示:

if (xx > (getPathX(z - 1))) {
    xx = xx - i;
    x = xx;
    if (xx < (getPathX(z - 1))) {
        xx = (getPathX(z - 1));
    }

    break;
}

Check if that fixes it. 检查是否可以解决。 It is of course dependant on the speed the tick() function is called with. 当然,这取决于调用tick()函数的速度。

我知道它可能已经被杀死了,但是Thread类有一个.sleep方法,该方法以纳秒为参数。

There is no perfect way to handle this, because you can never exactly 'predict' how much time will be spent until the image that you are rendering is actually displayed. 没有完美的方法来处理此问题,因为您永远无法准确地“预测”要实际显示要渲染的图像之前要花费多少时间。 What you can do is measure the time(s) that elapsed between the previous tick(s) and the current tick. 您可以做的是测量从前一个滴答声到当前滴答声之间经过的时间。

delta time = current tick time - last tick time

This gives you the actual delta time that elapsed. 这样可以为您提供实际经过的增量时间 So how do you fetch the tick time? 那么,您如何获取滴答时间呢? I believe in Java the Clock, System and Timer classes provide various functions that each come with different precision and granularity. 我相信Java中的Clock,System和Timer类提供各种功能,每种功能具有不同的精度和粒度。 Typically, some of these functions depend on the hardware clock, some might be interrupted by various software and hardware events, some experience problems when multiple hardware cores are context switching, some might occasionally even run backwards.. It's a zoo and you need to read the docs carefully to understand what you are actually measuring. 通常,其中一些功能取决于硬件时钟,某些功能可能会因各种软件和硬件事件而中断,某些功能在多个硬件内核进行上下文切换时会遇到问题,某些功能有时甚至会倒退。。这是一个动物园,您需要阅读请仔细阅读文档,以了解您实际要测量的内容。 Generally though, millisecond precision is decent enough for most applications. 通常,毫秒精度对于大多数应用程序来说已经足够了。 Eg this gets you the time in seconds (as a double with plenty of precision for millisecond ranges). 例如,这可以使您以秒为单位的时间(精度为毫秒范围的两倍)。

double stime = System.currentTimeMillis();

The speed is generally expressed as distance per time unit such as 速度通常表示为每时间单位的距离,例如

meters per second (m / s)

You can then compute the distance the object needs to move by multiplying the delta time that elapsed with the speed. 然后,您可以通过将经过的增量时间乘以速度来计算对象需要移动的距离。

delta object movement = delta elapsed time * speed

If you want to slow down the movement of your objects, you reduce the speed. 如果要减慢对象的运动,可以降低速度。

If you have a very 'stable' system framerate (and if you use standard double buffering this is mostly the case), you might be ok with a delta between the last 2 ticks, but of course you can use different prediction schemes to try to anticipate slowdowns if you have huge bursts of processing at irregular intervals. 如果您的系统帧率非常“稳定”(并且大多数情况下是使用标准的双缓冲),则在最后两个刻之间可以有一个增量,但是您当然可以使用不同的预测方案来尝试如果您以不规则的时间间隔进行大量处理,则可以预期速度会变慢。 Getting a predictable framerate is why making good games is still hard to do. 要获得可预测的帧速率,就是制作优质游戏仍然很困难的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM