Search code examples
javabuttondice

Adding a "Roll" button to my dice?


I am having trouble figuring out how to get the roll button to work with my program. I can get the roll button to appear, but can't figure out how to get the listener to work . I don't want to have to keep re-running the program for it to work.

Here is my Die Class:

public class Die {

    public static void main(String[] args) {
    }
        protected int face = 1;

        void roll() {
            face=(int)(Math.random()*6 + 1);
        }

        public int getFace() {
            return face;
        }

        public int setFace() {
            return face;
        }

Here is my Graphics class where I make the dice:

public class Graphics extends Die {
    private int x,y;
    boolean locked;


    public Graphics(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void draw(DrawingKit dk){
        Rectangle2D.Float die1 = new Rectangle2D.Float(x, y, 80, 80);

        Ellipse2D.Float topleft = new Ellipse2D.Float(x+3,y+3,18,18);
        Ellipse2D.Float topright = new Ellipse2D.Float(x+55,y+3,18,18);
        Ellipse2D.Float middleleft = new Ellipse2D.Float(x+3,y+28,18,18);
        Ellipse2D.Float middle = new Ellipse2D.Float(x+28,y+28,18,18);
        Ellipse2D.Float middleright = new Ellipse2D.Float(x+55,y+28,18,18);
        Ellipse2D.Float bottomleft = new Ellipse2D.Float(x+3,y+53,18,18);
        Ellipse2D.Float bottomright = new Ellipse2D.Float(x+55,y+53,18,18);
        dk.setPaint(Color.green);
        dk.fill(die1);
        if (face > 1) {
            dk.draw(topleft);
            dk.setPaint(Color.black);
            dk.fill(topleft);}
        if (face > 3)
        {   dk.draw(topright);
        dk.setPaint(Color.black);
        dk.fill(topright);}
        if (face == 6)
        {   dk.draw(middleleft);
        dk.setPaint(Color.black);
        dk.fill(middleleft);}
        if (face % 2 == 1)
        {   dk.draw(middle);
        dk.setPaint(Color.black);
        dk.fill(middle);}
        if (face == 6)
        {   dk.draw(middleright);
        dk.setPaint(Color.black);
        dk.fill(middleright);}
        if (face > 3)
        {   dk.draw(bottomleft);
        dk.setPaint(Color.black);
        dk.fill(bottomleft);}
        if (face > 1)
        {   dk.draw(bottomright);
        dk.setPaint(Color.black);
        dk.fill(bottomright);}
            }



    public static void main(String[] args) {

}
}

Lastly, here is my roll code which effectively runs the program and displays the button:

public class Roll {


    public static void main(String [] args ){

        JPanel topPanel = new JPanel ();
        JButton button1 = new JButton("Roll");
        topPanel.add(button1);

        int cnt1 = 1, cnt2 = 2, cnt3 = 3, cnt4 = 4, cnt5 = 5, cnt6 = 6;
        DrawingKit dk = new DrawingKit("My Dice");
        dk.addPanel(topPanel);
        Graphics die1 = new Graphics(0,45);
                die1.roll();
                die1.draw(dk);
                Graphics die2 = new Graphics(100,45);
                die2.roll();
                die2.draw(dk);
                Graphics die3 = new Graphics(200,45);
                die3.roll();
                die3.draw(dk);
                Graphics die4 = new Graphics(300,45);
                die4.roll();
                die4.draw(dk);
                Graphics die5 = new Graphics(400,45);
                die5.roll();
                die5.draw(dk);


        String s1 = String.format("val1 = %d", (int)10.5678);



        // System.out.println(s1);

        String result = String.format("Rolled a %d", die1.getFace());
        System.out.println(result);

        for (int i = 0; i< 60000; i++) {
            die1.roll();
        die1.setFace();
        switch (die1.getFace()) {
        case 1: cnt1++;;
        break;
        case 2: cnt2++;;
        break;
        case 3: cnt3++;;
        break;
        case 4: cnt4++;;
        break;
        case 5: cnt5++;;
        break;
        case 6: cnt6++;;
        break;
        default: System.out.print("Unexpected value");
        System.out.println(die1.getFace());
    }

}
        System.out.print("Number of 1's ");
        System.out.println(cnt1);
        System.out.print("Number of 2's ");
        System.out.println(cnt2);
        System.out.print("Number of 3's ");
        System.out.println(cnt3);
        System.out.print("Number of 4's ");
        System.out.println(cnt4);
        System.out.print("Number of 5's ");
        System.out.println(cnt5);
        System.out.print("Number of 6's ");
        System.out.println(cnt6);
}
}

Can anyone please explain to me what I am doing wrong and help me?

Thanks


Solution

  • After you create the JButton, you have to add an ActionListener for that button. Somthing like this:

    button1.addActionListener(new ActionListener() {
    
        public void actionPerformed(ActionEvent event) {
    
            // Whatever you want to do when the user clicks the button
    
        }
    
    });
    

    For example:

    public class Roll {
    
        DrawingKit dk = new DrawingKit("My Dice");
    
        Graphics die1 = new Graphics(0,45);
        Graphics die2 = new Graphics(100,45);
        Graphics die3 = new Graphics(200,45);
        Graphics die4 = new Graphics(300,45);
        Graphics die5 = new Graphics(400,45);    
    
        public static void main(String [] args ){
    
            JPanel topPanel = new JPanel ();
            JButton button1 = new JButton("Roll");
    
            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
    
                    // Roll and draws the dices
    
                    rollDices();
                    drawDices();
    
                }
            });
    
    
            topPanel.add(button1);
            dk.addPanel(topPanel);
    
        }
    
        public static void rollDices() {
    
            die1.roll();
            die2.roll();
            die3.roll();
            die4.roll();
            die5.roll();
    
        }
    
        public static void rollDices() {
    
            die1.draw(dk);
            die2.draw(dk);
            die3.draw(dk);
            die4.draw(dk);
            die5.draw(dk);
    
        }
    
    }