JAVA   26
RollAnotherOne
Guest on 9th February 2023 03:05:48 AM


  1. import objectdraw.*;
  2. import java.awt.*;
  3.  
  4.     // A program to simulate the rolling of a pair of dice.
  5. public class RollAnotherOne extends WindowController {
  6.    
  7.         // Coordinates to determine positions of text displayed
  8.     private static final int TEXT_X = 30;
  9.     private static final int PROMPT_Y = 30;
  10.     private static final int RESULT_Y = 100;
  11.    
  12.    
  13.         // The object that represents a single die
  14.     private RandomIntGenerator die =  new RandomIntGenerator( 1, 6);
  15.    
  16.         // A Text message updated to describe each simulated roll
  17.     private Text result;
  18.    
  19.         // value of each die on a given roll
  20.     private int roll1;
  21.     private int roll2;
  22.    
  23.         // Display a prompt and create the Text used to display the results
  24.     public void begin() {
  25.         new Text( "Click to make me roll the dice",
  26.                   TEXT_X, PROMPT_Y, canvas );
  27.         result = new Text( "", TEXT_X, RESULT_Y, canvas );
  28.     }
  29.    
  30.         // Roll the dice with each click
  31.     public void onMouseClick(Location point) {
  32.         roll1 = die.nextValue();
  33.         roll2 = die.nextValue();
  34.        
  35.         result.setText("You rolled a " + roll1 +  " and a " + roll2 +
  36.                        " for a total of " + ( roll1+roll2) );
  37.     }  
  38. }

Raw Paste

Login or Register to edit or fork this paste. It's free.