Results 1 to 4 of 4
  1. #1
    IdrinkJava is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Cool HELP WITH DICE ROLL PROBLEM (netbeans)

    All i need to do is print out the sum of the 2 dice being rolled... it seems fairly easy.. i just don't understand what variable to use to get that number. i've tried calling all the of classes that would seem to work but i always get a different number. this probably shouldn't be in the new to java because its complicated but i am new to java and this is what i get for doing complicated stuff.



    so i have this fairly simple to understand class

    public class RollDicePanel extends JPanel {
    //================================================== ===== instance variables
    private Die _leftDie; // component for one die
    private Die _rightDie;
    //private Die _botDie;

    //================================================== ============ constructor
    /** Create border layout panel with one button and two dice. */
    RollDicePanel() {
    //... Create the dice
    _leftDie = new Die();
    _rightDie = new Die();


    this.setLayout(new BorderLayout(5, 5));

    this.add(_leftDie , BorderLayout.WEST);
    this.add(_rightDie, BorderLayout.EAST);


    this.setBorder(BorderFactory.createEmptyBorder(5,5 ,5,5));
    }
    and the wee bit more complicated Die class that defines the die

    public class Die extends JComponent {
    //================================================== ============== constants
    private static final int SPOT_DIAM = 9; // Diameter of spots

    //================================================== ===== instance variables
    private int _faceValue; // value that shows on face of die


    //================================================== ============ constructor
    /** Initialize to white background and 60x60 pixels. Initial roll.*/
    public Die() {
    //-- Preferred size is set, but layout may change it.
    setPreferredSize(new Dimension(70,70));
    roll(); // Set to random initial value

    }

    //================================================== ============ method roll
    /** Produce random roll in range 1-6. Causes repaint().
    * @return Result of roll (1-6).
    */
    public int roll() {
    int dice1 = (int)(6*Math.random() + 1);
    // Range 1-6
    setValue(dice1);
    return dice1;
    }

    //================================================== ======== method getValue
    /** Returns result of last roll.*/
    public int getValue() {
    return _faceValue;
    }

    //================================================== ======== method setValue
    /** Sets the value of the Die. Causes repaint().
    * @param spots Number from 1-6.
    */
    public void setValue(int spots) {
    _faceValue = spots;
    repaint(); // Value has changed, must repaint
    }

    //================================================== == method paintComponent
    /** Draws spots of die face. */
    @Override public void paintComponent(Graphics g) {
    int w = getWidth(); // Get height and width
    int h = getHeight();

    //... Change to Graphic2D for smoother spots.
    Graphics2D g2 = (Graphics2D)g; // See note below
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);

    //... Paint background
    g2.setColor(Color.GREEN);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.BLACK);

    g2.drawRect(0, 0, w-1, h-1); // Draw border

    switch (_faceValue) {
    case 1:
    drawSpot(g2, w/2, h/2);
    break;
    case 3:
    drawSpot(g2, w/2, h/2);
    // Fall thru to next case
    case 2:
    drawSpot(g2, w/4, h/4);
    drawSpot(g2, 3*w/4, 3*h/4);
    break;
    case 5:
    drawSpot(g2, w/2, h/2);
    // Fall thru to next case
    case 4:
    drawSpot(g2, w/4, h/4);
    drawSpot(g2, 3*w/4, 3*h/4);
    drawSpot(g2, 3*w/4, h/4);
    drawSpot(g2, w/4, 3*h/4);
    break;
    case 6:
    drawSpot(g2, w/4, h/4);
    drawSpot(g2, 3*w/4, 3*h/4);
    drawSpot(g2, 3*w/4, h/4);
    drawSpot(g2, w/4, 3*h/4);
    drawSpot(g2, w/4, h/2);
    drawSpot(g2, 3*w/4, h/2);
    break;
    }
    }

    //================================================== ======== method drawSpot
    /** Utility method used by paintComponent(). */
    private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }
    }

    Heres My Tester

    public class Tester extends JApplet {



    public static void main(String[] args) {



    JFrame window = new JFrame("Dice Demo");
    Die player = new Die();

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setContentPane(new RollDicePanel());
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);

    System.out.println("what do i need here to output the sum of both rolled dice");

    }

    }

  2. #2
    IdrinkJava is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    i put a counter in the switch statement to add up the faces of the die, but i have no clue how to transfer that value back to my tester without it getting changed

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    IdrinkJava is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    anywhere. lol joption pane, regular output, i just need the number. i can output it to whatever from there

Similar Threads

  1. roll back
    By haya in forum NetBeans
    Replies: 1
    Last Post: 06-07-2010, 07:02 PM
  2. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 08:39 AM
  3. Need help with java rapid roll game
    By blunderblitz in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 12:36 PM
  4. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 01:37 AM
  5. Roll 2-Dice "Pig" Game Help
    By King8654 in forum AWT / Swing
    Replies: 7
    Last Post: 04-07-2008, 06:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •