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