-
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();
}
}
-
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
}
}
-
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-05-2006 at 11:11 PM.
Similar Threads
-
By manik_gopal in forum Java
Replies: 4
Last Post: 04-27-2006, 07:01 AM
-
Replies: 5
Last Post: 04-16-2005, 09:06 AM
-
By Blake Darche in forum Java
Replies: 0
Last Post: 04-08-2002, 07:12 PM
-
Replies: 1
Last Post: 10-29-2000, 04:51 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks