Solved

creating a hold button for a java slot machine

Posted on 2004-03-24
6
1,005 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
  • 4
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 total points
Comment Utility
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
Comment Utility
Cheers orangehead911!!!!
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
My pleasure! :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now