在update()void中添加if语句时不显示图形

时间:2013-08-03 01:51:01

标签: java swing graphics jpanel keylistener

我正在努力用一个简单的2D游戏引擎创建我的第一个游戏。无论如何,每次我添加if(input.KEY_RIGHT)x ++;时,画布都不会加载。它只显示JPanel的背景。但是当我删除if(input.KEY_RIGHT)x ++;时,它会起作用。让我知道它为什么不起作用。

package net.james222.game;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;


/**
 * Main class for the game
 */
public class Game extends JFrame
{       

public InputHandler input;

private static final long serialVersionUID = 1L;
    boolean isRunning = true;
    int fps = 30;
    int windowWidth = 500;
    int windowHeight = 500;

    BufferedImage backBuffer;
    Insets insets;

    int x = 0;

    public static void main(String[] args)
    {
            Game game = new Game();
            game.run();
            System.exit(0);
    }

    /**
     * This method starts the game and runs it in a loop
     */
    public void run()
    {
            initialize();

            while(isRunning)
            {
                    long time = System.currentTimeMillis();

                    update();
                    draw();

                    //  delay for each frame  -   time it took for one frame
                    time = (1000 / fps) - (System.currentTimeMillis() - time);

                    if (time > 0)
                    {
                            try
                            {
                                    Thread.sleep(time);
                            }
                            catch(Exception e){}
                    }
            }

            setVisible(false);
    }

    /**
     * This method will set up everything need for the game to run
     */
    void initialize()
    {
            setTitle("Game Tutorial");
            setSize(windowWidth, windowHeight);
            setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);

            insets = getInsets();
            setSize(insets.left + windowWidth + insets.right,
                            insets.top + windowHeight + insets.bottom);

            backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
            }

    /**
     * This method will check for input, move things
     * around and check for win conditions, etc
     */
    void update()
    {
       if(input.KEY_RIGHT) x++;
    }

    /**
     * This method will draw everything
     */
    void draw()
    {              
            Graphics g = getGraphics();

            Graphics bbg = backBuffer.getGraphics();

            bbg.setColor(Color.WHITE);
            bbg.fillRect(0, 0, windowWidth, windowHeight);

            bbg.setColor(Color.BLACK);
            bbg.drawOval(x, 10, 20, 20);

            g.drawImage(backBuffer, insets.left, insets.top, this);
    }
} 

1 个答案:

答案 0 :(得分:2)

绘画是Swing通常使用从paintComponent扩展的组件的JComponent方法(通常为JPanel

你永远不应该使用getGraphics。这只是在最后一个绘制周期之后组件状态的快照,如果组件尚未开始绘制,则可能返回null。它的内容也将在下一个油漆循环中被覆盖。

首先看看

我还鼓励您使用key bindings API而非KeyListener(并非我可以看到您实际上使用任何类型的输入事件处理程序)...

我还建议您在违反Swing的单线程模型时查看Concurrency in SwingInitial Threads

Java / Swing是一个复杂的API /框架,对它的使用有非常具体的要求。虽然它非常灵活,但它确实需要您了解它的工作原理,以便您可以充分利用它。

当你掌握这些概念时,我会抛开你的游戏愿望,因为它会让你的生活变得简单百万倍