Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

creating a hold button for a java slot machine

Posted on 2004-03-24
6
Medium Priority
?
1,020 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 14

Accepted Solution

by:
Tommy Braas earned 500 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…
Suggested Courses

715 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