Solved

creating a hold button for a java slot machine

Posted on 2004-03-24
6
1,013 Views
Last Modified: 2007-12-19
Hi experts
i am currently designing a slot machine for a college project. i have manged to create the GUI and a few buttons.

i have a randomNumberGenerator class that creates 3 random numbers from 1 - 8. for example this how i generated one of my random numbers :-
public int oneRandomNum(){
    int randNum1;

  randNum1 = 1 + (int)(Math.random()*8);

return(randNum1);

}// this is done three times to generate 3 random numbers

Each number is assigned to an image using a switch statement in ( void btnStart_actionPerformed(ActionEvent e). So there are three switch statements one for each reel.

Each time the start button is clicked it loads the random images onto the 3 reels.  

Im having difficulty getting the hold buttons to work. Somehow i have to pause the randomNumberGenerator class for each reel individually.

any help would be appreciated
0
Comment
Question by:bagz1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10669901
Add a couple of boolean variables tracking hold state. Add one for each wheel. Then add if statements for each wheel in your btnStart_actionPerformed(ActionEvent e) to determine if a certain wheel should be spun or not,
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10669918
Of course, you would have to control the state of those booleans with the help of one hold button per wheel, and don't forget to reset the values of those variables after a run.
0
 

Author Comment

by:bagz1
ID: 10670119
is there a specific command that i could use to hold a reel boolean or not?
at the moment my reels are just empty buttons that images are loaded on to when the start button is clicked.  hers the start button code
void btnStart_actionPerformed(ActionEvent e) {
    int resultOne, resultTwo, resultThree;
     
 if(e.getSource() == btnStart) {
 resultOne = rn.oneRandomNum()
 
   switch (resultOne)
    {
        case 1:
          jBReel1.setIcon(iconBanana) ;
                             break;

        case 2:
          jBReel1.setIcon(iconCherry) ;
                                 break;

        case 3:
          jBReel1.setIcon(iconApple) ;
                               break;

        case 4:
          jBReel1.setIcon(iconSun) ;
                       break;
        case 5:
          jBReel1.setIcon(iconCoke) ;
                       break;

        case 6:
          jBReel1.setIcon(iconStar) ;
                      break;

        case 7:
          jBReel1.setIcon(iconPie) ;
                        break;

        case 8:
          jBReel1.setIcon(iconCoolade) ;
                      break;
    }

    resultTwo = rn.oneRandomNumtwo();


  switch (resultTwo)
  {
      case 1:
        jBReel2.setIcon(iconBanana) ;
                  break;

      case 2:
        jBReel2.setIcon(iconCherry) ;
                    break;

       case 3:
         jBReel2.setIcon(iconApple) ;
                    break;

       case 4:
         jBReel2.setIcon(iconSun) ;
                  break;


       case 5:
         jBReel2.setIcon(iconCoke) ;
                   break;

       case 6:
         jBReel2.setIcon(iconStar) ;
                             break;

       case 7:
         jBReel2.setIcon(iconPie) ;
                       break;

    case 8:
      jBReel2.setIcon(iconCoolade) ;
                     break;
  }


  resultThree = rn.oneRandomNumThree();


switch (resultThree)
{
    case 1:
      jBReel3.setIcon(iconBanana) ;
                    break;

    case 2:
      jBReel3.setIcon(iconCherry) ;
                     break;

     case 3:
       jBReel3.setIcon(iconApple) ;
                      break;
     case 4:
       jBReel3.setIcon(iconSun) ;
                  break;

     case 5:
       jBReel3.setIcon(iconCoke) ;
                  break;

     case 6:
       jBReel3.setIcon(iconStar) ;
                      break;

     case 7:
       jBReel3.setIcon(iconPie) ;
                      break;
     case 8:
       jBReel3.setIcon(iconCoolade) ;
                       break;

       }

    }

  }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 total points
ID: 10670189
You would want to split your code up a bit as you have a lot of code duplication. Create a method that sets the icon for a button! E.g.

public void setButtonIcon(JButton bttn, int iconNumber) {

  switch (iconNumber)
  {
      case 1:
        bttn.setIcon(iconBanana) ;
                  break;

      case 2:
        bttn.setIcon(iconCherry) ;
                    break;

       case 3:
         bttn.setIcon(iconApple) ;
                    break;

       case 4:
         bttn.setIcon(iconSun) ;
                  break;


       case 5:
         bttn.setIcon(iconCoke) ;
                   break;

       case 6:
         bttn.setIcon(iconStar) ;
                             break;

       case 7:
         bttn.setIcon(iconPie) ;
                       break;

    case 8:
      bttn.setIcon(iconCoolade) ;
                     break;
  }
}

I think you'll be able to simplify your btnStart_actionPerformed method by issuing calls to this method for each button, but only of there is no hold on it. You will need to add methods similar to btnStart_actionPerformed to each icon button, and have that method set/unset the state of the boolean hold value for that button.
0
 

Author Comment

by:bagz1
ID: 10670262
Cheers orangehead911!!!!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10670300
My pleasure! :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 37
map related example 6 59
jdbc error in jsp application 20 74
Is Applet the way to go for my drag and drop system? 8 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question