Solved

creating a hold button for a java slot machine

Posted on 2004-03-24
6
1,007 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

20 Experts available now in Live!

Get 1:1 Help Now