我的编程项目不起作用

时间:2014-03-27 19:38:54

标签: java eclipse jframe jpanel

这是Die类,它应该根据主要显示的用户规范绘制模具。

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Die extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int xCoordinate;
    int yCoordinate;
    int num;


    Graphics pen, g;

    private static final int DIE_LENGTH = 60;        //Set's the size
    private static final int DIE_WIDTH = 60;        //of the die
    private static final int DIE_HEIGHT = 60;
    private static final int DOT_LENGTH = 10;
    private static final int DOT_WIDTH = 10;
    private static final Color LINE_COLOR = Color.black;
    private static final Color DIE_COLOR = Color.white;
    private static final Color DOT_COLOR = Color.black;
    private static final int DIE_VERT = 10;
    private static final int DIE_HORI = 10;


    public Die(int dieNum, int x, int y)
    {
        xCoordinate = x;
        yCoordinate = y;

        num = dieNum;
    }

    @Override protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        //Creates the square
        g.drawRect(getX(), getY(), DIE_WIDTH, DIE_HEIGHT);
        g.setColor(LINE_COLOR);
        g.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
        if (num <= 0)
            drawBlank();
        else if (num == 1)
            drawOne();
        else if (num == 2)
            drawTwo();
        else if (num == 3)
            drawThree();
        else if (num == 4)
            drawFour();
        else if (num == 5)
            drawFive();
        else if (num == 6)
            drawSix();



    }

    private void drawBlank ()
    {
        pen.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
        pen.setColor(DIE_COLOR);
    }

    private void drawDot ( int x, int y)
    {
        super.paintComponent(g);
        g.setColor(DOT_COLOR);
        g.fillOval(x, y, DOT_LENGTH, DOT_WIDTH);

    }
    private void drawOne ()
    {
        super.paintComponent(g);
        drawBlank();
        drawDot(xCoordinate, yCoordinate);

    }
    private void drawTwo ()
    {
        super.paintComponent(g);
        drawBlank();
        pen.fillOval((xCoordinate - DIE_HORI), (yCoordinate + DIE_VERT), DOT_LENGTH, DOT_WIDTH);
        pen.fillOval((xCoordinate + DIE_HORI), (yCoordinate - DIE_VERT), DOT_LENGTH, DOT_WIDTH);

    }
    private void drawThree ()
    {

        drawBlank();
        drawOne();
        drawTwo();

    }

    private void drawFour ()
    {
        super.paintComponent(g);
        drawBlank();
        drawDot((xCoordinate - DIE_HORI),(yCoordinate + DIE_VERT));
        drawDot((xCoordinate + DIE_HORI),(yCoordinate + DIE_VERT));
        drawDot((xCoordinate - DIE_HORI),(yCoordinate - DIE_VERT));
        drawDot((xCoordinate + DIE_HORI),(yCoordinate - DIE_VERT));

    }

    private void drawFive ()
    {
        drawBlank();
        drawFour();
        drawOne();


    }


    private void drawSix()
    {
        drawBlank();
        drawFour();
        drawDot((xCoordinate - DIE_HORI), yCoordinate);
        drawDot((xCoordinate + DIE_HORI) , yCoordinate);

    }

}

这是主要的。

import javax.swing.JFrame;
import java.awt.Color;
import java.util.Scanner;

public class RollTheDie
{

    public static void main(String[] args)
    {

        final int WINDOW_HEIGHT = 350;
        final int WINDOW_WIDTH = 300;

        System.out.println("Hi! Let's play dice!\n");

        Scanner kb = new Scanner( System.in );

        System.out.print("Enter the number on the face of the die:");
        int num = kb.nextInt();

        System.out.print("Enter the location of the die:");
        int x = kb.nextInt();
        int y = kb.nextInt();

        System.out.println("I hope you had fun! Bye!");

        JFrame dieWindow = new JFrame();
        dieWindow.setBackground(Color.gray);
        dieWindow.setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
        dieWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dieWindow.setTitle("Roll the Die");

        Die newDie = new Die(num,x,y);
        dieWindow.add(newDie);
        dieWindow.setVisible(true);

        kb.close();
    }
}

当我运行代码时,我得到这个空的异常错误,我似乎无法修复,任何提示?,错误指向我的绘制方法的第一行,取决于用户输入的内容(点数上窗口打开但没有画出来,背景是白色的。以下是我运行代码时在控制台中显示的内容

嗨!让我们玩骰子!

输入骰子面上的数字:5

输入骰子的位置:50

50

我希望你玩得开心!再见!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Die.drawBlank(Die.java:67)--(This Points to the drawBlank() method first line.)
    at Die.drawFive(Die.java:115)--(This points to the drawBlank() in the method drawFive().)
    at Die.paintComponent(Die.java:57)--(This points to my paintComponent(Graphics g) method, the line that has drawFive().)
    at javax.swing.JComponent.paint(Unknown Source)
    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$1100(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)

2 个答案:

答案 0 :(得分:2)

您声明变量pen但从不初始化它。

您的代码中包含:

Graphics pen, g;
// ...
private void drawBlank ()
{
    pen.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
    pen.setColor(DIE_COLOR);
}

pen实际上没有将其初始化或设置为实例。执行pen.fillRect()时,将抛出NullPointerException。

有关NullPointerException的含义的更多信息,请参阅@Sotirios_Delimanolis对您的问题的评论。

答案 1 :(得分:0)

我可能只是失明,但你有没有分配给g或pen?

Graphics g, pen;

编辑:在调用方法之前,你必须先将一个Graphics对象分配给g和pen。