这段代码如何导致几秒暂停?

时间:2013-06-21 19:51:30

标签: java debugging awt lag

在您阅读本文之前,请了解我知道编写此代码有更好的方法。这段代码很久以前就开始了,我比以前更了解Java。  尽管如此,我最近查看了代码并希望找出导致问题的原因,以便将来知道。


更新: 我已经下载了一个JProfiler实验。事实证明,当我绘制字符串时,第158行的内存分配有一个巨大的峰值。但是,我不知道它究竟意味着什么,或者它为什么会造成巨大的滞后峰值。

    public void draw(Graphics g) {
    if (isVisible) {
        for (int i = 0; i < debugList.length - 1; i += 2) {
            g.setColor(color);
            g.setFont(font);
            try {
                g.drawString(debugList[i] + ":", xPos, yPos + (i * 15)); // Line 158
                g.drawString(debugList[i + 1], xPos, yPos + 10 + (i * 15));
            }
            catch (NullPointerException e) {
                if (!errorDisplayed) {
                    System.out
                            .println("There was a problem while displaying the debug variables. Check to make sure you added all of the variables you declared in the debug constructor");
                    errorDisplayed = true;
                }
            }
        }
    }
}

前段时间我正在制作一个2D平台游戏并且不得不修复错误。我认为错误修复的一个很酷的方法是让一个调试类在屏幕上显示变量。事实证明,它很棒!它显示屏幕上的所有变量没有问题!

但是,当我按下一个键后,当我调用changeVisible() (在代码的最底部)方法时,它会冻结我的整个线程大约3秒钟。

它并不是那么重要,因为它已经与我制作的其他调试类已经过时了,但我仍然想知道是什么导致了未来项目的峰值。

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class Debug {
    private String[] debugList;
    private boolean errorDisplayed = false, isVisible = true;
    private Color color;
    private int xPos = 0, yPos = 0, currentIndex = 0;
    private Font font;
    /**
     * This constructor will create an array that can fit (numberOfVars)
     * variables
     * 
     * @param numberOfVars
     *            - The amount of variables you wish to display
     * @param setVisible
     *            - Will toggle visibility on instantiation (true):on
     *            (false):off
     */
    public Debug(int numberOfVars, boolean setVisible) {
        debugList = new String[numberOfVars * 2];
        if (setVisible)
            isVisible = true;
        else
            isVisible = false;
        color = new Color(0, 0, 0);
        xPos = 20;
        yPos = 30;
        font = new Font("SanSerif",Font.BOLD,12);
    }

    /**
     * This constructor will create an array that can fit (numberOfVars)
     * variables
     * 
     * @param numberOfVars
     *            - The amount of variables you wish to display
     * @param setVisible
     *            - Will toggle visibility on instantiation (true):on
     *            (false):off
     * @param inputXPos
     *            - The location of the text on the x axis
     * @param inputYPos
     *            - The location of the text on the y axis
     */
    public Debug(int numberOfVars, boolean setVisible, int inputXPos,
            int inputYPos) {
        debugList = new String[numberOfVars * 2];
        if (setVisible)
            isVisible = true;
        else
            isVisible = false;
        color = new Color(0, 0, 0);
        xPos = inputXPos;
        yPos = inputYPos;
    }

    /**
     * This method will initialize the variable you wish to display
     * 
     * @param objectName
     *            - the display name of the variable
     * @param arg
     *            - the value of the variable you wish to display
     */
    public void addDebug(String objectName, Object arg) {
        if (currentIndex + 2 <= debugList.length) {
            debugList[currentIndex] = objectName;
            debugList[currentIndex + 1] = arg.toString();
            currentIndex += 2;
        }
    }

    /**
     * This method will change the text color
     * 
     * @param newColor
     *            - the color of the debug text
     */

    public void changeColor(Color newColor) {
        color = newColor;
    }
    /**
     * This method will change the text font
     * @param font
     *          - the font of the debug text
     */
    public void changeFont(Font font) {
        this.font = font;
    }
    /**
     * This method will update the variable value
     * 
     * @param objectName
     *            - the name of the variable you wish to display (must be the
     *            same name used in addDebug())
     * @param arg
     *            - the value of the variable you wish to display
     */
    public void update(String objectName, Object arg) {
        for (int i = 0; i < debugList.length; i += 2) {
            if (debugList[i] != null && debugList[i].equals(objectName)) {
                if (i < debugList.length + 1)
                    debugList[i + 1] = arg.toString();
            }
        }
    }

    /**
     * This method will draw the debug information
     * 
     * @param g
     *            - The graphics you wish to paint on
     * @param lineSpacing
     *            - The display space between the variables
     */
    public void draw(Graphics g, int lineSpacing) {
        if (isVisible) {
            if (currentIndex == debugList.length) {
                for (int i = 0; i < debugList.length - 1; i += 2) {
                    g.setColor(color);
                    g.setFont(font);
                    try {
                        g.drawString(debugList[i] + ":", xPos, yPos
                                + (i * lineSpacing));
                        g.drawString(debugList[i + 1], xPos, yPos + 10
                                + (i * lineSpacing));
                    }
                    catch (NullPointerException e) {
                        if (!errorDisplayed) {
                            System.out
                                    .println("There was a problem while displaying the debug variables. Check to make sure you added all of the variables you declared in the debug constructor");
                            errorDisplayed = true;
                        }
                    }
                }
            }
        }

    }

    /**
     * This method will draw the debug information
     * 
     * @param g
     *            - The graphics you wish to paint on
     */
    public void draw(Graphics g) {
        if (isVisible) {
            for (int i = 0; i < debugList.length - 1; i += 2) {
                g.setColor(color);
                g.setFont(font);
                try {
                    g.drawString(debugList[i] + ":", xPos, yPos + (i * 15));
                    g.drawString(debugList[i + 1], xPos, yPos + 10 + (i * 15));
                }
                catch (NullPointerException e) {
                    if (!errorDisplayed) {
                        System.out
                                .println("There was a problem while displaying the debug variables. Check to make sure you added all of the variables you declared in the debug constructor");
                        errorDisplayed = true;
                    }
                }
            }
        }
    }

    /**
     * Will toggle the visibility of the debug display
     */
    public void changeVisible() {
        isVisible = !isVisible;

    }
}

主类(用Debug评论的行很重要):

package com.bustedearlobes.platformergame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.*;
import javax.swing.JFrame;
public class Game extends Applet implements Runnable {
    private static final long serialVersionUID = 1L;

    public static int pixelSize = 3;

    public static double sx = 0, sy = 0, dir = 0;

    public static Dimension windowSize = new Dimension(700,560), pixels = new Dimension(windowSize.width/pixelSize,windowSize.height/pixelSize);

    public static Point mousePos = new Point(0,0);

    public static boolean isRunning = false,
            isMoving = false,
            isJumping = false,
            isMouseLeft = false,
            isMouseRight = false;

    public static String name = "2D Game";

    public static Level level;
    public static Character character;
    public static Inventory inventory;
    public static Sky sky;
    public static ArrayList<Mob> mobs = new ArrayList<Mob>();
    public static ArrayList<BrokenBlocks> brokenBlocks = new ArrayList<BrokenBlocks>();
    public static Spawner spawner;
    public static Sound sound;
    public static GameFile gameFile;
    public static JFrame frame;
    public static Tile tile;
    public final static double GRAVITYCONSTANT = 1;

    private Image screen;

    public static Debug debug = new Debug(5,false); //Debug

    public Game() {
        setPreferredSize(windowSize);

        addKeyListener(new Listening()); //Debug this is where the key listener is that calls the changeVisible() method
        addMouseListener(new Listening());
        addMouseMotionListener(new Listening());
        addMouseWheelListener(new Listening());
    }

    public void start() { // Defining all the objects required
        requestFocus();

        tile = new Tile(); // Loading Images...
        gameFile = new GameFile();
        level = new Level();
        character = new Character(Tile.tileSize, Tile.tileSize * 2);
        isRunning = true;
        sound = new Sound();
        inventory = new Inventory();
        sky = new Sky();
        spawner = new Spawner(10);
        debug.changeFont(new Font("San Serif",Font.PLAIN,10)); // Debug
        debug.addDebug("Player", character); // Debug
        debug.addDebug("Side X", sx); // Debug
        debug.addDebug("Side Y", sy); // Debug
        debug.addDebug("Sky", sky); // Debug
        debug.addDebug("Level.animation", level.animation); // Debug
        new Thread(this).start();
    }

    public void stop() {
        isRunning = false;
    }

    public void tick() {
        character.tick();
        level.tick(pixels.width / Tile.tileSize + 2,pixels.height / Tile.tileSize + 2);
        sky.tick();
        inventory.tick();
        for (int i = 0; i < mobs.toArray().length;i++)
            mobs.get(i).tick();
        for (int i = 0; i < brokenBlocks.toArray().length;i++)
            brokenBlocks.get(i).tick();

        debug.update("Player", character); // Debug
        debug.update("Side X", sx); // Debug
        debug.update("Side Y", sy); // Debug
        debug.update("Sky", sky); // Debug
        debug.update("Level.animation", level.animation); // Debug
    }

    public void render() {
        Graphics g = screen.getGraphics();

        g.setColor(new Color(100,100,255));
        g.fillRect(0,0,pixels.width, pixels.height);

        sky.render(g);
        level.render(g,pixels.width / Tile.tileSize + 2,pixels.height / Tile.tileSize + 2);
        for (int i = 0; i < brokenBlocks.toArray().length;i++)
            brokenBlocks.get(i).render(g);
        for (int i = 0; i < mobs.toArray().length;i++)
            mobs.get(i).render(g);
        character.render(g);
        inventory.render(g);
        debug.draw(g); // Debug
        g = getGraphics();
        g.drawImage(screen,0,0,windowSize.width,windowSize.height,0,0,pixels.width,pixels.height,null);
        g.dispose();
    }
    public static void main(String[] args) {
        Game game = new Game();

        frame = new JFrame();
        frame.add(game);
        frame.setTitle(name);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);

        game.start();
    }

    public void run() {
        screen = createVolatileImage(pixels.width, pixels.height);
        while(isRunning) {

            tick();
            render();

            try {
                Thread.sleep(5);
            } catch (Exception e) {
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

我无法立即看到问题所在,除了debugList是列表而不是HashMap这一事实。 (不确定这有多大的影响。)

但是,我可以告诉你如何找到自己。查看一个名为JProfiler的工具。它允许您在代码中查看逐行执行时间,这将让您了解瓶颈所在的位置。说明here