import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.JTextField;
import javax.swing.Timer;
public class ElectronicSlot8 extends JFrame implements ActionListener
{
private Icon iconPic1, iconPic2, iconPic3, iconPic4, iconPic5, iconPic6, iconPic7, iconPic8;
private JLabel lblBalance, lblWinnings;
private JTextField txtBalance, txtWinnings;
private JButton btnBye, btnHoldLeft, btnHoldRight, btnHoldMiddle;
private Timer timer;
private JButton btnStart, btnMoney, btnCashout, btnTransfer;
private int one, two, three, counter;
private int balance, winnings;
private Random random;
private JButton btnSpinPicture1, btnSpinPicture2, btnSpinPicture3;
public static void main(String[] args)
{
ElectronicSlot8 electonicslot8 = new ElectronicSlot8();
electonicslot8.setVisible(true);
}
public ElectronicSlot8()
{
balance = 0;
random = new Random();
setLayout(new FlowLayout());
btnSpinPicture1 = new JButton();
btnSpinPicture1.setPreferredSize(new Dimension (150,150));
btnSpinPicture1.setBackground (Color.gray);
btnSpinPicture2 = new JButton();
btnSpinPicture2.setPreferredSize(new Dimension (150,150));
btnSpinPicture2.setBackground (Color.gray);
btnSpinPicture3 = new JButton();
btnSpinPicture3.setPreferredSize(new Dimension (150,150));
btnSpinPicture3.setBackground (Color.gray);
txtBalance = new JTextField(3);
txtBalance.setFont(new Font("Arial", Font.BOLD, 16));
txtBalance.setBackground(Color.white);
txtBalance.setForeground(Color.blue);
txtBalance.setText(balance + "");
txtWinnings = new JTextField(3);
txtWinnings.setFont(new Font("Arial", Font.BOLD, 16));
txtWinnings.setBackground(Color.white);
txtWinnings.setForeground(Color.blue);
txtWinnings.setText(balance + "");
btnTransfer = new JButton("Add to Balance");
btnTransfer.setEnabled(false);
btnMoney = new JButton("Add £1");
btnStart = new JButton("Play");
btnStart.setEnabled(false);
btnBye = new JButton("Exit");
btnCashout = new JButton("Withdraw");
btnHoldLeft = new JButton("Hold");
btnHoldLeft.setPreferredSize(new Dimension(150,25));
btnHoldLeft.setEnabled(false);
btnHoldRight = new JButton("Hold");
btnHoldRight.setPreferredSize(new Dimension(150,25));
btnHoldRight.setEnabled(false);
btnHoldMiddle = new JButton("Hold");
btnHoldMiddle.setPreferredSize(new Dimension(150,25));
btnHoldMiddle.setEnabled(false);
lblBalance = new JLabel("Balance");
lblBalance.setPreferredSize(new Dimension(100,50));
lblBalance.setBackground(Color.black);
lblWinnings = new JLabel("Winnings");
lblWinnings.setPreferredSize(new Dimension(100,50));
lblWinnings.setBackground(Color.black);
add(txtBalance);
add(lblBalance);
add(txtWinnings);
add(lblWinnings);
add(btnStart);
add(btnBye);
add(btnSpinPicture1);
add(btnSpinPicture2);
add(btnSpinPicture3);
add(btnHoldLeft);
add(btnHoldMiddle);
add(btnHoldRight);
add(btnMoney);
add(btnCashout);
add(btnTransfer);
btnMoney.addActionListener(this);
btnStart.addActionListener(this);
btnTransfer.addActionListener(this);
btnBye.addActionListener(this);
btnCashout.addActionListener(this);
btnHoldLeft.addActionListener(this);
btnHoldMiddle.addActionListener(this);
btnHoldRight.addActionListener(this);
iconPic1 = new ImageIcon("bin/Pic1.jpg");
iconPic2 = new ImageIcon("bin/Pic2.jpg");
iconPic3 = new ImageIcon("bin/Pic3.jpg");
iconPic4 = new ImageIcon("bin/Pic4.jpg");
iconPic5 = new ImageIcon("bin/Pic5.jpg");
iconPic6 = new ImageIcon("bin/Pic6.jpg");
iconPic7 = new ImageIcon("bin/Pic7.jpg");
iconPic8 = new ImageIcon("bin/Pic8.jpg");
random = new Random();
setTitle("Electronic Bandit");
setSize(500,320);
}
public void actionPerformed (ActionEvent event)
{
if(event.getSource()==btnBye)
{
JOptionPane.showMessageDialog(null,"Thank You For Playing....Bye!!!");
System.exit(0);
}
if(event.getSource()==btnCashout)
{
JOptionPane.showMessageDialog(null, "You have collected " +winnings+"p, Bye!!!");
System.exit(0);
}
if (event.getSource()==btnMoney){
txtBalance.setForeground(Color.black);
balance = balance + 100;
txtBalance.setText(balance + "");
}
if (event.getSource() ==btnMoney)
{
btnStart.setEnabled(true);
}
if (event.getSource()==btnTransfer){
balance = winnings + balance;
txtBalance.setText(balance +"");
JOptionPane.showMessageDialog(null,"You have transferred " +winnings+"p");
}
if (event.getSource()==btnTransfer){
txtWinnings.setText("0");
}
if (event.getSource()==btnTransfer){
winnings =0;
btnTransfer.setEnabled(false);
JOptionPane.showMessageDialog(null,"You now have no winnings to transfer!!!");
}
if(winnings >=20){
btnTransfer.setEnabled(true);
}
if (event.getSource() ==btnStart)
{
btnHoldLeft.setEnabled(true);
btnHoldMiddle.setEnabled(true);
btnHoldRight.setEnabled(true);
}
if (event.getSource() ==btnStart){
balance = balance - 20;
txtBalance.setText(balance +"");
}
if (balance>=20){
btnStart.setEnabled(true);
}
if(balance <20){
balance = 0;
btnStart.setEnabled(false);
JOptionPane.showMessageDialog(null,"After this spin you will not have enough Credit. Please insert more Money!!!");
}
if(event.getSource()==btnStart){
counter = 0;
timer = new Timer(100,this);
timer.start();
}
if(event.getSource()==timer) {
setIcon ();
}
if(event.getSource()==timer){
timer.stop();
}
}
public void stopTimer(){
timer.stop();
}
public void setIcon(){
one = random.nextInt(8);
switch (one){
case 0:
btnSpinPicture1.setIcon(iconPic1);
one = 1;
counter ++;
break;
case 1:
btnSpinPicture1.setIcon(iconPic2);
one = 2;
counter ++;
break;
case 2:
btnSpinPicture1.setIcon(iconPic3);
one = 3;
counter ++;
break;
case 3:
btnSpinPicture1.setIcon(iconPic4);
one = 4;
counter ++;
break;
case 4:
btnSpinPicture1.setIcon(iconPic5);
one = 5;
counter ++;
break;
case 5:
btnSpinPicture1.setIcon(iconPic6);
one = 6;
counter ++;
break;
case 6:
btnSpinPicture1.setIcon(iconPic7);
one = 7;
counter ++;
break;
case 7:
btnSpinPicture1.setIcon(iconPic8);
one = 0;
counter ++;
break;
}
two = random.nextInt(8);
switch (two){
case 0:
btnSpinPicture2.setIcon(iconPic1);
two = 1;
counter ++;
break;
case 1:
btnSpinPicture2.setIcon(iconPic2);
two = 2;
counter ++;
break;
case 2:
btnSpinPicture2.setIcon(iconPic3);
two = 3;
counter ++;
break;
case 3:
btnSpinPicture2.setIcon(iconPic4);
two = 4;
counter ++;
break;
case 4:
btnSpinPicture2.setIcon(iconPic5);
two = 5;
counter ++;
break;
case 5:
btnSpinPicture2.setIcon(iconPic6);
two = 6;
counter ++;
break;
case 6:
btnSpinPicture2.setIcon(iconPic7);
two = 7;
counter ++;
break;
case 7:
btnSpinPicture2.setIcon(iconPic8);
two = 0;
counter ++;
break;
}
three = random.nextInt(8);
switch (three){
case 0:
btnSpinPicture3.setIcon(iconPic1);
three = 1;
counter ++;
break;
case 1:
btnSpinPicture3.setIcon(iconPic2);
three = 2;
counter ++;
break;
case 2:
btnSpinPicture3.setIcon(iconPic3);
three = 3;
counter ++;
break;
case 3:
btnSpinPicture3.setIcon(iconPic4);
three = 4;
counter ++;
break;
case 4:
btnSpinPicture3.setIcon(iconPic5);
three = 5;
counter ++;
break;
case 5:
btnSpinPicture3.setIcon(iconPic6);
three = 6;
counter ++;
break;
case 6:
btnSpinPicture3.setIcon(iconPic7);
three = 7;
counter ++;
break;
case 7:
btnSpinPicture3.setIcon(iconPic8);
three = 0;
counter ++;
break;
}
checkWin();
if(counter >20)
stopTimer();
}
public void checkWin(){
if (one == two && two == three){
winnings = winnings + 100;
txtWinnings.setText(winnings + "");
JOptionPane.showMessageDialog(null, "Winner £1.00");
}
else if ( one == two || one == three || two == three){
winnings = winnings + 50;
txtWinnings.setText(winnings + "");
JOptionPane.showMessageDialog(null, "50p Win");
}
else if (three == 1){
winnings = winnings + 20;
txtWinnings.setText(winnings + "");
JOptionPane.showMessageDialog(null, "20p Win");
}
}
}