Hi guys, im facing a real problem with a project im doing at the moment as Im new to Java. Ive set all the buttons for the program however, i need to make the pictures show randomly. also, il need to make some buttons work, like the nudge button for each reel. here is the code ive done so far and im no way near completion yet. thanks guys.

import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class ElectronicBandit extends JFrame implements ActionListener{
  private int iSlot = 0;
  private int counter;
  private JTextField txtNum;
  private int num;
  private Random random;
  private JButton btnSpinPicture1,btnSpinPicture2,btnSpinPicture3,btnSpin, btnHold1,btnHold2,btnHold3,
  btnNudge1, btnNudge2, btnNudge3, btnCashIn, btnAddWinstoPlay, btnInsert£1;
  private Timer timer;
  private Icon fruit1, fruit2, fruit3, fruit4, fruit5, fruit6, fruit7, fruit8;
  
  
  public static void main(String[] args) {

	  ElectronicBandit elecBan = new ElectronicBandit();
      elecBan.setVisible(true) ;
  }
  public ElectronicBandit(){

	 super(".... Electronic Bandit....");
      num = 0;
      random = new Random();
      setLayout(new FlowLayout());
      txtNum = new JTextField(2);
      txtNum.setFont(new Font("TimesRoman", Font.BOLD, 16));
      txtNum.setBackground(Color.green);
      txtNum.setForeground(Color.red);
      txtNum.setText(num + " ");
      btnInsert£1 = new JButton ("Insert £1");
      btnInsert£1.setBackground(Color.cyan);
      btnSpinPicture1 = new JButton("");
      btnSpinPicture2 = new JButton("");
      btnSpinPicture3 = new JButton("");
      btnSpinPicture1.setPreferredSize(new Dimension(200, 200)) ;
      btnSpinPicture1.setBackground(Color.blue );
      btnSpinPicture2.setPreferredSize(new Dimension(200, 200)) ;
      btnSpinPicture2.setBackground(Color.blue );
      btnSpinPicture3.setPreferredSize(new Dimension(200, 200)) ;
      btnSpinPicture3.setBackground(Color.blue );
      btnHold1 = new JButton("Hold");
      btnHold2 = new JButton("Hold");
      btnHold3 = new JButton("Hold");
      btnHold1.setBackground(Color.gray ) ;
      btnHold2.setBackground(Color.gray ) ;
      btnHold3.setBackground(Color.gray ) ;
      btnNudge1 = new JButton("Nudge");
      btnNudge2 = new JButton("Nudge");
      btnNudge3 = new JButton("Nudge");
      btnNudge1.setBackground(Color.red ) ;
      btnNudge2.setBackground(Color.red ) ;
      btnNudge3.setBackground(Color.red ) ;
      btnSpin = new JButton("Spin");
      btnSpin.setBackground(Color.green ) ;
      btnCashIn = new JButton ("Cash In") ;
      btnCashIn.setBackground (Color.white);
      btnAddWinstoPlay = new JButton ("Add Wins to Play");
      btnAddWinstoPlay.setBackground(Color.white);
      btnInsert£1 = new JButton ("Insert £1");
      btnInsert£1.setBackground(Color.green);
      
      setBackground(Color.orange) ;
      setLayout(new FlowLayout()) ;
      add(txtNum);add(btnInsert£1);
      add(btnHold1);add(btnNudge1) ;add(btnSpinPicture1);
      add(btnHold2);add(btnNudge2) ;add(btnSpinPicture2);
      add(btnHold3);add(btnNudge3) ;add(btnSpinPicture3);
      add(btnSpin) ;
      add(btnCashIn); add(btnAddWinstoPlay);
      
      btnHold1.addActionListener(this) ;
      btnHold2.addActionListener(this) ;
      btnHold3.addActionListener(this) ;
      btnNudge1.addActionListener(this) ;
      btnNudge2.addActionListener(this) ;
      btnNudge3.addActionListener(this) ;
      btnSpin.addActionListener(this) ;
      btnCashIn.addActionListener(this) ;
      btnAddWinstoPlay.addActionListener(this);
      btnInsert£1.addActionListener(this);
      
      fruit1 = new ImageIcon("bin/fruit1.gif");
      fruit2 = new ImageIcon("bin/fruit2.gif");
      fruit3 = new ImageIcon("bin/fruit3.gif");
      fruit4 = new ImageIcon("bin/fruit4.gif");
      fruit5 = new ImageIcon("bin/fruit5.gif");
      fruit6 = new ImageIcon("bin/fruit6.gif");
      fruit7 = new ImageIcon("bin/fruit7.gif");
      fruit8 = new ImageIcon("bin/fruit8.gif");
      
      setTitle ("Welcome to Electronic Bandit Machine!");
      setSize(200, 250);
      setVisible(true);

  }


    private JTextField JTextField(int i) {
	// TODO Auto-generated method stub
	return null;
}
	public void actionPerformed( ActionEvent event){

	 if (event.getSource() == btnCashIn){
        JOptionPane.showMessageDialog(null, " You won! :-)") ;
        System.exit(0) ;
      }
	  if (event.getSource()== btnInsert£1)
	  {
			if (num == 5){
			}
			else 
			{
				txtNum.setForeground(Color.black);
				num = num + 1;
				txtNum.setText(num + " ");				
			}			
		}
      if (event.getSource() == btnHold1 )
        stopTimer();

      if (event.getSource() == btnSpin ){
        counter = 0;
        timer = new Timer(10, this);
        timer.start() ;
      }
      if (event.getSource() == timer ){
            setIcon() ;
      }
      }
	}

  // end of Action Performed

    public void stopTimer(){
    	timer.stop();
    }
    private void setIcon(){
    	
      	switch (iSlot) {
    		case 0:
    			btnSpinPicture1.setIcon(fruit1);
    			iSlot=1;
    			counter++;
    			break;
    		case 1:
    			btnSpinPicture1.setIcon(fruit2);
    			iSlot=2;
    			counter++;
    			break;
    		case 2:
    			btnSpinPicture1.setIcon(fruit3);
    			iSlot=3;
    			counter++;
    			break;
    		case 3:
    			btnSpinPicture1.setIcon(fruit4);
    			iSlot=4;
    			counter++;
    			break;
    		case 4:
    			btnSpinPicture1.setIcon(fruit5);
    			iSlot=5;
    			counter++;
    			break;
    		case 5:
    			btnSpinPicture1.setIcon(fruit6);
    			iSlot=6;
    			counter++;
    			break;
    		case 6:
    			btnSpinPicture1.setIcon(fruit7);
    			iSlot=7;
    			counter++;
    			break;
    		case 7:
    			btnSpinPicture1.setIcon(fruit8);
    			iSlot=0;
    			counter++;
    			break;
      	}
    	switch (iSlot)	{
    		case 0:
    			btnSpinPicture2.setIcon(fruit1);
    			iSlot=1;
    			counter++;
    			break;
    		case 1:
    			btnSpinPicture2.setIcon(fruit2);
    			iSlot=2;
    			counter++;
    			break;
    		case 2:
    			btnSpinPicture2.setIcon(fruit3);
    			iSlot=3;
    			counter++;
    			break;
    		case 3:
    			btnSpinPicture2.setIcon(fruit4);
    			iSlot=4;
    			counter++;
    			break;
    		case 4:
    			btnSpinPicture2.setIcon(fruit5);
    			iSlot=5;
    			counter++;
    			break;
    		case 5:
    			btnSpinPicture2.setIcon(fruit6);
    			iSlot=6;
    			counter++;
    			break;
    		case 6:
    			btnSpinPicture2.setIcon(fruit7);
    			iSlot=7;
    			counter++;
    			break;
    		case 7:
    			btnSpinPicture2.setIcon(fruit8);
    			iSlot=0;
    			counter++;
    			break;
    	}
    	switch (iSlot)	{
		case 0:
			btnSpinPicture3.setIcon(fruit1);
			iSlot=1;
			counter++;
			break;
		case 1:
			btnSpinPicture3.setIcon(fruit2);
			iSlot=2;
			counter++;
			break;
		case 2:
			btnSpinPicture3.setIcon(fruit3);
			iSlot=3;
			counter++;
			break;
		case 3:
			btnSpinPicture3.setIcon(fruit4);
			iSlot=4;
			counter++;
			break;
		case 4:
			btnSpinPicture3.setIcon(fruit5);
			iSlot=5;
			counter++;
			break;
		case 5:
			btnSpinPicture3.setIcon(fruit6);
			iSlot=6;
			counter++;
			break;
		case 6:
			btnSpinPicture3.setIcon(fruit7);
			iSlot=7;
			counter++;
			break;
		case 7:
			btnSpinPicture3.setIcon(fruit8);
			iSlot=0;
			counter++;
			break;
	}
           	
  
       	if (counter >800)
    		stopTimer();


    }
	  
}

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

it seems like you didn't define the ImageIcon variables instead you just used Icon

Edited 6 Years Ago by sirlink99: n/a

This article has been dead for over six months. Start a new discussion instead.