Java awt repaint()没有调用update(图形g)

时间:2015-08-11 03:58:08

标签: java graphics awt

我一直在关注" Kilobolt游戏开发"在这里找到:http://www.kilobolt.com/unit-2-creating-a-game-i.html

我目前遇到的问题是我有一个update()方法,根据教程和java的文档应该在调用repaint()时调用,但它不是,这是框架根本不渲染任何东西。

以下是相关代码:

 @Override
public void run() {
    // TODO Auto-generated method stub
    while(true){
        ball.update();
        playerpaddle.update();
        computerpaddle.update();

        repaint();
        //System.out.println("s!");
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Error " + e.getMessage());
        }
    }

}
 @Override
public void update(Graphics g) {
    // TODO Auto-generated method stub
    System.out.println("update");
    if(background == null){
        background = createImage(this.getWidth(), this.getHeight());
        second = background.getGraphics();
    }
    second.setColor(getBackground());
    second.fillRect(0, 0, getWidth(), getHeight());
    second.setColor(getForeground());
    paint(second);

    g.drawImage(background, 0, 0, this);
}
@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    System.out.println("paint");
    g.drawImage(iball, ball.getXPos(), ball.getYPos(), this);
    g.drawImage(iplayerpaddle, playerpaddle.getXPos(), playerpaddle.getYPos(), this);
    g.drawImage(icomputerpaddle, computerpaddle.getXPos(), computerpaddle.getYPos(), this);
}

编辑: 我现在正处于第四天。根据那个教程,除了我必须改变的框架代码之外,一切都是一样的,因为keylistener没有正常工作。

 Frame game = (Frame)this.getParent().getParent();
    game.setSize(Constants.MAX_WIDTH, Constants.MAX_HEIGHT);
    game.setBackground(Color.BLACK);
    game.setFocusable(true);
    game.addKeyListener(this);
    game.setTitle("Pong Clone");

我还尝试直接使用update(this.getGraphics())调用update()但是我仍然只得到一个永远不会改变的白框,即使在调用update()和paint()之后也是如此。 / p>

EDIT2: 我已经解决了我误读线程功能的背景问题,并且我改变了我的#34; gameloop.run()" to" gameloop.start()",这允许我渲染黑色背景,但是我的球和桨仍然不会渲染。

0 个答案:

没有答案