0

这是我的主要代码:

package main;
  import java.awt.*;
  import java.awt.event.*;

  import javax.swing.*;






    public class Panel extends JPanel{
public Thread update;
public YourPaddle pa;
public EnemyPaddle ea;
public static int fps;
public static long lasttimechecked = System.nanoTime();
public static int frames = 0;
public Panel(){
    addKeyListener(new TAdapter());
    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    pa = new YourPaddle();
    update = new Thread(){
        public void run(){
            for (int i = 0; i < 2; ) {
                pa.gor();
                try{
                ea.gorg();
                }catch(Exception e){

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

                            }
                frames++;
                if (System.nanoTime() - lasttimechecked >= 1000000000){
                    fps = frames;
                    frames = 0;
                    lasttimechecked = System.nanoTime();
                }

            }

        }
    };

    update.start();
}

public void paint(Graphics g){      
    super.paint(g);
    g.setColor(Color.white);
    g.drawLine(300, 10, 300, 340);      
    g.drawImage(pa.i, 20, pa.getY(), this);

    g.drawImage(ea.i, 550, ea.getYy(), this);

    g.drawString("FPS: " + fps + "   Y: " + pa.getY(), 10,17);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

 private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            pa.keyReleased(e);
            ea.keyReleased(e);
       }

       public void keyPressed(KeyEvent e) {
            pa.keyPressed(e);
            ea.keyReleased(e);
        }
    }

        }

事情基本上是这样的,比赛是乒乓球,这个班里有一个叫pa的桨。桨叶有效,但我将相同的东西复制到另一个类中,但重命名了所有方法。当我运行程序时,pa paddle 可以工作,但是enemypaddle(ea) 给我一个错误说该方法是一个空指针异常。这是堆栈跟踪:

    Exception in thread "Thread-2" java.lang.NullPointerException
at main.Panel$1.run(Panel.java:31)
     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Panel.paint(Panel.java:61)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

谁能说为什么只有一个桨不起作用?这是同一件事...

4

1 回答 1

2
  1. 我没有看到任何ea开始初始化的地方......
  2. 我会避免覆盖paintpaintComponent改为使用。查看在 AWT 和 Swing 中执行自定义绘画和绘画。

这可能是开始使用调试器的好时机;)

于 2013-08-03T01:47:18.367 回答