2 Replies - 177 Views - Last Post: 07 December 2012 - 12:43 PM Rate Topic: -----

#1 john186  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 12

Buttons how do they work?

Posted 07 December 2012 - 02:19 AM

hi i am trying to create a nudge button and a hold button for my fruit slot machine here the code i have so far
import javax.swing.*;
	import java.awt.*;
	import java.awt.event.*;
	import java.util.*;
	import javax.swing.Timer;


	
	class Bandit extends JFrame implements ActionListener{
	    private int num1, num2, num3, counter1,counter2,counter3;

	    
	    private JButton btnSpinPictures,btnSpinPictures2,btnSpinPictures3,btnPlay, btnExit, btn£, btnCredit, btnCollect,btnhold1,btnhold2,btnhold3,btnnudge1,btnnudge2,btnnudge3;
	   
	    private Timer Timer1;
private Timer Timer2;
private Timer Timer3;
	    private Icon iconPic1, iconPic2, iconPic3, iconPic4, iconPic5, iconPic6, iconPic7, iconPic8;
	    private Random random;
	    public static void main(String[] args) {
	        Bandit spinApp = new Bandit();
	        spinApp.setVisible(true) ;
	    }
	    public Bandit(){
	        super("The Electronic Bandit!!!");
	        //iCons
	        iconPic1 = new ImageIcon("bin/1.gif");
	        iconPic2 = new ImageIcon("bin/2.gif");
	        iconPic3 = new ImageIcon("bin/3.gif");
	        iconPic4 = new ImageIcon("bin/4.gif");
	        iconPic5 = new ImageIcon("bin/5.gif");
	        iconPic6 = new ImageIcon("bin/6.gif");
	        iconPic7 = new ImageIcon("bin/7.gif");
	        iconPic8 = new ImageIcon("bin/8.gif");
	        Timer 1= new Timer1(100, this);
Timer 2= new Timer2(100, this);

Timer 3= new Timer3(100, this);

	        random = new Random ();
	        btnSpinPictures = new JButton("");
	        btnSpinPictures.setPreferredSize(new Dimension(200, 200)) ;
	        btnSpinPictures.setBackground(Color.white );
	        btnSpinPictures2 = new JButton("");
	        btnSpinPictures2.setPreferredSize(new Dimension(200, 200)) ;
	        btnSpinPictures2.setBackground(Color.white );
	        btnSpinPictures3 = new JButton("");
        btnSpinPictures3.setPreferredSize(new Dimension(200, 200)) ;
	        btnSpinPictures3.setBackground(Color.white );
	        btnExit = new JButton("Leaving?");
	        btnExit.setBackground(Color.white);
	        btnPlay = new JButton("Spin");
btnhold1, = new JButton("hold1");
btnhold1,= new JButton("hold2");
btnhold1,= new JButton("hold3");
btnnudge1= new JButton("nudge");
btnnudge1= new JButton("nudge");
btnnudge1 = new JButton("nudge?");
	        btnPlay.setBackground(Color.white);
        btnPlay.setEnabled(false);
	        btnCredit = new JButton("Credit Winnings to Playing Money");
	        btnCollect = new JButton("Collect Winnings");
	        btn£ = new JButton ("Add £1");

	        lblCredit = new JLabel ("Credit");
	        lblCredit.setPreferredSize(new Dimension(100, 50)) ;
	        lblCredit.setBackground(Color.white);
	        lblWinnings = new JLabel ("Winnings");
	        lblWinnings.setPreferredSize(new Dimension(100, 50)) ;
	        lblWinnings.setBackground(Color.white);

      	lblMatchTwo = new JLabel();
		lblMatchTwo.setText("Matched Two: ");
		lblMatchThree = new JLabel();
		lblMatchThree.setText("Matched Three: ");
		lblWon = new JLabel();
		lblWon.setText("Won: ");
		lblLost = new JLabel();
		lblLost.setText("Lost: ");
      
      




	        txtCredit = new JTextField (5);
	        txtCredit.setFont(new Font("Arial",Font.BOLD,16));
	        txtCredit.setForeground(Color.red);
	        txtCredit.setText(money + " ");
	        txtWinnings = new JTextField (4);
	        txtWinnings.setFont(new Font("Arial",Font.BOLD,16));
	        txtWinnings.setForeground(Color.green);
	        txtWinnings.setText(prizeMoney + " ");
	        //Add Buttons
	        add(btnSpinPictures);add(btnSpinPictures2);add(btnSpinPictures3) ;
	        add(btnPlay);add(btnExit);add(btn£);add(btnCredit);add(btnCollect);
	        add(lblCredit);add(txtCredit);add(lblWinnings);add(txtWinnings)add(hold1),add(hold2),add(hold3),
add(nudge1),add(nudge2),add(nudge);
	        //Actions
	        btnPlay.addActionListener(this) ;
	nudge2 .addActionListener(this) ;
	nudge1 .addActionListener(this) ;
nudge3.addActionListener(this) ;
hold1.addActionListener(this) ;
hold2.addActionListener(this) ;
hold3.addActionListener(this) ;





	        btnExit.addActionListener(this) ;
	        btn£.addActionListener(this);
	        btnCredit.addActionListener(this);
       btnCollect.addActionListener(this);
	        //Window
	        setBackground(Color.cyan) ;
	        setLayout(new FlowLayout()) ;
	        setSize(850,400);
	        setVisible(true);
	    }
	    //Button Actions
    public void actionPerformed( ActionEvent e){
	        //Quit Program    
	        if (e.getSource() == btnExit){
	            JOptionPane.showMessageDialog(null,"Thank You For Playing");
            System.exit(0);
	        }
	        //Add£
	        if (e.getSource()== btn£ ){
	            txtCredit.setForeground(Color.red);
	            money = money + 100;
	            txtCredit.setText(money +" ");
	        }
	        //Take credit away
        if (e.getSource() == btnPlay ){
	            money = money - 20;
	            txtCredit.setText(money + " ");
       } 
        //Add Winnings to credit 
	        if (e.getSource() == btnCredit ){
	            money = money + prizeMoney;
	            JOptionPane.showMessageDialog(null,"You Have turn " + prizeMoney + " to credit");
	            prizeMoney = 0;
	            txtCredit.setText(money + " ");
	            txtWinnings.setText(prizeMoney + "");
	 
	        }
	        //Collect Winnings
	        if (e.getSource() == btnCollect ){
	            JOptionPane.showMessageDialog(null,"You Have Collected " + prizeMoney);   
	            prizeMoney = 0;
            txtWinnings.setText (prizeMoney + " ");
	        }
	        //No play    
	        if (money <20){
	            btnPlay.setEnabled(false);
	        }
	        //play
        if (money >0){
            btnPlay.setEnabled(true);
           //creditLimit         
	            if (money >=500){
	                btn£.setEnabled(false);
	            }
	            if (money <=480){
	                btn£.setEnabled(true);
	            }
            //Assign timers to case
	            if (e.getSource()== Timer1){
	                setIcon();
            }
	            if (e.getSource()== Timer2){
	                setIcon2();
	            }
	            if (e.getSource()== Timer3){
	                setIcon3();
	            }
            //Play Bandit
	            if (e.getSource() == btnSpin ){
	                counter = 0;
	                Timer1.start();
Timer3.start();

Timer2.start();
	 
	            } }
	    }    
//hold 
if(e.getSource() == btnhold1) {
timer1.stop():
}

	    private void stopTimer(){
        Timer.stop();
	        //Winnings
	        if (num1==num2 && num2==num3){
	            prizeMoney = prizeMoney + 100;
	            txtWinnings.setText(prizeMoney+ " ");
            JOptionPane.showMessageDialog(null,"You Have Won 100");}
	        else if (num1==num2 || num2 == num3 || num1 == num3 ) {
	            prizeMoney = prizeMoney + 50;
	            txtWinnings.setText(prizeMoney + " ");
            JOptionPane.showMessageDialog(null,"You Have Won 50" );}
	        else if (num1 == 7 || num2 == 7 || num3 == 7){
	            prizeMoney = prizeMoney + 20;
	            txtWinnings.setText(prizeMoney + " ");
	            JOptionPane.showMessageDialog(null,"You Have Won 20" );}
	    }
public void matchCheck() {
    	 if (num1 == num2 && num2 == num3) {
    		 lblMatchThree.setText("Matched Three: "+matchThree());;
    	 } else if (num1 == num2 || num1 == num3) {
    		 lblMatchTwo.setText("Matched Two: "+matchTwo());
    	 } else if (num2 == num3) {
    		 lblMatchTwo.setText("Matched Two: "+matchTwo());
    	 } else {
    		 lblLost.setText("Lost: "+lose());
    	 }
    	 lblWon.setText("Wins: "+win());
    		 
    	 }




	public int matchThree() {
 		++matchThree;
 		return matchThree;
 	}

 	/** Increments matchTwo by 1 and returns value. */
 	public int matchTwo() {
 		++matchTwo;
 		return matchTwo;
 	}

 	/** Increments lost by 1 and returns value. */
 	public int lose() {
 		++lost;
 		return lost;
 	}

 	/** Increments win by 1, increases progress bar and returns value. */
 	public int win() {
 		win = matchThree + matchTwo;
 		
 		return win;
   	       }
	    //spin pics            
	    private void setIcon(){
	        num1 = random.nextInt(8);
	        switch (num1){
        case 0:
	            btnSpinPictures.setIcon(iconPic1);
	            counter1 = counter 1+ 1;
	            break;
	        case 1:
            btnSpinPictures.setIcon(iconPic2);
	                        counter1 = counter 1+ 1;
	            break;
	        case 2:
	            btnSpinPictures.setIcon(iconPic3);
	            counter1 = counter 1+ 1;
	            break;
	        case 3:
	            btnSpinPictures.setIcon(iconPic4);
	            counter1 = counter 1+ 1;
            break;
	        case 4:
	            btnSpinPictures.setIcon(iconPic5);
	            counter1 = counter 1+ 1;
	            break;
	        case 5:
	            btnSpinPictures.setIcon(iconPic6);
	            counter1 = counter 1+ 1;
	            break;
	        case 6:
            btnSpinPictures.setIcon(iconPic7);
	            counter1 = counter 1+ 1;

            break;
	        case 7:
	            btnSpinPictures.setIcon(iconPic8);
	            counter1 = counter 1+ 1;
	            break;
        }
	    } 
	    private void setIcon2(){
        num2 = random.nextInt(8);
	        switch (num2){
        case 0:
	            btnSpinPictures2.setIcon(iconPic1);
	            counter2 = counter2 + 1;
	            break;
	        case 1:
	            btnSpinPictures2.setIcon(iconPic2);
           counter2 = counter2 + 1;
	            break;
        case 2:
	            btnSpinPictures2.setIcon(iconPic3);
	counter2 = counter2 + 1;

	            break;
	        case 3:
	            btnSpinPictures2.setIcon(iconPic4);
	counter2 = counter2 + 1;
	            break;
	        case 4:
	            btnSpinPictures2.setIcon(iconPic5);
	counter2 = counter2 + 1;
	            break;
	        case 5:
	            btnSpinPictures2.setIcon(iconPic6);
	counter2 = counter2 + 1;

	            break;
	        case 6:
            btnSpinPictures2.setIcon(iconPic7);
	counter2 = counter2 + 1;
	            break;
	        case 7:
	            btnSpinPictures2.setIcon(iconPic8);
	counter2 = counter2 + 1;
	            break;
	        } 
	    }              
	    private void setIcon3(){
	        num3 = random.nextInt(8);
	        switch (num3){
	        case 0:
	            btnSpinPictures3.setIcon(iconPic1);
	counter3 = counter3 + 1;
	            break;
	        case 1:
	            btnSpinPictures3.setIcon(iconPic2);
	counter3 = counter3 + 1;
	            break;
	        case 2:
            btnSpinPictures3.setIcon(iconPic3);
	counter3 = counter3 + 1;
	            break;
	        case 3:
	            btnSpinPictures3.setIcon(iconPic4);
	counter3 = counter3 + 1;
	            break;
	        case 4:
	            btnSpinPictures3.setIcon(iconPic5);
	counter3 = counter3 + 1;
	            break;
	        case 5:
	            btnSpinPictures3.setIcon(iconPic6);
	            counter3 = counter3 + 1;
	            break;
	        case 6:
	            btnSpinPictures3.setIcon(iconPic7);
	counter3 = counter3 + 1;
	            break;
	        case 7:
	            btnSpinPictures3.setIcon(iconPic8);
            counter3 = counter3 + 1;
	            break;
	        }
	        {if (counter3 >=100);
	        Timer1.stop();
Timer3.stop();
Timer2.stop();
	        } }}





I havnt figured out how to do a nudge but do i have to create another "private void"?

Is This A Good Question/Topic? 0
  • +

Replies To: Buttons how do they work?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,652
  • Joined: 27-December 08

Re: Buttons how do they work?

Posted 07 December 2012 - 06:22 AM

Quote

I havnt figured out how to do a nudge but do i have to create another "private void"?

Methods are good to use for organizing your code. You probably don't "have" to create another method, but it would probably be a good idea for code organization.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Buttons how do they work?

Posted 07 December 2012 - 12:43 PM

         case 0:  
                 btnSpinPictures.setIcon(iconPic1);  
                 counter1 = counter 1+ 1;  
                 break;  

          case 1:  


better to learn how to use an array. The whole switch would becomde

btnSpinPictures.setIcon[iconPic[num1]);
counter1 = counter1 + 1;



actually even now the
counter1 = counter1 + 1;
can be removed from every case: and put at the end
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1