DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006

    Another Thread question

    Im writing a simple slot machine so far I get a JFrame window to display a simple animation from jpgs that I made the wait() method will let me stop the reel on a randomized pic but I need some help with how to read what pic the reel stopped on so that when I put 3 reels in I can run a test for all the payouts. At this point my program just gets stuck inside my thread. Someone told me that I need to kill the thread but when I do my JFrame window just flashes on the screen. I need a way to kill the thread while still displaying the JFrame window. Please help.. here is my code.

    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;

    public class SpudSlotTest extends JComponent implements Runnable
    Image[] images = new Image[10];
    int frame = 0;

    public void paint(Graphics g)
    Image image = images[frame];
    if (image != null)
    g.drawImage(image, 0, 0, this);

    public void run()
    images[0] = new ImageIcon(
    images[1] = new ImageIcon(
    images[2] = new ImageIcon(
    images[3] = new ImageIcon(
    images[4] = new ImageIcon(
    images[5] = new ImageIcon(
    images[6] = new ImageIcon(
    images[7] = new ImageIcon(
    images[8] = new ImageIcon(
    images[9] = new ImageIcon(
    int delay = 7;

    while (true)
    int v = (int)(Math.random() * 10);
    while (v<100)
    frame = (frame+1)%images.length;
    catch (Exception e){}

    public static void main(String[] args)
    AnimApp app = new AnimApp();
    JFrame frame = new JFrame();
    frame.setSize(300, 300);

    (new Thread(app)).start();


  2. #2
    Join Date
    Mar 2004
    The thread only needs to last as long as the animation, so get rid of that while(true) which keeps the thread alive indefinately.

    Personally, I'd use a timer to control the animation frames. It'll be more efficient when having 3 reels of varying speeds rather than 3 threads.

    public void run()
         delay = 0
         while (delay < 500)  //0.5 seconds
              frame = (int)Math.random()*10;
              delay += 2;  //gradually flip to new image slower and slower

  3. #3
    Join Date
    Jul 2006


    Thanks for your input and help, your advise helped. I have run into another problem with my buttons now. Since my last post I have added the other 2 reels and animated them along with a background image. Now Im trying to put the buttons that will control the run method. Since my buttons have to be in precise locations I need to set the frame layout to null, then I used setLocation() to position the button. But by setting the layout to null I erased all my images except my new button. Can someone help me have my cake and eat it too. Someone told me that setting the layout to null was unecessary and there was a layout manager that allows you to set exact positions and sizes of JButtons but I cant find it. Im trying to save space by not displaying my code again but if you need to see it let me know and I will post it.
    Last edited by Dr Spud; 08-05-2006 at 11:11 PM.

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2006, 07:01 AM
  2. A question about Thread
    By nickiss in forum Java
    Replies: 5
    Last Post: 04-16-2005, 09:06 AM
  3. Help on Thread question about yielding-sleeping!
    By Blake Darche in forum Java
    Replies: 0
    Last Post: 04-08-2002, 07:12 PM
  4. Replies: 1
    Last Post: 10-29-2000, 04:51 PM
  5. Replies: 2
    Last Post: 10-27-2000, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.