3 Replies - 278 Views - Last Post: 18 December 2013 - 08:46 AM Rate Topic: -----

#1 ptpg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-December 13

Slot machine coding

Posted 18 December 2013 - 08:28 AM

Hi I am having problems with coding an aspect of a slot machine. Basically i need three of the images i have saved in the bin folder to appear when the spin button is clicked. Unfortunately this is not happening and i cannot understand why. Can anyone help? Here is my code;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;


public class ElecBandit extends JFrame implements ActionListener
{

	private JPanel paper;
	
	private JButton btnspin,btninsertfunds,btncollect,btnquit,btnpic1,btnpic2,btnpic3;
	
	private Random random;
	
	private Timer timer1;
	
	private int a =0, b=0, c=0,counter,number,collect,winnings,bank;
	 
	private Icon iconpic1, iconpic2, iconpic3, iconpic4, iconpic5, iconpic6, iconpic7, iconpic8; 
	
	private JTextField txtbank, txtwinnings;
	
	private JLabel match3, match2, onecherry;



	public static void main(String[] args) 
	{

		ElecBandit elecbandit = new ElecBandit();
		elecbandit.setVisible(true);
		}

	public ElecBandit(){
	
setLayout (new FlowLayout());
timer1 = new Timer(900,this);
random = new Random();
}
{
	
btncollect = new JButton("Collect");
btncollect.setPreferredSize( new Dimension(200,200));
btncollect.addActionListener(this);
btncollect.setBackground(Color.cyan);
btncollect.setForeground(Color.white);

btninsertfunds = new JButton("Insert Funds");
btninsertfunds.setPreferredSize( new Dimension(200,200));
btninsertfunds.addActionListener(this);
btninsertfunds.setBackground(Color.orange);
btninsertfunds.setForeground(Color.white);

btnquit = new JButton("Quit");
btnquit.setPreferredSize( new Dimension(200,200));
btnquit.addActionListener(this);
btnquit.setBackground(Color.red);
btnquit.setForeground(Color.black);

btnspin = new JButton("Spin");
btnspin.setPreferredSize( new Dimension(200,200));
btnspin.addActionListener(this);
btnspin.setBackground(Color.black);
btnspin.setForeground(Color.red);

btnpic1 = new JButton("");
btnpic1.setPreferredSize( new Dimension(200,200));
btnpic1.setBackground(Color.white);

btnpic2 = new JButton("");
btnpic2.setPreferredSize( new Dimension(200,200));
btnpic2.setBackground(Color.white);

btnpic3 = new JButton("");
btnpic3.setPreferredSize( new Dimension(200,200));
btnpic3.setBackground(Color.white);

txtwinnings = new JTextField("Winnings");
txtwinnings.setPreferredSize( new Dimension(200,200));
txtwinnings.setBackground(Color.magenta);

txtbank = new JTextField("Bank");
txtbank.setPreferredSize( new Dimension(200,200));
txtbank.setBackground(Color.yellow);

match3 = new JLabel ("Match 3 Symbols = 100");
match2 = new JLabel ("Match 2 Symbols = 50");
onecherry = new JLabel ("One Cherry = 20");


iconpic1 = new ImageIcon("bin/app.gif");

iconpic2 = new ImageIcon("bin/nte.gif");

iconpic3 = new ImageIcon("bin/ban.gif");

iconpic4 = new ImageIcon("bin/che.gif");

iconpic5 = new ImageIcon("bin/pin.gif");

iconpic6 = new ImageIcon("bin/org.gif");

iconpic7 = new ImageIcon("bin/7.gif");

iconpic8 = new ImageIcon("bin/lem.gif");



add(btninsertfunds);

add(btncollect);

btncollect.setEnabled(false);

add(txtwinnings);

add(txtbank);

add(btnspin);

btnspin.setEnabled(false);

add(btnquit);

add(btnpic1);

add(btnpic2);

add(btnpic3);

add(match3);

add(match2);

add(onecherry);


btninsertfunds.addActionListener(this);

btncollect.addActionListener(this);

btnquit.addActionListener(this);

btnspin.addActionListener(this);



setTitle("One Arm Bandit");
setSize(700,700);
}



public void actionPerformed (ActionEvent e){
	//TODO Auto-generated method stub

	if(e.getSource()==btninsertfunds){
		bank=bank + 50;
	txtbank.setText("bank = " + bank);
		
		if (bank>=20)
		
			btninsertfunds.setEnabled(true);
			
		if (bank>=400)
			
			btninsertfunds.setEnabled(false);
{
	
	if (bank>=20)
		btnspin.setEnabled(true);
	
	if (bank<20)
		btnspin.setEnabled(false);
}

 if (e.getSource()== btnspin){
bank=bank - 20;
txtbank.setText("bank= " + bank);
 }

}

	
	if (e.getSource()==btnquit)
	{
		System.exit(0);
	}
		
	}
		
	

	
private void setIcon1(){
	// TODO Auto-generated method stub

	a=random.nextInt(8);
	switch(a)
	{
	
	case 0:
		btnpic1.setIcon(iconpic1);
		counter = counter+1;
		
		break;
		
	case 1:
		btnpic1.setIcon(iconpic2);
		counter = counter+1;
		
		break;
		
	case 2:
		btnpic1.setIcon(iconpic3);
		counter = counter+1;
		
		break;
		
	case 3:
		btnpic1.setIcon(iconpic4);
		counter = counter+1;
		
		break;
		
	case 4:
		btnpic1.setIcon(iconpic5);
		counter = counter+1;
		
		break;
		
	case 5:
		btnpic1.setIcon(iconpic6);
		counter = counter+1;
		
		break;
		
	case 6:
		btnpic1.setIcon(iconpic7);
		counter = counter+1;
		
		break;
		
	case 7:
		btnpic1.setIcon(iconpic8);
		counter = counter+1;
		
	}
	
		
	}


private void setIcon2()
{
	b=random.nextInt(8);
	switch(B)/>
	{
	
	case 0:
		btnpic2.setIcon(iconpic1);
		counter = counter+1;
		
		break;
		
	case 1:
		btnpic2.setIcon(iconpic2);
		counter = counter+1;
		
		break;
		
	case 2:
		btnpic2.setIcon(iconpic3);
		counter = counter+1;
		
		break;
		
	case 3:
		btnpic2.setIcon(iconpic4);
		counter = counter+1;
		
		break;
		
	case 4:
		btnpic2.setIcon(iconpic5);
		counter = counter+1;
		
		break;
		
	case 5:
		btnpic2.setIcon(iconpic6);
		counter = counter+1;
		
		break;
		
	case 6:
		btnpic2.setIcon(iconpic7);
		counter = counter+1;
		
		break;
		
	case 7:
		btnpic2.setIcon(iconpic8);
		counter = counter+1;
		
	}
	
	if(counter ==10);
	timer1.stop();
	
}

private void setIcon3()
{
	c=random.nextInt(8);
	switch(c)
	{
	
	case 0:
		btnpic3.setIcon(iconpic1);
		counter = counter+1;
		
		break;
		
	case 1:
		btnpic3.setIcon(iconpic2);
		counter = counter+1;
		
		break;
		
	case 2:
		btnpic3.setIcon(iconpic3);
		counter = counter+1;
		
		break;
		
	case 3:
		btnpic3.setIcon(iconpic4);
		counter = counter+1;
		
		break;
		
	case 4:
		btnpic3.setIcon(iconpic5);
		counter = counter+1;
		
		break;
		
	case 5:
		btnpic3.setIcon(iconpic6);
		counter = counter+1;
		
		break;
		
	case 6:
		btnpic3.setIcon(iconpic7);
		counter = counter+1;
		
		break;
		
	case 7:
		btnpic3.setIcon(iconpic8);
		counter = counter+1;
		


Is This A Good Question/Topic? 0
  • +

Replies To: Slot machine coding

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,635
  • Joined: 20-September 08

Re: Slot machine coding

Posted 18 December 2013 - 08:35 AM

Better to load your files/images as resources:
http://technojeeves....a-with-netbeans
http://technojeeves....va-with-eclipse
Was This Post Helpful? 0
  • +
  • -

#3 ptpg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-December 13

Re: Slot machine coding

Posted 18 December 2013 - 08:38 AM

The problem is i have to do it the way i have been taught to and i was taught to do it like this but i just cant understand why it isnt working
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,635
  • Joined: 20-September 08

Re: Slot machine coding

Posted 18 December 2013 - 08:46 AM

Well you've been taught the wrong way :P

Your problem is that you don't know where you are
http://technojeeves....rectory-in-java
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1