Computer Science Canada

Help on How to Make Slot Machine Graphics Appear Spinning and then Stop Randomly

Author:  unicorn-ellie [ Sun Dec 28, 2014 1:00 pm ]
Post subject:  Help on How to Make Slot Machine Graphics Appear Spinning and then Stop Randomly

Hi, I am a student who is very new to Java and I am coding a project--a slot machine.
As of now, I am having trouble making my slot graphics stop randomly. I am trying to use a timer that will execute the task, and then use a random int generator for the delay/time it will stop, however, this is not working for me.
Any input would be greatly appreciated!

Here is what I have tried so far (this is for one slot):

code:

import java.applet.*;   // allows access to MediaTracker
import java.awt.*;
import hsa.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;

public class ApplePic
{
    static Timer timer;
    static final int MAX_TIME = 1;
    static final long PERIOD = 1;
    static Console c;
    static Image picture;                   // image class
    static int picWidth, picHeight;                 // store width/height

    private static final String PICTURE_PATH = "Fruits.jpg";

    public static void loadImage ()         // This sets up the image
    {

        picture = Toolkit.getDefaultToolkit ().getImage (PICTURE_PATH);       // Load the image.


        // Now, it can actually take some time to load the image, and
        // it could fail (image not found, etc).  The following checks for  all that.
        MediaTracker tracker = new MediaTracker (new Frame ());

        tracker.addImage (picture, 0);  // Add the picture to the list of images to be tracked

        // Wait until all the images are loaded.  This can throw an
        // InterruptedException although it's not likely, so we ignore  it if it occurs.

        try
        {
            tracker.waitForAll ();
        }
        catch (InterruptedException e)
        {
        }

        if (tracker.isErrorAny ())      // If there were any errors then abort theprogram
        {
            c.println ("Couldn't load " + PICTURE_PATH);
            return;
        }
        picWidth = picture.getWidth (null);     // Initialize the picture size
        picHeight = picture.getHeight (null);
    } // init method


    public ApplePic ()      // Create the console
    {
        c = new Console ();
    }


    public static void main (String[] args)
    {
        ApplePic a = new ApplePic ();
 
        // Load the picture

        loadImage ();
       
        Random rd = new Random ();
        final long time = 1 + rd.nextInt (MAX_TIME);
        final Timer timer = new Timer ();
        timer.schedule (new TimerTask ()
        {
            public void run ()
            {
                // // Initialize x, y, dx, dy
                int x, y;                       
                int dx, dy;
                x = 200;
                y = -100;
                dx = 3;
                dy = 3;

                do
                {
                    for (y = -500 ; y < 500 ; y++)
                    {
                        try
                        {
                            Thread.sleep (5);
                        }
                        catch (InterruptedException e)
                        {
                        }
           
                        x = x;
                        y++;
                        dy = +dy;
                        dx = -dx;
                   
                        c.clearRect (x, y, picWidth, picHeight);

                        x += dx;
                        y += dy;

                        c.drawImage (picture, x, y, null);
                       
                    }

                }
                while (y - dy > 0);
            }
            timer.cancel ();
        }
        , 0, PERIOD);

    } // main method
}


: