Error with timer and JFrame

被刻印的时光 ゝ 提交于 2019-12-02 23:23:04

问题


I'm making a game with a timer and a JFrame (and many other things but only these 2 are causing problems), and after running the segments below, I got a weird error. At least for me who has never used these classes prior to this.

Start executing this

private static GameView window;
private static Timer time;
public static void main(String args[])
{
    window = new GameView(800,600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    time = new Timer();
    time.schedule( new TimerTask(){
        public void run(){GameState.update(); 
        window.paintComponents(null);}
        },0, 40);

}

which calls this:

public void paintComponents (Graphics g)
{

    for(Bullet j : GameState.getEnBullets()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Enemy j : GameState.getEnemies()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Bullet j : GameState.getPlayBullets()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    this.paint(g);
}

And here's the error:

Exception in thread "Timer-0" java.lang.NullPointerException
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at Game.GameView.paintComponents(GameView.java:59)
    at Game.GameController$1.run(GameController.java:39)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

I also get a blank JFrame window (GameView extends JFrame).


回答1:


You get NPE because you are passing null as graphics in window.paintComponents(null); And then you are calling g.drawImage(j.getImage(),j.getX(), j.getY(), null); where g is null.




回答2:


You shouldn't be painting direclty in a JFrame at all but rather a JPanel or JComponent that is held by the JFrame. You should override the JPanel's paintComponent method as noted above (not the JFrame since it doesn't even have this method) and paint in there. Another thing, don't use a java.util.Timer but rather a javax.swing.Timer better known as a Swing Timer since this is a Swing application. Also you shouldn't call paint/paintComponent directly but rather have your GUI update class fields then call repaint() on the JPanel that you're doing your drawing on and then paintComponent will (usually) be called by the JVM. There are many examples of Swing animation here in this forum and I suggest you search out these examples and learn from them as I think that they can help you.

Edit: heck, you've already been told all of this in your previous threads. Why should we help you if you ignore our advice?




回答3:


The method to override is paintComponent() not "paintComponents" (with an s).

You should never invoke the paintComponent() method directly. Instead you invoke the repaint() method on the component.



来源:https://stackoverflow.com/questions/6263409/error-with-timer-and-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!