JButton help
Hey everyone, I have a program that will be a slot game. basically what I need help with is I have a bet max button that should set the bet to 3. Then another button to spin the reels. When the spin button is pressed, the bet amout should be subtracted from the total credits. When I run the program and press the bet max button and then spin, my credits display correctly at 97 (origionlly set to 100). The second time I press the spin button, the credits display -296. Im not sure what I did wrong can someone look through my code and find my error. thx for the help.
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import javax.swing.JOptionPane;
import java.awt.event.*;
publicclass SpudSlotTestextends JComponent
{
Image background =new ImageIcon("background1.png").getImage();
Image[] images =new Image[10];
int x = (int)(Math.random()*10);
int y = (int)(Math.random()*10);
int z = (int)(Math.random()*10);
int frame = x;
int frame2= y;
int frame3= z;
int bet = 0;
int credits = 100;
int win = 0;
int spinCounter = 0;
ImageIcon change =new ImageIcon("change.png");
JButton button =new JButton(change);
ImageIcon cashout =new ImageIcon("cashout.png");
JButton button2 =new JButton(cashout);
ImageIcon betone =new ImageIcon("betone.png");
JButton button3 =new JButton(betone);
ImageIcon betmax =new ImageIcon("betmax.png");
JButton button4 =new JButton(betmax);
ImageIcon spin =new ImageIcon("spin.png");
JButton button5 =new JButton(spin);
publicvoid paintComponent(Graphics g)
{
button.setSize(112, 95);
button.setLocation(363, 538);
button.addActionListener(new CashierListener());
add(button);
button2.setSize(112, 95);
button2.setLocation(521, 538);
button2.addActionListener(new CashoutListener());
add(button2);
button3.setSize(112, 95);
button3.setLocation(667, 538);
button3.addActionListener(new BetOneListener());
add(button3);
button4.setSize(112, 95);
button4.setLocation(813, 538);
button4.addActionListener(new BetMaxListener());
add(button4);
button5.setSize(171, 150);
button5.setLocation(958, 487);
button5.addActionListener(new MyStartListener());
add(button5);
Image image = images[frame];
Image image2 = images[frame2];
Image image3 = images[frame3];
g.drawImage(background, 0, 0,this);
g.drawImage(image, 199, 150,this);
g.drawImage(image2, 475, 150,this);
g.drawImage(image3, 750, 150,this);
g.setColor(Color.red);
g.setFont(new Font("Garamond", Font.BOLD, 50));
g.drawString("" + bet, 770, 428);
g.drawString("" + credits, 225, 428);
g.drawString("" + win, 871, 428);
}
publicstaticvoid main(String[] args)
{
SpudSlotTest slot =new SpudSlotTest();
JFrame frame =new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 800);
frame.getContentPane().add(slot);
frame.setVisible(true);
}
publicclass MyStartListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
credits = credits - bet;
new Thread()
{
publicvoid run()
{
images[0] =new ImageIcon(
"spud1.png").getImage();
images[1] =new ImageIcon(
"blank2.png").getImage();
images[2] =new ImageIcon(
"cherries.png").getImage();
images[3] =new ImageIcon(
"blank3.png").getImage();
images[4] =new ImageIcon(
"triplebone.png").getImage();
images[5] =new ImageIcon(
"blank4.png").getImage();
images[6] =new ImageIcon(
"doublebone.png").getImage();
images[7] =new ImageIcon(
"blank5.png").getImage();
images[8] =new ImageIcon(
"bone.png").getImage();
images[9] =new ImageIcon(
"blank6.png").getImage();
int delay = 10;
try
{
int t = (int)(Math.random() * 10);
while (t<150)
{
int a = (int)(Math.random()*10);
int s = (int)(Math.random()*10);
int d = (int)(Math.random()*10);
frame = (frame+a)%images.length;
frame2 = (frame2+s)%images.length;
frame3 = (frame3+d)%images.length;
Thread.sleep(delay);
repaint();
++t;
}
++spinCounter;
//JOptionPane.showMessageDialog(null, "" + spinCounter);
wait();
}
catch (Exception e){}
//PayoutTest();
}
}.start();
}
}
publicclass BetOneListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.setFont(new Font("Garamond", Font.BOLD, 50));
if (bet<3)
{
g.drawString("" + bet, 770, 428);
++bet;
g.setColor(Color.red);
g.drawString("" + bet, 770, 428);
}
else
{
g.drawString("" + bet, 770, 428);
g.setColor(Color.red);
g.drawString("3", 770, 428);
}
}
}
publicclass BetMaxListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.setFont(new Font("Garamond", Font.BOLD, 50));
g.drawString("" + bet, 770, 428);
g.setColor(Color.red);
bet = 3;
g.drawString("" + bet, 770, 428);
}
}
publicclass CashoutListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
publicclass CashierListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}

