Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Java coding for Rolling 2 Dices .. Can Any1 Help ??!!

    Hello every1 , i seriously need some help with this coding as I am struggling with it ..


    I am actually working on a small project yet its widning me up

    the task is :

    I need to create a game using NETBEANS , the game should proceed as Follows :

    •On pressing the new game button, both dice are removed from view, any message is removed from view and the roll the dice button is enabled
    •On pressing the roll the dice button once after the new game started the computer’s dice will be rolled and the result displayed
    •On pressing the roll the dice button a second time, the player’s dice will be rolled and the result will be displayed; the roll the dice button will then be disabled. If the player’s dice is higher than the computer’s dice, the window will display “Winner!” to the user and the score will increase by 2 points. If the player loses, the window will display “You lose” and the score will decrease by 1 point.


    I have managed to complete the first bullet point , however am struggling with the dice rolling, especially that am dealing with 2 dices one representing the Computer and the other representing the Player..


    The following code is what I have done so far and by comiling it I actually meet the requirements of bullet point 1 :

    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.*;



    public class HelloWorldFrame extends JFrame implements ActionListener {

    //define and create the components with their default values
    JLabel jlbLabel1 = new JLabel();
    JLabel jlbLabel2 = new JLabel();
    JButton jbtMyButton = new JButton ();
    JButton jbtMyButton3 = new JButton ();
    JLabel jlbLabel3 = new JLabel ();

    public static void main(String args[]) {
    new HelloWorldFrame();

    }

    //a constructor which sets up all the components
    HelloWorldFrame() {
    FlowLayout flowMyLayout = new FlowLayout ();
    setLayout(flowMyLayout);

    jlbLabel1.setText("");
    jlbLabel2.setText("Dice 1");
    jbtMyButton.setText("New Game");
    jbtMyButton3.setText("RollThe Dice");
    jlbLabel3.setText("Dice 2");


    //put the components on the window
    add(jlbLabel1);
    add(jlbLabel2);
    add(jbtMyButton);
    //add(jbtMyButton3);
    add(jlbLabel3);



    jbtMyButton.addActionListener(this);

    this.setSize(100, 100);
    setVisible(true);


    jbtMyButton3.addActionListener(this);

    this.setSize(100, 100);
    setVisible(true);

    }
    public void actionPerformed(ActionEvent e) {
    jbtMyButton.setText("Roll Dice");

    jbtMyButton3.setVisible(true);

    jlbLabel2.setVisible(false);
    jlbLabel3.setVisible(false);



    }


    }



    Can Any1 help me the coding for the rolling both dices !!

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    First off, please add code tags to make this easier to read.

    Second, take the setVisible() calls out of the Constructor of the JFrame. They should never be in there. You should call those after you construct an instance in the main method.

    I would suggest creating separate ActionListener's for each of the buttons to better separate the logic for each one.

Similar Threads

  1. Java rolling dice
    By fifa4ever in forum Java Applets
    Replies: 18
    Last Post: 06-10-2010, 08:43 PM
  2. any1 is ready to help me with my assignment
    By javanew in forum New To Java
    Replies: 3
    Last Post: 05-04-2010, 06:07 PM
  3. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 03:15 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. Any1 can help?
    By Diimitri in forum Java Applets
    Replies: 0
    Last Post: 03-03-2008, 04:35 PM

Posting Permissions

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