TicTacToe运行时错误

时间:2016-02-13 15:15:27

标签: java tic-tac-toe

我目前正在尝试用Java制作TicTacToe游戏;但是我遇到了一个我无法理解的障碍。我有一个功能,当用户点击左上角时,它应该绘制和X或O,取决于转弯 - 此刻我只有它,所以它应该绘制O.我' m一旦我确定了最初的部分,我将继续实施其余部分。但是,当我点击画椭圆形时,我得到一个我不明白的错误。我的代码如下,错误将低于该值。

P.S。我只是高中的一名大四学生,所以我想我知道我在做什么,但我可能不知道。

主要课程:

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


public class ticTacToeMain{

    public static void main(String[]args){
            ticTacToeBoard theBoard = new ticTacToeBoard();
            theBoard.setBackground(Color.white);
            JFrame frame = new JFrame();
            frame.add(theBoard);
            frame.setTitle("Tic Tac Toe");  
            frame.setSize(600,600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);     
    }
}

董事会成员:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ticTacToeBoard extends JPanel{
    int turn=1;
    protected Graphics g2,g3;
    private static int x,y;

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawLine(200, 0, 200, 600);
        g.drawLine(400, 0, 400, 600);
        g.drawLine(0,200, 600, 200);
        g.drawLine(0, 400, 600, 400);
        addMouseListener(new mouseListener());
    }
    public void drawX(int x1,int y1,int x2,int y2){
        g2.setColor(Color.red);
        g2.drawLine(x1, y1, x2, y2);

    }
    public void drawO(int x1, int y1, int width, int height){
        g3.setColor(Color.blue);
        g3.drawOval(x1,y1, width, height);
    }
    private class mouseListener extends MouseAdapter{
        public void mouseClicked(MouseEvent e){
            x = e.getX();
            y = e.getY();
            topLeftClicked();
        }
    }
    public void topLeftClicked(){
        if (x<200 && y<200 && turn == 0){
            drawX(25,25,175,175);
        }else if (x<200 && y<200 && turn == 1){
            drawO(25,25,150,150);
        }

    }

}

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ticTacToeBoard.drawO(ticTacToeBoard.java:34)
    at ticTacToeBoard.topLeftClicked(ticTacToeBoard.java:48)
    at ticTacToeBoard$mouseListener.mouseClicked(ticTacToeBoard.java:41)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:269)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:269)
    at java.awt.Component.processMouseEvent(Component.java:6528)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

当需要绘制时,swingComponent方法被swing框架调用,你不应该在该方法中添加一个鼠标监听器,否则每次重新绘制组件时都会反复添加一个新的....

关于你的错误,它来自于从未初始化的g3实例变量(因此它为null并引发npe)。如果要在topLeftClicked方法中绘制o或x并调用repaint(),则可以设置标记indcating ...然后使用paintComponent中的标志值来绘制右符号.....这将阻止您存储不必要的图形实例变量......