Java 简单侧滚JFrame游戏滞后,其他问题';你帮不上忙?

Java 简单侧滚JFrame游戏滞后,其他问题';你帮不上忙?,java,swing,lag,Java,Swing,Lag,目前,我正试图用Java制作一个简单的视频游戏,只是为了好玩。但似乎有滞后,我不知道为什么会发生这种情况。我会告诉你真相: 它的绘制方式是使用JFrame,实际的绘制发生在ImagePanel类中。在ImagePanel中,我是这样绘制的。它包括一些关于调试以显示FPS的内容,以及显示运行长度的计时器,但我不确定这是否重要。它通过多个ArrayList来显示JFrame上的所有对象 //Painting public void paintComponent(Graphics g) {

目前,我正试图用Java制作一个简单的视频游戏,只是为了好玩。但似乎有滞后,我不知道为什么会发生这种情况。我会告诉你真相: 它的绘制方式是使用JFrame,实际的绘制发生在ImagePanel类中。在ImagePanel中,我是这样绘制的。它包括一些关于调试以显示FPS的内容,以及显示运行长度的计时器,但我不确定这是否重要。它通过多个ArrayList来显示JFrame上的所有对象

//Painting

public void paintComponent(Graphics g) 
    {
        //Paint the background with its upper left corner at the upper left corner of the panel
        g.drawImage(background, 0, 0, null); 
        //Paint each image in the foreground where it should go
        for(MovingImage img : backgrounds)
        {
            g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
        }
        for(MovingImage img : foreground)
        {
            g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
        }

        if(g instanceof Graphics2D)
        {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
            g2.scale(2, 2);
            g2.setColor(Color.WHITE);

            String milSpace;
            if(timer%100 < 10)
                milSpace = "0";
            else
                milSpace = "";

            String secSpace;
            if(timer/100 < 10)
                secSpace = "0";
            else
                secSpace = "";

            g2.drawString(secSpace + timer/100 + ":" + milSpace + timer%100, 10, 20);

            //Debug
            if(debug)
            {
                long currentTime = System.currentTimeMillis();
                if (currentTime > nextSecond)
                {
                    nextSecond += 1000;
                    frameInLastSecond = framesInCurrentSecond;
                    framesInCurrentSecond = 0;
                }
                framesInCurrentSecond++;
                //g2.drawString("LagMS:" + (-frameRate - 10) + " FPS:" + frameInLastSecond, 20, 40);   <-includes "lag"
                g2.drawString("FPS:" + frameInLastSecond, 20, 40);
            }
         }
    }

    //Replaces the list of foreground images with the one given, and repaints the panel
    public void updateImages(ArrayList<MovingImage> newForeground, ArrayList<MovingImage> newBackgrounds)
    {
        foreground = newForeground;
        backgrounds = newBackgrounds;
        //time checking
        long time = System.currentTimeMillis();
        lastTime = time;

        repaint();  //This repaints stuff... you don't need to know how it works
    }
//绘画
公共组件(图形g)
{
//绘制背景,使其左上角位于面板的左上角
g、 drawImage(背景,0,0,空);
//在前景中画出每个图像应该去的地方
用于(移动图像img:背景)
{
g、 drawImage(img.getImage(),(int)(img.getX(),(int)(img.getY()),null);
}
用于(移动图像img:前景)
{
g、 drawImage(img.getImage(),(int)(img.getX(),(int)(img.getY()),null);
}
if(图形的g实例2D)
{
图形2d g2=(图形2d)g;
g2.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.量表(2,2);
g2.设置颜色(颜色为白色);
字符串空间;
如果(计时器%100<10)
milSpace=“0”;
其他的
milSpace=“”;
字符串secSpace;
如果(计时器/100<10)
secSpace=“0”;
其他的
secSpace=“”;
g2.抽绳(secSpace+定时器/100+”:“+milSpace+定时器%100,10,20);
//调试
如果(调试)
{
长currentTime=System.currentTimeMillis();
如果(当前时间>下一秒)
{
下一秒+=1000;
frameInLastSecond=framesInCurrentSecond;
framesInCurrentSecond=0;
}
framesInCurrentSecond++;

//g2.drawString(“LagMS:+(-frameRate-10)+”FPS:“+frameInLastSecond,20,40);您是否愿意将此帖子移动到?我看到了其他类似于此的问题,但这似乎是一个好主意。请参见此和此。
g.drawImage(background,0,0,null);
应该是
g.drawImage(background,0,0,this)
哇,这很管用!谢谢安德鲁,这大大加快了速度!我希望我能给你选择答案,但这只是一个评论。
public void tick()
    {
        long lastTime = System.currentTimeMillis(); 
        int place = 0;
        boolean go = true;
        while(go)
        {
            long time = System.currentTimeMillis(); //The current time
            if(time - 10 > lastTime)    //Update every .01 seconds, or 1 TICK (if time - 10 > lastTime)
            {
                lastTime = time;
                //Reset the last time
                place++;

                imagePanel.incTime();

                for(MovingImage object : movingObjects)
                {
                    if(object instanceof Building)
                    {
                        object.incrementPosition();     //Augment position by velocity
                        if(place%500 == 0)//If 5 seconds have passed...
                        {
                            ((Building) object).speedUp();//make it go a little faster!
                        }
                    }
                    if(object instanceof Player)
                    {
                        if(jumpKeyOn)
                            ((Player) object).jump();//Initiate jump class assuming key is pressed
                        object.incrementPosition();
                        ((Player) object).constrainPlayerToObjects(movingObjects, yMax);
                        if(object.getY()>yMax + 1000)
                        {
                            go = false;
                        }
                    }
                }
                //Repaint all the things in their new positions, possibly faster
                for(MovingImage bg : backgrounds)
                {
                    bg.incrementPosition();
                    if(place%500 == 0)
                        bg.setVelocity(bg.getXvel() - 0.1, 0);
                }

                /*
                 * Acceleration
                 */
                //Removes buildings once left screen
                int i = 0;
                while(i < movingObjects.size())
                {
                    if(movingObjects.get(i) instanceof Building)
                    {
                        if(movingObjects.get(i).getX() + movingObjects.get(i).getImage().getWidth(null) < 0)
                            movingObjects.remove(i);
                        else
                            i++;
                    }
                    else
                        i++;
                }

                imagePanel.updateImages(movingObjects, backgrounds);
            }
        }
        gameOver();
    }