【问题标题】:This code is not drawing the die that its supposed too..any tips? (Java, see both classes)这段代码没有画出它应该画的骰子……有什么提示吗? (Java,查看这两个类)
【发布时间】:2014-03-25 02:37:34
【问题描述】:

此代码应该根据从用户那里收集的输入、骰子面上的点数以及骰子位置来创建骰子。

这是主类 导入 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);


    }
}

这是模具类 导入 java.awt.Color; 导入 java.awt.Graphics; 导入 javax.swing.JPanel;

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


    Graphics pen, pane;

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

    public void paintDie(Graphics pen)
    {

        //Creates the square
        pen.drawRect(getX(), getY(), DIE_WIDTH, DIE_HEIGHT);
        pen.setColor(LINE_COLOR);
        pen.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 ()
    {
        pane.setColor(DIE_COLOR);
        pane.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);

    }

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

    }
    private void drawOne ()
    {

        drawBlank();
        drawDot(xCoordinate, yCoordinate);

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

    }
    private void drawThree ()
    {
        drawBlank();
        drawOne();
        drawTwo();

    }

    private void drawFour ()
    {
        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);

    }

}

【问题讨论】:

  • 您已经告诉我们您的目标,您已经发布了代码,但您根本没有真正解释问题所在。会发生什么?
  • 一些导入语句没有出现在代码中,但我向你保证我把它们都写进去了,所以这不是我认为的问题。
  • 你应该描述你的问题。
  • 我没有画出它应该画的东西,而是得到一个空的窗口,里面什么都没有。

标签: java eclipse


【解决方案1】:

您的类有一个 paintDie(Graphics g) 方法,其中包含用于绘制事物的代码,但它从未被调用过,因此什么都看不到是有道理的。此外,您的 JPanel 类没有 paintComponent(Graphics g) 方法覆盖,这是绘制应该完成的地方。没有这个,什么都不会画。

尝试将paintDie改为paintComponent:

@Override
protected void paintComponent(Graphics pen) {
   super. paintComponent(pen);
   // ... etc
}

不要忘记添加@Override 注释,这样您就知道您实际上是在覆盖父方法。

【讨论】:

  • 这样做会给我以下错误:线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
  • @user3457911:然后找到引发异常的行并修复它。找出该行中哪个变量为空,回顾您的代码并确保它已初始化。
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 2021-05-28
  • 2012-09-22
相关资源
最近更新 更多