import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LabEx4 extends JFrame{
private JLabel rollgame, rid1, rid2, rid3, rid4, rid5, rid6, led1, led2, led3, led4, led5, led6;
private JButton Roll, Stop;
private StartButtonHandler sbh;
private StopButtonHandler stbh;
private boolean stop = false;
private int pict = 1;
public void Runner(){
rollgame = new JLabel("Rolling Dice for Games");
rollgame.setFont(new Font("Grease", Font.BOLD, 40));
rollgame.setLocation(180, 20);
rollgame.setSize(300, 80);
ImageIcon rd1 = new ImageIcon(
getClass().getResource("Dice1.jpg"));
rid1 = new JLabel(rd1);
rid1.setVisible(true);
ImageIcon rd2 = new ImageIcon(
getClass().getResource("Dice2.jpg"));
rid2 = new JLabel(rd2);
rid2.setVisible(false);
ImageIcon rd3 = new ImageIcon(
getClass().getResource("Dice3.jpg"));
rid3 = new JLabel(rd3);
rid3.setVisible(false);
ImageIcon rd4 = new ImageIcon(
getClass().getResource("Dice4.jpg"));
rid4 = new JLabel(rd4);
rid4.setVisible(false);
ImageIcon rd5 = new ImageIcon(
getClass().getResource("Dice5.jpg"));
rid5 = new JLabel(rd5);
rid5.setVisible(false);
ImageIcon rd6 = new ImageIcon(
getClass().getResource("Dice6.jpg"));
rid6 = new JLabel(rd6);
rid6.setVisible(false);
ImageIcon ld1 = new ImageIcon(
getClass().getResource("Dice1.jpg"));
led1 = new JLabel(ld1);
led1.setVisible(true);
ImageIcon ld2 = new ImageIcon(
getClass().getResource("Dice2.jpg"));
led2 = new JLabel(ld2);
led2.setVisible(false);
ImageIcon ld3 = new ImageIcon(
getClass().getResource("Dice3.jpg"));
led3 = new JLabel(ld3);
led3.setVisible(false);
ImageIcon ld4 = new ImageIcon(
getClass().getResource("Dice4.jpg"));
led4 = new JLabel(ld4);
led4.setVisible(false);
ImageIcon ld5 = new ImageIcon(
getClass().getResource("Dice5.jpg"));
led5 = new JLabel(ld5);
led5.setVisible(false);
ImageIcon ld6 = new ImageIcon(
getClass().getResource("Dice6.jpg"));
led6 = new JLabel(ld6);
led6.setVisible(false);
Container pane = getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.CENTER, 40,40));
JButton Roll = new JButton("ROLL");
Roll.setFont(new Font("Georgia", Font.BOLD, 12));
Roll.setSize(100,50);
sbh = new StartButtonHandler();
Roll.addActionListener(sbh);
pane.add(Roll);
JButton Stop = new JButton("STOP");
Stop.setFont(new Font("Georgia", Font.BOLD, 12));
Stop.setSize(100,50);
stbh = new StopButtonHandler();
Stop.addActionListener(stbh);
pane.add(Stop);
pane.add(rollgame);
pane.add(rid1);
pane.add(rid2);
pane.add(rid3);
pane.add(rid4);
pane.add(rid5);
pane.add(rid6);
pane.add(led1);
pane.add(led2);
pane.add(led3);
pane.add(led4);
pane.add(led5);
pane.add(led6);
JPanel pan = new JPanel();
pan.setLayout(new FlowLayout(FlowLayout.CENTER, 45,45));
pan.add(Roll);
pane.add(pan, BorderLayout.CENTER);
pan.add(Stop);
setTitle("Two Roll Dice");
setSize(600, 500);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class StartButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ev){
stop = false;
Thread to = new Thread(){
public void run(){
while(!stop){
try{
if(pict == 1){
rid1.setVisible(true);
rid2.setVisible(false);
rid3.setVisible(false);
rid4.setVisible(false);
rid5.setVisible(false);
rid6.setVisible(false);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(false);
led4.setVisible(false);
led5.setVisible(false);
led6.setVisible(true);
pict++;
sleep(200);
}
else if(pict == 2){
rid1.setVisible(false);
rid2.setVisible(true);
rid3.setVisible(false);
rid4.setVisible(false);
rid5.setVisible(false);
rid6.setVisible(false);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(false);
led4.setVisible(false);
led5.setVisible(true);
led6.setVisible(false);
pict++;
sleep(200);
}
else if(pict == 3){
rid1.setVisible(false);
rid2.setVisible(false);
rid3.setVisible(true);
rid4.setVisible(false);
rid5.setVisible(false);
rid6.setVisible(false);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(false);
led4.setVisible(true);
led5.setVisible(false);
led6.setVisible(false);
pict++;
sleep(200);
}
else if(pict == 4){
rid1.setVisible(false);
rid2.setVisible(false);
rid3.setVisible(false);
rid4.setVisible(false);
rid5.setVisible(true);
rid6.setVisible(false);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(true);
led4.setVisible(false);
led5.setVisible(false);
led6.setVisible(false);
pict++;
sleep(200);
}
else if(pict == 5){
rid1.setVisible(false);
rid2.setVisible(false);
rid3.setVisible(false);
rid4.setVisible(false);
rid5.setVisible(true);
rid6.setVisible(false);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(false);
led4.setVisible(false);
led5.setVisible(true);
led6.setVisible(false);
pict++;
sleep(200);
}
else{
rid1.setVisible(false);
rid2.setVisible(false);
rid3.setVisible(false);
rid4.setVisible(false);
rid5.setVisible(false);
rid6.setVisible(true);
led1.setVisible(false);
led2.setVisible(false);
led3.setVisible(false);
led4.setVisible(false);
led5.setVisible(false);
led6.setVisible(true);
pict=1;
sleep(200);
}
}
catch(InterruptedException e){
}
}
}
};
to.start();
}
}
private class StopButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ev){
stop = true;
}
}
}