繁体   English   中英

如何在java中绘制骰子?

[英]How to draw dice in java?

红色骰子我一直在上班的掷骰子游戏,我无法完成代码,因为我不知道如何在模具上绘制实际的点。 我有桌子和实际的模具,而不是骰子上的点。 我需要帮助来绘制它们。 骰子是红色的。 这就是我所拥有的:

// Draws a given number of dots on this die
private void drawDots(Graphics g, int x, int y, int numDots)
    {
    g.setColor(Color.WHITE);

    int dotSize = dieSize / 4;
    int step = dieSize / 8;
    int x1 = x + step - 1;
    int x2 = x + 3*step;
    int x3 = x + 5*step + 1;
    int y1 = y + step - 1;
    int y2 = y + 3*step;
    int y3 = y + 5*step + 1;

    switch (numDots)
    {
      case 1:
    g.fillOval(x2, y2, dotSize, dotSize);
    break;
  case 2:
    g.fillOval(x3, y1, dotSize, dotSize);
    g.fillOval(x1, y3, dotSize, dotSize);
    break;
  case 3: 
    g.fillOval(x1, y1, dotSize, dotSize);
    g.fillOval(x2, y2, dotSize, dotSize);
    g.fillOval(x3, y3, dotSize, dotSize);
    break;
  case 4:
    g.fillOval(x1, y1, dotSize, dotSize);
    g.fillOval(x3, y3, dotSize, dotSize);
    g.fillOval(x1, y3, dotSize, dotSize);
    g.fillOval(x3, y3, dotSize, dotSize);
    break;
  case 5: 
    g.fillOval(x1, y1, dotSize, dotSize);
    g.fillOval(x2, y2, dotSize, dotSize);
    g.fillOval(x3, y1, dotSize, dotSize);
    g.fillOval(x1, y3, dotSize, dotSize);
    g.fillOval(x3, y3, dotSize, dotSize);
    break;
  case 6:
    g.fillOval(x1, y1, dotSize, dotSize);
    g.fillOval(x2, y1, dotSize, dotSize);
    g.fillOval(x3, y1, dotSize, dotSize);
    g.fillOval(x1, y3, dotSize, dotSize);
    g.fillOval(x2, y3, dotSize, dotSize);
    g.fillOval(x3, y3, dotSize, dotSize);
    break;


}

}
}

我不知道我做错了什么。 当我运行骰子时,它们不会在它们上面显示任何点。

解决了我的问题。 我的Die.java课并没有说它需要什么。 谢谢你的帮助!

我班上原来说:

import java.util.Random;


public class Die
{
  private final static Random random = new Random();
  public Die()
{

}
  public int getRoll()
 {
   return random.nextInt(6)+1;
  }
  public int getNumDots()
  {
    // TODO Auto-generated method stub
    return 0;
  }
}

什么时候应该说:

import java.util.Random;


public class Die
{
  private int sides;
  {
  }

    public void roll()
    {
      sides = (int)(Math.random()* 6 +1);
    }
    public int getNumDots()
    {
      return sides;
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM