Another Thread question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Another Thread question

  1. #1
    Join Date
    Jul 2006
    Posts
    5

    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(
    "spud1.jpg").getImage();
    images[1] = new ImageIcon(
    "blank2.jpg").getImage();
    images[2] = new ImageIcon(
    "cherries.jpg").getImage();
    images[3] = new ImageIcon(
    "blank3.jpg").getImage();
    images[4] = new ImageIcon(
    "triplebone.jpg").getImage();
    images[5] = new ImageIcon(
    "blank4.jpg").getImage();
    images[6] = new ImageIcon(
    "doublebone.jpg").getImage();
    images[7] = new ImageIcon(
    "blank5.jpg").getImage();
    images[8] = new ImageIcon(
    "bone.jpg").getImage();
    images[9] = new ImageIcon(
    "blank6.jpg").getImage();
    int delay = 7;

    try
    {
    while (true)
    {
    synchronized(this)
    {
    int v = (int)(Math.random() * 10);
    while (v<100)
    {
    frame = (frame+1)%images.length;
    repaint();
    Thread.sleep(delay);
    ++v;
    }
    wait();
    }
    }
    }
    catch (Exception e){}
    }

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

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



    }
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    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.

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

  3. #3
    Join Date
    Jul 2006
    Posts
    5

    JButtons

    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-06-2006 at 12:11 AM.

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2006, 08:01 AM
  2. A question about Thread
    By nickiss in forum Java
    Replies: 5
    Last Post: 04-16-2005, 10:06 AM
  3. Help on Thread question about yielding-sleeping!
    By Blake Darche in forum Java
    Replies: 0
    Last Post: 04-08-2002, 08:12 PM
  4. Replies: 1
    Last Post: 10-29-2000, 05:51 PM
  5. Replies: 2
    Last Post: 10-27-2000, 04: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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