Java trouble with threads
I am trying to create a slot machine in java. In this slot machine I have completed the basic beginning of the project. I have the animators that lands on a random space (cherry, blank, seven, etc), the background, buttons to start and bet in the opening, however I have to figure out the ending of the slot; how to make the results of the slot machine appear without the user clicking a button. To do this I figure the best way is to dive into the realm of threads. However when I tried to create a simple thread, the animators stopped working and when I took the thread out, the animators did work. It is quite the conundrum.
Here is some of the code if that helps explanation :public class SlotMachineOpeningGraphic extends JPanel implements Runnable
{
JLayeredPane layeredPane= new JLayeredPane ();
JFrame frame = new JFrame();
public Thread thread;
public volatile boolean running = false;
Player plyr = new Player ();
public static final long startTime = System.currentTimeMillis ();
JLabel numberBet = new JLabel ("" + plyr.getBet()); //things that change on JFrame
JLabel numberAccount = new JLabel (""+ plyr.getAccount ());
JButton betButton = new JButton ("Bet ++");
JButton playAgain = new JButton ("Start");
public SlotMachineOpeningGraphic()
{
layeredPane.setPreferredSize(new Dimension(750, 460));//changes size to image with border for buttons
//andlabels
ImageIcon bG = new ImageIcon ("/Users/Documents/slotmachine.png");//background file
JLabel backGround = new JLabel (bG);
backGround.setBounds (70,0, bG.getIconWidth(), bG.getIconHeight());//won't display if you do not set bounds
layeredPane.add (backGround, new Integer (0));//add to first layer
/*add buttons and labels to give user options
* and information, placed in layer 2 */
playAgain.setBounds (110,420, 100, 25);
layeredPane.add (pl开发者_开发知识库ayAgain, new Integer (2));
playAgain.addActionListener (new Start());
betButton.setBounds (320,420, 100, 25);
layeredPane.add (betButton, new Integer (2));
betButton.addActionListener (new SlotBB ());
JButton mainMenu = new JButton ("Main Menu");
mainMenu.setBounds (520,420, 100, 25);
layeredPane.add (mainMenu, new Integer (2));
long checkTime = System.currentTimeMillis ();
System.out.println ("Total execution time : " + (checkTime - startTime));
JLabel bet = new JLabel ("Bet:");
bet.setBounds (620, 320, 100, 30);
Font font = new Font ("Corsiva Hebrew",bet.getFont().getStyle(),30); //desired font & size
bet.setFont (font);
layeredPane.add (bet, new Integer (2));
numberBet.setBounds (620, 340, 100, 30);
layeredPane.add (numberBet, new Integer (2));
numberBet.setFont (font);
JLabel account = new JLabel ("Account: ");
account.setBounds (5, 320, 150, 30);
account.setFont (font);
layeredPane.add (account, new Integer (2));
numberAccount.setBounds (5, 340, 150, 30);
numberAccount.setFont (font);
layeredPane.add (numberAccount, new Integer (2));
add(layeredPane);
JComponent newContentPane = layeredPane;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setBackground (Color.white);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void run ()
{
try {
Thread.sleep (20);
System.out.println ("works");
}
catch (Exception e) {
System.out.println ("doesn't work");
}
}
class Start extends JPanel implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
SlotAnimator a0 = new SlotAnimator (40);
a0.setBounds(155, 85, 100, 90);
layeredPane.add (a0, new Integer (1));
SlotAnimator a1 = new SlotAnimator (85);
a1.setBounds(320, 85, 100, 90);
layeredPane.add (a1, new Integer (1));
SlotAnimator a2 = new SlotAnimator (135);
a2.setBounds(470, 85, 100, 90);
layeredPane.add (a2, new Integer (1));
playAgain.setText ("play again?");
hearSound();
thread = new Thread (new SlotMachineOpeningGraphic ());
thread.start ();
}
}
public static void main (String [] args)
{
new SlotMachineOpeningGraphic();
}
}
Any suggestions on an approach? It would be greatly appreciated!
@HFOE is right about using javax.swing.Timer
, which manages its own thread to generate periodic GUI events in a safe way. You may also want to look at adopting the Model-View-Controller pattern, exemplified here.
This is at least one of your mistakes:
thread = new Thread(new SlotMachineOpeningGraphic());
You are creating a new SlotMachineOpeningGraphic object, one that is completely distinct from the original one. Don't do this. If you had to use a SlotMachineOpeningGraphic object here, then you should use a reference to the original class that already exists and holds the Start private inner class, such as:
thread = new Thread(SlotMachineOpeningGraphic.this);
So this isn't a "threading" issue per se, but rather a reference issue -- you use the wrong reference as the Runnable for your thread.
But I strongly advise you against using your GUI classes for ActionListeners, OtherListeners or Runnables as you're asking the class to do too much. Better to use an anonymous inner class or a separate stand-alone class.
You could try to use a Future or Runnable classes for doing the processing, and retriving their result as soon as they finish. Just make sure to put those threads in a Thread pool!
精彩评论