public class Main { public static void main(String[] args) { final int NUM_DICE = 5; final int DICE_SIDES = 10; Histogram disp = new Histogram(NUM_DICE, DICE_SIDES); disp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); disp.setVisible(true); } }
NOTE: The code here may look a little different from what was given for the assignment. The important stuff is the same.
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; public class Histogram extends JFrame implements MouseListener { private final int NUM_DICE; private final int MIN_ROLL; private final int DICE_SIDES; private final int MAX_ROLL; final int FONT_SIZE = 12; // number of pixels in x-direction per number private int[] results; // Use this variable to store results of dice rolls public Histogram(int numDice, int diceSides) { NUM_DICE = numDice; MIN_ROLL = NUM_DICE; DICE_SIDES = diceSides; MAX_ROLL = NUM_DICE * DICE_SIDES; init(); } public void init() { setSize(80 + 2 * FONT_SIZE * MAX_ROLL,700); setBackground(Color.WHITE); setTitle("AWSUM!@(!*(*@(@#??@1! Histogram Thing"); addMouseListener(this); computeResults(); repaint(); } // Roll NUM_DICE dice 1000 times, keeping track of the results. // The number of times each result is produced will be presented // visually in drawBars(). public void computeResults() { Die[] die = new Die[NUM_DICE]; // Create array of Die references results = new int[MAX_ROLL+1]; // Don't use results[0..NUM_DICE-1] final int TRIALS = 1000; // #times to roll four dice // Actually create the dice to go in the array // Remember: An object is only created if new with parentheses // is used. New with square brackets creates an array, but // not any individual objects that go in the array. for (int i = 0; i < die.length; i++) { die[i] = new Die(DICE_SIDES); } // Keep rolling dice and keep track of how many time // a result is generated in the array. Use the sum of // the dice as the index. // // Example: If the sum of the four dice is 17, then add // one to the 17th element of the array for (int i = 0; i < TRIALS; i++) { int sum = 0; for (int j = 0; j < die.length; j++) { sum += die[j].roll(); } results[sum]++; } // for (int i = 0; i < results.length; i++) { // System.out.print(results[i] + " "); // } } public void paint(Graphics g) { int yAxisXPos = 50; int xAxisYPos = getHeight() - 50; clearDisplay(g); g.setColor(Color.BLACK); drawAxes(g, yAxisXPos, xAxisYPos); drawNums(g, yAxisXPos, xAxisYPos); g.setColor(Color.BLUE); drawBars(g, yAxisXPos, xAxisYPos); } public void clearDisplay(Graphics g) { Color c = g.getColor(); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(c); } public void drawAxes(Graphics g, int xPos, int yPos) { g.drawLine(xPos, yPos, xPos, 0); g.drawLine(xPos, yPos, getWidth(), yPos); } public void drawNums(Graphics g, int xPos, int yPos) { g.setFont(new Font("Courier", Font.ITALIC, FONT_SIZE)); for (int i = MIN_ROLL; i <= MAX_ROLL; i++) { if (i < 10) { g.drawString("" + i, xPos + 2 * FONT_SIZE * (i-MIN_ROLL) + 13, yPos + 12); } else { g.drawString("" + i, xPos + 2 * FONT_SIZE * (i-MIN_ROLL) + 10, yPos + 12); } } } public void drawBars(Graphics g, int xPos, int yPos) { final int UNIT_HEIGHT = 4; for (int i = MIN_ROLL; i < results.length; i++) { g.fillRect(xPos + 2 * FONT_SIZE * (i-MIN_ROLL) + 10, yPos - UNIT_HEIGHT*results[i], 20, UNIT_HEIGHT*results[i]); } } public void mouseClicked(MouseEvent arg0) { computeResults(); repaint(); } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }