Solved

creating a hold button for a java slot machine

Posted on 2004-03-24
6
1,009 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 63
session migration servlets 2 38
Configure a Bean in an XML file 4 32
Java string replace 11 48
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

803 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