-1

I am creating a slot machine using eclipse.
I am trying to get the "winnings" JTextField to be updated in a way so that when the random images have been selected it adds to the number that is already displayed in the JTextField as opposed to what it is doing at the minute which is just displaying how much was won on that particular spin.
I am also struggling to set a code for when noting is won, nothing is added. Any help would be appreciated. Cheers

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.pink);
    btncollect.setForeground(Color.white);

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

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

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

    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.orange);
    txtwinnings.setForeground(Color.black);

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

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

    iconpic1 = new ImageIcon("src/resources/app.png");
    iconpic2 = new ImageIcon("src/resources/str.png");
    iconpic3 = new ImageIcon("src/resources/ban.png");
    iconpic4 = new ImageIcon("src/resources/che.png");
    iconpic5 = new ImageIcon("src/resources/pin.png");
    iconpic6 = new ImageIcon("src/resources/org.png");
    iconpic7 = new ImageIcon("src/resources/grp.png");
    iconpic8 = new ImageIcon("src/resources/lem.png");

    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,800);
}

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

    if(e.getSource()==btninsertfunds){
        txtbank.setForeground(Color.black);
        bank=bank +25 ;
        txtbank.setText("Bank = " + bank);

        if (bank>=50)
            btninsertfunds.setEnabled(true);

        if(bank>=400)
            btninsertfunds.setEnabled(false);
        {

            if (bank>=50)
                btnspin.setEnabled(true);
            if (bank<50)
                btnspin.setEnabled(false);  

        }
    }   
        if (e.getSource()==btnspin){

            bank = bank -50 ;
            txtbank.setText("Bank = " );
            System.out.println("here");
            setIcon1();setIcon2();setIcon3();

            if(a==b && a==c ){
                txtwinnings.setText("winnings = 100");
            }
            else
                if (a==b || a==c || b==c)
                {
                    txtwinnings.setText("winnings = 50"); 
                }
                else
                    if (a== 3 || b== 3 ||c== 3){
                        txtwinnings.setText("winnings = 20");

                    }


        }   



        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;


        }

        }
   }
Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jan 23 '15 at 05:25

1 Answers1

1

Maintain a separate variable for the winnings, perhaps as a double. Calculate the winnings each round and add this to the variable, then use something like Double.toString or NumberFormat.getNumberInstance().format(...) to convert the value to a String and set it as the text for the text field

For example...

if(a==b && a==c ){
    winnings += 100;
}
else if (a==b || a==c || b==c)
{
    winnings += 50;
}
else if (a== 3 || b== 3 ||c== 3){
    winnings += 20;
}
txtwinnings.setText(NumberFormat.getInstance().format(winnings));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for your quick response. I am really new to java and programming generally could you give me an example of how to write this in regards to my code? I`d be very thankful – user3256504 Jan 23 '15 at 05:28
  • Thank you. However when i type this in it is saying syntax error insert")" to complete expression. basically asking for an extra closed bracket at the end but when i do this and run the program the winnings has changed to a zero and isnt updated when it should be it just stays at zero? Thanks again for your continued help – user3256504 Jan 23 '15 at 06:26
  • Do you mind helping me with another issue I`m having with my code now? I would like the machine to give 20 winnings if the user hits iconpic3 on their spin but I cant seem to work it out. thats what i`m attempting to do at the end of that code there. the "else if (a==3 || b==3 || c==3)} – user3256504 Jan 23 '15 at 06:33
  • Isn't `a==2 || b==2 || c==2` (based on your `switch` statements). Also, you may not want that as a `if-else` branch, but a stand alone `if`, assuming you want to add an additional `20`. Otherwise, you may need to run it as the first `if` condition – MadProgrammer Jan 23 '15 at 06:36
  • The winnings text field is now being added to after each spin but incorrectly for what i want it to and i cant understand why. I basically want the winnings to go up by 100 if all 3 images match. 50 if 2 match and 20 if they get one cherry on any of the images. So if they got 2 bananas and a cherry the winnings should be 70. or 3 cherries 160. Any help you could give me to get this working correctly would be much appreciated. thankyou – user3256504 Jan 23 '15 at 06:50
  • the cherry image is set as iconpic4 – user3256504 Jan 23 '15 at 06:52
  • Add `winnings = 0` before the start of the `if` statement – MadProgrammer Jan 23 '15 at 06:52
  • thanks that has now meant the machine is producing the correct amount of winnings which is great! but is no longer being added to after each spin if you know what i mean. its just giving the correct amount of winnings from that spin. would there be a simple way of me adding a message appearing after each spin saying how much that spin had won while the winningstextfield just gets continuously added to? – user3256504 Jan 23 '15 at 07:01
  • So you need to values...? One for the spin and one for the tally? – MadProgrammer Jan 23 '15 at 07:03
  • Yes preferrably. Is there a simple way of adding this in? Also I`ve actually just realised that the winnings being produced arent 100%correct. 3 images=100 is working fine. 2 images=50 is working fine. one cherry=20 is working fine the only thing that isnt for example is when two bananas and one cherry come up the winnings should be 2images=50 + one cherry=20 therefore 70 but its just producing 50 atm – user3256504 Jan 23 '15 at 07:12
  • So you will need to variables, one for the "game" winnings and one for the total. You would calculate the winnings for this round in one variable when done, add it to the total – MadProgrammer Jan 23 '15 at 08:57
  • Yes that sounds perfect would this involve a lot of coding? I`m on a pretty short time-limit. Could you explain how I`d go about doing that? Thanks if you could. You`ve been very helpful so far – user3256504 Jan 23 '15 at 09:28
  • Great. how would i go about doing whats left? Inserting some sort of "tally label" or something? how would i code that? – user3256504 Jan 23 '15 at 09:45
  • Well, you need another `int` variable to maintain the `tally`. You need another label or text field for the UI. Other then that, it's pretty much the same process you've been following, update the `tally`, convert it to a `String` and update the UI component... – MadProgrammer Jan 23 '15 at 09:53
  • so ive added a new int called winningstally and a new label called totalwinnings but not sure exactly what you mean with the rest of it? How and where would i write the code to link it all? thanks again – user3256504 Jan 23 '15 at 09:58
  • would it be like totalwinnings.setText(NumberFormat.getInstance().format(winnings)); in the btn spin section? – user3256504 Jan 23 '15 at 10:02
  • tried that but for some reason the label isn't even appearing on the application? can you have alook at this code and see if ive done something wrong? – user3256504 Jan 23 '15 at 10:59
  • if (e.getSource()==btnspin){ bank = bank -50 ; txtbank.setText("Bank = " ); setIcon1();setIcon2();setIcon3(); winnings = 0; if(a==b && a==c ){ winnings += 100; } else if (a==b || a==c || b==c) { winnings +=50; } else if (a== 3|| b== 3 ||c== 3){ winnings +=20; } txtwinnings.setText(NumberFormat.getInstance().format(winnings)); totalwinnings.setText(NumberFormat.getInstance().format(winnings)); – user3256504 Jan 23 '15 at 11:00
  • Actually i`ve got it appearing on the application now but its just doing the same as the other one. As in just producing the amount of winnings off the latest spin then replacing it after the next spin as opposed to adding to it? – user3256504 Jan 23 '15 at 11:13
  • ah ok. what should i use instead? – user3256504 Jan 23 '15 at 11:37
  • I`ve used winningstally as opposed to winnings but now that figure just stays at 0? Im guessing i need to add a little bit of code in somewhere but not sure what? I`ve tried a couple of things but they`ve not worked. Any ideas? would be much appreciated thanks – user3256504 Jan 23 '15 at 11:58