gpt4 book ai didi

java - 我的编程项目无法运行

转载 作者:行者123 更新时间:2023-12-02 06:00:44 25 4
gpt4 key购买 nike

这是 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)

最佳答案

您声明了变量pen,但从未初始化它。

您的代码中有:

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

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

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

关于java - 我的编程项目无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22697567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com