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);

}

}

}

[11258 byte] By [Dr_Spuda] at [2007-10-3 2:58:40]
# 1

Hi there Dr_Spud

Quickly skimming through your code, I believe your problem lies in the call to repaint() in your MyStartListener, it generates a recursive loop that keeps adding ActionListeners. Also, calls to repaint() should be done in the EVT, not in your worker thread. Read up a bit on Threads and Swing and change your program structure around to avoid this loop. Cheers!

Roswella at 2007-7-14 20:48:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

Before we get to the real problem we have some design issues to discuss:

a) Your paintComponent(...) method should not be painting all the components. You should be using JLabels to display the text and images you want painted. Then you use LayoutManagers to position the labels the way you want them displayed. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url]. You can mix and match layout managers to get the desired effect.

b) don't hardcode a frame size. You size of 1280 x 800 doesn't work for me since I run at 1024 x 768. If you want to maximize the frame then you can use:

frame.setExtendedState (JFrame.MAXIMIZED_BOTH);

Even better is to use LayoutManagers and then just use the pack() method so the frame will size to the preferred size of all your components.

c) Don't load images in you ActionListener. Every time the button is clicked you reload the images.

d) don't invoke repaint() in your ActionListener code. This will repaint the entire frame. All you want to do is use a JLabel and then use setIcon(...) to change the image. The label will repaint itself. This way you don't need to keep an Image array of the current image.

e) When you catch an exception what good does it do when you don't display an error message? How do you expect to debug your program. Take a look at your try/catch block in the actionPerformed method.

Finally, the answer to your original question.

In your paintComponent(...) method you keep adding the ActionListener to your button. Since your Thread loops 150 times, 150 listeners get added, the next time more get added.......

camickra at 2007-7-14 20:48:11 > top of Java-index,Desktop,Core GUI APIs...