骰子模拟

时间:2015-07-09 02:45:24

标签: java simulation

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class DiceSimulation extends JApplet
{
  /* Initialising Applet */
  public DiceSimulation()
  {
    this.setContentPane(new DiceRollPanel());
  }
  /* method main */
  public static void main(String[] args)
  {
    /* frame */
    JFrame dicewindow = new JFrame();
    dicewindow.setTitle("Dice Simulation");
    dicewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    dicewindow.setContentPane(new DiceRollPanel());
    dicewindow.pack();
    dicewindow.setVisible(true);
  }
}
class DiceRollPanel extends JPanel
{
  /* Variables declaration */
  private Dice left;
  private Dice right;
  /* constructor */
  DiceRollPanel()
  {
    left = new Dice();
    right = new Dice();
    JButton dicerollButton = new JButton("Click");
    /* dice button font */
    dicerollButton.setFont(new Font("Calibri", Font.PLAIN, 24));
    dicerollButton.addActionListener(new DiceRollListener());
    JPanel dicerollPanel =new JPanel();
    /* dice simulation layout */
    dicerollPanel.setLayout(new GridLayout(1, 2, 6, 0));
    dicerollPanel.add(left);
    dicerollPanel.add(right);
    this.setLayout(new BorderLayout());
    this.add(dicerollButton, BorderLayout.SOUTH);
    this.add(dicerollPanel, BorderLayout.CENTER);
  }
  /* Class for DiceRoll button */
  private class DiceRollListener implements ActionListener
  {
    /* performing action */
    public void actionPerformed(ActionEvent e)
    {
      left.rolls();
      right.rolls();
    }
  }
}
class Dice extends JPanel
{
  /* variable declaration */
  private int values; /* values on the Dice face */
  private int diameterr = 9; /* Diameter of the spots */
  /* class variables */
  private static Random random = new Random();
  public Dice()
  {
    setBackground(Color.white);
    setPreferredSize(new Dimension(90,90));
    rolls();
  }
  public int rolls()
  {
    int value = random.nextInt(6) + 1;
    setValues(value);
    return value;
  }
  public int getValues()
  {
    return values;
  }
  public void setValues(int spots)
  {
    values = spots;
    repaint();
  }
  public void paint(Graphics a)
  {
    super.paint(a);
    int width = getWidth();
    int height = getHeight();
    switch (values)
    {
      case 1: drawSpots(a, width/2, height/2);
              break;
      case 2: drawSpots(a, width/4, height/4);
              drawSpots(a, 3*width/4, 3*height/4);
              break;
      case 3: drawSpots(a, width/2, height/2);
      case 4: drawSpots(a, width/4, height/4);
              drawSpots(a, 3*width/4, 3*height/4);
              drawSpots(a, 3*width/4, height/4);
              drawSpots(a, width/4, 3*height/4);
              break;
      case 5:drawSpots(a, width/2, height/2); 
      case 6:drawSpots(a, width/4, height/4);
             drawSpots(a, 3*width/4, 3*height/4);
             drawSpots(a, 3*width/4, height/4);
             drawSpots(a, width/4, 3*height/4);
             drawSpots(a, width/4, height/2);
             drawSpots(a, 3*width/4, height/2);
             break;
    }
  }
  private void drawSpots(Graphics a, int m, int n) /* drawSpots fn */
               {
   a.fillOval(m-diameter/2, n-diameter/2, diameter, diameter);
  }
}

因此,此代码用于制作骰子模拟作为标题推断。我已接近完成它,但我遇到了第116行错误的路障:a.fillOval(m-diameter/2, n-diameter/2, diameter, diameter); 错误:找不到符号   符号:可变直径   location:class Dice

1 个答案:

答案 0 :(得分:2)

您已将其定义为直径 r

   $newscript.rb FIRSTLINE

并将其用作 private int diameterr = 9; /* Diameter of the spots */

diameter