-2

I have a program here that rolls two dices. It uses thread(have I done correct? like I used two threads is that ok?) When I click roll, eventually it'll roll. It's working good but everytime I click stop, I always get the same output. How do I get a randomized output? Please help thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Rolling extends JFrame {

private boolean pause = false;
private int pic = 1;
private JLabel lblGame, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, out1, out2;
private JButton roll, stop;
private RollButtonHandler rbh;
private StopButtonHandler sbh;

public void showGui() {

    roll = new JButton("Roll");
    roll.setFont(new Font("Arial", Font.BOLD, 20));
    roll.setLocation(170, 350);
    roll.setSize(150, 80);
    roll.setVisible(true);
    rbh = new RollButtonHandler();
    roll.addActionListener(rbh);

    stop = new JButton("Stop");
    stop.setFont(new Font("Arial", Font.BOLD, 20));
    stop.setLocation(375, 350);
    stop.setSize(150, 80);
    stop.setVisible(true);
    sbh = new StopButtonHandler();
    stop.addActionListener(sbh);

    lblGame = new JLabel("Rolling Dice...");
    lblGame.setFont(new Font("Arial", Font.BOLD, 40));
    lblGame.setLocation(300, 20);
    lblGame.setSize(300, 80);
    lblGame.setVisible(true);

    ImageIcon dice1 = new ImageIcon(getClass().getResource("dice1.jpg"));
    d1 = new JLabel(dice1);
    d1.setSize(500, 200);
    d1.setLocation(200, 80);
    d1.setVisible(true);

    ImageIcon dice2 = new ImageIcon(getClass().getResource("dice2.jpg"));
    d2 = new JLabel(dice2);
    d2.setSize(500, 200);
    d2.setLocation(200, 80);
    d2.setVisible(true);

    ImageIcon dice3 = new ImageIcon(getClass().getResource("dice3.jpg"));
    d3 = new JLabel(dice3);
    d3.setSize(500, 200);
    d3.setLocation(200, 80);
    d3.setVisible(true);

    ImageIcon dice4 = new ImageIcon(getClass().getResource("dice4.jpg"));
    d4 = new JLabel(dice4);
    d4.setSize(500, 200);
    d4.setLocation(200, 80);
    d4.setVisible(true);

    ImageIcon dice5 = new ImageIcon(getClass().getResource("dice5.jpg"));
    d5 = new JLabel(dice5);
    d5.setSize(500, 200);
    d5.setLocation(200, 80);
    d5.setVisible(true);

    ImageIcon dice6 = new ImageIcon(getClass().getResource("dice6.jpg"));
    d6 = new JLabel(dice6);
    d6.setSize(500, 200);
    d6.setLocation(200, 80);
    d6.setVisible(true);

    ImageIcon dice7 = new ImageIcon(getClass().getResource("dice1.jpg"));
    d7 = new JLabel(dice7);
    d7.setSize(500,200);
    d7.setLocation(0,80);
    d7.setVisible(true);

    ImageIcon dice8 = new ImageIcon(getClass().getResource("dice2.jpg"));
    d8 = new JLabel(dice8);
    d8.setSize(500,200);
    d8.setLocation(0,80);
    d8.setVisible(true);

    ImageIcon dice9 = new ImageIcon(getClass().getResource("dice3.jpg"));
    d9 = new JLabel(dice9);
    d9.setSize(500,200);
    d9.setLocation(0,80);
    d9.setVisible(true);

    ImageIcon dice10 = new ImageIcon(getClass().getResource("dice4.jpg"));
    d10 = new JLabel(dice10);
    d10.setSize(500,200);
    d10.setLocation(0,80);
    d10.setVisible(true);

    ImageIcon dice11 = new ImageIcon(getClass().getResource("dice5.jpg"));
    d11 = new JLabel(dice11);
    d11.setSize(500,200);
    d11.setLocation(0,80);
    d11.setVisible(true);

    ImageIcon dice12 = new ImageIcon(getClass().getResource("dice6.jpg"));
    d12 = new JLabel(dice12);
    d12.setSize(500,200);
    d12.setLocation(0,80);
    d12.setVisible(true);

    Random ran = new Random();
    int rollDice1 = ran.nextInt(6) + 1;
    int rollDice2 = ran.nextInt(6) + 1;

    ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 + ".jpg"));
    out1 = new JLabel(output1);
    out1.setSize(500, 200);
    out1.setLocation(0, 80);
    out1.setVisible(false);

    ImageIcon output2 = new ImageIcon(getClass().getResource("dice" + rollDice2 + ".jpg"));
    out2 = new JLabel(output2);
    out2.setSize(500, 200);
    out2.setLocation(200, 80);
    out2.setVisible(false);

    Container pane = getContentPane();
    pane.setLayout(null);
    pane.add(lblGame);
    pane.add(d1);
    pane.add(d2);
    pane.add(d3);
    pane.add(d4);
    pane.add(d5);
    pane.add(d6);
    pane.add(d7);
    pane.add(d8);
    pane.add(d9);
    pane.add(d10);
    pane.add(d11);
    pane.add(d12);
    pane.add(roll);
    pane.add(stop);
    pane.add(out1);
    pane.add(out2);

    RollThread1 thread1 = new RollThread1();
    RollThread2 thread2 = new RollThread2();

    setTitle("Two Roll Dice");
    setSize(600, 500);
    setResizable(false);
    setLocation(400, 200);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public class RollThread1 extends Thread {

    public RollThread1() {
        start();
    }

    public void run() {

        try {
            while(pause) {
                if(pic == 1) {
                    d1.setVisible(false);
                    d2.setVisible(true);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(true);
                    pic++;
                    sleep(250);
                } else if(pic == 2) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(true);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(true);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 3) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(true);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(true);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 4) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(true);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(true);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 5) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(true);
                    d7.setVisible(false);
                    d8.setVisible(true);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 6) {
                    d1.setVisible(true);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(true);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic = 1;
                    sleep(250);
                }
            }
        } catch(InterruptedException e) {
        }
    }
}

public class RollThread2 extends Thread {

    public RollThread2() {
        start();
    }

    public void run() {

        try {
            while(pause) {
                if(pic == 7) {
                    d1.setVisible(false);
                    d2.setVisible(true);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(true);
                    pic++;
                    sleep(250);
                } else if(pic == 8) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(true);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(true);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 9) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(true);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(true);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 10) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(true);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(true);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 11) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(true);
                    d7.setVisible(false);
                    d8.setVisible(true);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 12) {
                    d1.setVisible(true);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(true);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic = 1;
                    sleep(250);
                }
            }
        } catch(InterruptedException e) {
        }
    }
}

public class RollButtonHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        RollThread1 thread1 = new RollThread1();
        RollThread2 thread2 = new RollThread2();
        pause = true;
        stop.setEnabled(true);
        roll.setEnabled(false);

        Random ran = new Random();
        int rollDice1 = ran.nextInt(6) + 1;
        int rollDice2 = ran.nextInt(6) + 1;
    }
}

public class StopButtonHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        RollThread1 thread1 = new RollThread1();
        RollThread2 thread2 = new RollThread2();
        pause = false;
        stop.setEnabled(false);
        roll.setEnabled(true);
        d1.setVisible(false);
        d2.setVisible(false);
        d3.setVisible(false);
        d4.setVisible(false);
        d5.setVisible(false);
        d6.setVisible(false);
        d7.setVisible(false);
        d8.setVisible(false);
        d9.setVisible(false);
        d10.setVisible(false);
        d11.setVisible(false);
        d12.setVisible(false);
        out1.setVisible(true);
        out2.setVisible(true);

        Random ran = new Random();
        int rollDice1 = ran.nextInt(6) + 1;
        int rollDice2 = ran.nextInt(6) + 1;

        ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 +".jpg"));
        out1 = new JLabel(output1);

        ImageIcon output2 = new ImageIcon(getClass().getResource("dice" + rollDice2 +".jpg"));
        out2 = new JLabel(output2);
    }
}


}


public static void main(String[] args) {

    Rolling exe = new Rolling();
    exe.showGui();
}
Ciara
  • 197
  • 1
  • 3
  • 11
  • 1
    `pause` normally false, right? What will `while(pause)` do, then? I suggest `while(!pause)` – John Dvorak Jan 28 '13 at 11:25
  • Also, you create two threads, but you never actually start them. – John Dvorak Jan 28 '13 at 11:26
  • 1
    Please don't just dump your code and hope for something to dig through it. Locate your problem (where are the numbers being generated? Are they repeating themselves at generation? Or maybe they are generated randomly but they get changed somewhat, at someplace? Do your duties before posting. – SJuan76 Jan 28 '13 at 11:28
  • 1
    He starts in constructor. Its bad. http://stackoverflow.com/questions/5623285/java-why-not-to-start-a-thread-in-the-constructor-how-to-terminate – bigGuy Jan 28 '13 at 11:28
  • @Jan Dvorak - thanks! that made it work! i finally get randomized output, but everytime I run it, it automatically rolls, then the roll button seem to act as the stop button, and vice -versa. what's wrong? – Ciara Jan 28 '13 at 18:28
  • @Ciara I guess it's because you use `while(pause)` instead of `while(!pause)` – John Dvorak Jan 28 '13 at 18:30

3 Answers3

1

In the stop code have this

Random ran = new Random(System.currentTimeMillis());
int rollDice1 = ran.nextInt(6) + 1;
ran.setSeed(System.currentTimeMillis());
int rollDice2 = ran.nextInt(6) + 1;
shazin
  • 21,379
  • 3
  • 54
  • 71
  • @Ciara see my point? I was looking for this but found the random calls in other methods, which were not involved. – SJuan76 Jan 28 '13 at 11:38
0

First of all, that is a LOT of code. What I would do is create the image icons, add them to a collection and pick a random one, and display it.

ArrayList<JLabel> diceList = new ArrayList<JLabel>();

public Constructor() {
    ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 + ".jpg"));
    out1 = new JLabel(output1);
    out1.setSize(500, 200);
    out1.setLocation(0, 80);
    out1.setVisible(false);
    diceList.add(out1);
    //Add the other dices aswell
}

public JLabel getRandomDice() {
    Random rand = new Random();
    return diceList.get(rand.nextInt(diceList.getLength()))

}

Note: I havent written Java in some time so it might not compile but you can get the general idea. No need to use threading for this.

Tuim
  • 2,491
  • 1
  • 14
  • 31
0

This is quite bad code even with the requirement to use threads. Google research

  • 'static' fields.
  • ' Java Collections'

Once you do you will instantly see some improvements required in your own code.

Peter_James
  • 647
  • 4
  • 14