2

私は Java の初心者で、カーソルが置かれている場所に四角形を描画するアプリケーションを作成しようとしています。mouseMoved(MouseEvent) methodすでにすべてを実行しましたが、に を再描画させることができませんJPanel。再描画がなければ、長方形は一度だけ描画され、それだけです。再描画すると問題なくコンパイルされますが、実行すると、マウスを動かすたびにこの大きな " Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" エラーが発生します。

それで、誰かこれについて私を助けてもらえますか?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Game extends JPanel implements MouseMotionListener 
{
    public static void main(String[] args) {
        new Game().game();
    }
    JPanel panel;
    JButton button2;
    JButton button;
    public void game() {
        JPanel panel = new Game();
        button = new JButton("Ok");
        panel.setLayout(new FlowLayout());
        panel.add(button);

        button2 = new JButton("Cancel");

        JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setResizable(false);
        frame.add(panel);

        frame.setVisible(true); 
        panel.addMouseMotionListener(this);
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        int x = (int) b.getX();
        int y = (int) b.getY();
        g.fillRect(x,y,100,100);        
    }

    public void mouseMoved(MouseEvent evt) {
        panel.repaint; //This is the line of code that I need help with. Thanks!
    }
    public void mouseDragged(MouseEvent evt) {}
}
4

2 に答える 2

4

うまくいけば、コード例のコメントが、コードで何が間違っているかを伝えることができます:-)、そうでなければ、疑問を呈する理由が常にあります...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Game extends JPanel {
    /*
     * Now x and y are instance variables,
     * whose values you can change at each
     * MouseMove Event, and call repaint()
     * to see the effects
     */

    private int x;
    private int y;
    private MouseAdapter mouseActions =
        new MouseAdapter() {
        @Override
        public void mouseMoved(MouseEvent me) {
            /*
             * Now as the Mouse moves, we simply
             * updating the instance variables,
             * i.e. x and y to the new values
             * of the Mouse Location and calling
             * repaint() to draw the rectangle.
             * Since this class (Game) extends JPanel,
             * hence all the functions of the JPanel
             * belongs to this class, hence like
             * as we call any other method of this
             * class, without using the object,
             * we can call repaint, likewise.
             */
            x = me.getX();
            y = me.getY();
            repaint();
        }
    };

    /*
     * This JPanel panel is unnecessary in 
     * this case, since the class itself 
     * extends JPanel, hence you can use
     * this (keyword) to access the instance
     */
    //JPanel panel;
    // Not needed for this case.
    //JButton button2;
    //JButton button;
    public void game() {

        JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        addMouseMotionListener(mouseActions);
        /*
         * Here this means the instance
         * of the current class
         */
        frame.add(this);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    /*
     * While overriding methods of the 
     * super class, try to keep the 
     * ACCESS SPECIFIER, as close to
     * the original thingy as possible
     * In this case, it's protected
     * and not public
     */
    @Override
    protected void paintComponent(Graphics g) {

        /*
         * Do not perform calculation in this method
         * atleast.
         */
        super.paintComponent(g);
        g.fillRect(x, y, 100, 100);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new Game().game();
            }
        };
        EventQueue.invokeLater(runnable);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }
}
于 2013-07-10T14:31:58.970 に答える
4

これを変える :

public void game() {
JPanel panel = new Game();

これに:

public void game() {
panel = new Game();

最初のケースでは、ローカル変数を作成しているだけです。これを修正するには、クラス変数をインスタンス化する必要があります。

于 2013-07-10T13:56:55.163 に答える