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);
        }
    }

        }

これが基本的なことです。4 ゲームはピンポンで、このクラスには pa という名前のパドルが 1 つあります。pa パドルは機能しますが、同じものを別のクラスにコピーしましたが、すべてのメソッドの名前を変更しました。プログラムを実行すると、pa paddle は機能しますが、enemypaddle(ea) でメソッドが null ポインター例外であるというエラーが表示されます。スタック トレースは次のとおりです。

    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)

パドルが 1 つしか機能しない理由を誰か教えてください。それは同じことです...

4

1 に答える 1

2
  1. 初期化された場所がどこにもありませんea...
  2. 私はオーバーライドを避け、代わりにpaint使用paintComponentします。カスタム ペインティングの実行AWT および Swing でのペインティングを確認してください。

これは、デバッガーを使い始める良い機会かもしれません ;)

于 2013-08-03T01:47:18.367 に答える