Java - 双缓冲NullPointerException

发布于 2025-01-02 22:52:48 字数 1115 浏览 0 评论 0原文

我正在写一个简单的游戏。我有 3 个类,第一个是球,它负责处理与之相关的所有事情,第二个是由一系列“球”组成的游戏,最后一个是窗口,其中包含主线程。

window.paint调用game.draw来接收游戏场景的图形。而游戏本身对其进行双缓冲,以便Image对象可以移动到玩家的球位置(尚未实现)。

所以我的问题是因为我正在创建一个 Image 对象但不知何故它初始化为 null,因此我得到 NullPointerException。

这是处理绘画的方法的来源:

public class MyWindow extends JFrame {
        //...the other code
        public void paint(Graphics g){
            thegame.draw();
            repaint();
        }
}

public class Game extends JFrame implements Runnable {
    ball[] cellmap;

    //...the other code

    public void draw(){
        Image GameImage = createImage(800,800);
        Graphics GameGraphics = GameImage.getGraphics();

        for(int i = 0;i<cellmap.length;i++)
            cellmap[i].draw(GameGraphics);

        g.drawImage(GameImage, 0, 0, this); 
    }
}

public class Ball extends JFrame {
        //...the other code
    public void draw(Graphics g){
        g.setColor(Color.red);
        g.fillOval((int)(this.x+this.radious),(int)(this.y+this.radious),
                       (int)this.radious,(int)this.radious);        
    }
}

I'm writing a simple game. I have 3 classes the first one: ball which take care of every thing refereed to it, the second one game which made out of an array of "ball"s and the final one is windows, the one which contains the MAIN thread.

window.paint calls game.draw in order to receive the graphics of the game scene.While the game itself double buffering it in order that the Image object can be moved to the Player's ball location(yet to be implemented).

So my problem caused because I'm creating an Image object but somewhy this initialized to null, thus I get NullPointerException.

Here is the source of the methods which handle the painting:

public class MyWindow extends JFrame {
        //...the other code
        public void paint(Graphics g){
            thegame.draw();
            repaint();
        }
}

public class Game extends JFrame implements Runnable {
    ball[] cellmap;

    //...the other code

    public void draw(){
        Image GameImage = createImage(800,800);
        Graphics GameGraphics = GameImage.getGraphics();

        for(int i = 0;i<cellmap.length;i++)
            cellmap[i].draw(GameGraphics);

        g.drawImage(GameImage, 0, 0, this); 
    }
}

public class Ball extends JFrame {
        //...the other code
    public void draw(Graphics g){
        g.setColor(Color.red);
        g.fillOval((int)(this.x+this.radious),(int)(this.y+this.radious),
                       (int)this.radious,(int)this.radious);        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2025-01-09 22:52:48

1) 请阅读 Java 命名约定

2) 直接绘制不是个好主意JFrame,将您的绘画放入 JComponentJLabelJPanel

3) for Swing 使用方法 paintComponent,请不要使用方法 paint(Graphics g)绘制(图形g)

4) 如果你想延迟或动画你的绘画使用 javax.swing.Timer

1) please read Java Naming Conventions

2) not good idea paint directly to the JFrame, put your painting to the JComponent, JLabel, JPanel

3) for Painting in Swing use method paintComponent, please not methods paint(Graphics g) or draw(Graphics g)

4) if you want to delay or animate you painting use javax.swing.Timer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文