2017-11-17 7 views
-2

私は学校用のこのJavaスロットマシンを作り、それをコード化しました。私はJFramesを使用していますが、画面に表示する画像を取得できません。スロット[] jLabelに問題があるようです。スロットは、3つの異なるリールを保持する配列です。問題がgridlayoutと関係しているかどうかは分かりません。混乱したImスロットマシンJavaゲーム

package slots; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.border.TitledBorder; 

public class SlotMachineProgram extends JFrame { 
    private int total = 2000; //this is the max bet you can have 
    private int bet = 100; //default bet amount 
    private int maxWin; //this variable stores the max wins 

    private String[] symbols = { 
    "Cherry", 
    "Bear", 
    "Diamond", 
    "Penguin", 
    "Star", 
    "Shoe", 
    "Banana", 
    "Orange", 
    "Apple" 
    }; //symbols that are available for the slot machine 

    //amount winning 
    int[] gotTwo = { 
    30, 
    16, 
    15, 
    12, 
    11, 
    10, 
    9, 
    7, 
    5 
    }; //Prize moneys for two matches 
    int[] gotThree = { 
    60, 
    32, 
    30, 
    24, 
    22, 
    20, 
    18, 
    14, 
    10 
    }; //prize money for three matches 

    public JPanel slotPanel = new JPanel(new GridLayout(1, 3)); //creates a grid that holds the three slot options 
    public JLabel[] slot = new JLabel[3]; //create the three slots 

    //LABELS 
    private JLabel amount = new JLabel("Total: $" + total); //this is the label for the total amount 
    private JLabel betLabel = new JLabel("Bet: $" + bet); //this is the label for the bet amount 

    //BUTTONS 
    private JButton add50Button = new JButton("+50"); //creates a button called add50Button that adds 50 dollars to the bet 
    private JButton spin = new JButton("Spin"); //creates a button that spins the slot machine 
    private JButton maxBetButton = new JButton("Max"); //creates a button that makes the max amount the bet 
    private JButton subtract50Button = new JButton("-50"); //creates a button that subtracts 50 from bet 
    private JButton getBet = new JButton("Enter Bet"); //a button that pops up a new window that allows the user to type in a bet 

    public SlotMachineProgram() { //constructor 
    maxWin = total; //set win to total 

    //Coloring Buttons 
    spin.setBackground(Color.GREEN); //sets color of button to be green 
    spin.setOpaque(true); //Sets Button Opaque so it can be seen 
    maxBetButton.setBackground(Color.CYAN); //sets color of button to be green 
    maxBetButton.setOpaque(true); //Sets Button Opaque so it can be seen 
    add50Button.setBackground(Color.YELLOW); //sets color of button to be green 
    add50Button.setOpaque(true); //Sets Button Opaque so it can be seen 
    subtract50Button.setBackground(Color.MAGENTA); //sets color of button to be green 
    subtract50Button.setOpaque(true); //Sets Button Opaque so it can be seen 
    getBet.setBackground(Color.ORANGE); //sets color of button to be green 
    getBet.setOpaque(true); //Sets Button Opaque so it can be seen 

    //money panel 
    JPanel moneyPanel = new JPanel(new GridLayout(2, 1)); //creates a grid called money panel that is 2x1 that stores total and bet 
    moneyPanel.setBorder(new TitledBorder("Money")); //creates the title of the panel 
    moneyPanel.add(amount); //adds the amount to the panel 
    moneyPanel.add(betLabel); //adds the BetLabel to the panel 

    //button panel 
    JPanel buttonsPanel = new JPanel(new GridLayout(2, 1)); //creates a new panel called buttonsPanel that has all of the buttons 
    JPanel buttonTopPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2)); //this orients the buttons to the middle in a row 
    buttonsPanel.setBorder(new TitledBorder("    Buttons")); //sets the title of the buttons panel to buttons 

    buttonTopPanel.add(add50Button); //add the add50button to the button panel 
    buttonTopPanel.add(spin); //adds a spin button 
    buttonTopPanel.add(maxBetButton); //adds a max bet button 

    JPanel BottomPanel = new JPanel(); //creates a panel for the bottom of the screen 
    BottomPanel.add(subtract50Button); //adds the subtract 50 button 
    BottomPanel.add(getBet); //adds the get bet button 

    buttonsPanel.add(buttonTopPanel, BorderLayout.NORTH); //adds a border on the north edge 
    buttonsPanel.add(BottomPanel, BorderLayout.SOUTH); //adds a border on the south edge 

    // TODO 
    //set slots to their default image 
    slot[0] = new JLabel(new ImageIcon("/images/left.png")); 
    slot[1] = new JLabel(new ImageIcon("/images/middle.png")); 
    slot[2] = new JLabel(new ImageIcon("/images/right.png")); 


    //add Action listeners to bet buttons 
    add50Button.addActionListener(new ActionListener() { //action listener for the add 50 button 
     @Override public void actionPerformed(ActionEvent e) { //makes an action performed method 
     bet += 50; //increase bet by 50 if the button add50 is clicked 
     if (bet > total) bet = total; //else skip 
     betLabel.setText("Bet: $" + bet); //show the bet next to the bet label 
     } 
    }); 

    maxBetButton.addActionListener(new ActionListener() { //creates an action listener for the maxbetbutton 
     @Override public void actionPerformed(ActionEvent e) { //creates a method for the maxbetbutton's action listener 
     bet = total; //make bet the maxBet 
     betLabel.setText("Bet: $" + bet); //show bet next to the bet label 
     } 
    }); 

    subtract50Button.addActionListener(new ActionListener() { //creates an actionlistener for the subtract 50 button 
     @Override public void actionPerformed(ActionEvent e) { //creates an actionperformed method 
     bet -= 50; //subtracts the value of the bet by 50 
     if (bet < 0) bet = 0; //is the user tries to have a negative bet, set bet to 0 
     betLabel.setText("Bet: $" + bet); //display the value of the bet next to a bet label 
     } 
    }); 

    getBet.addActionListener(new ActionListener() { //create an actionlistener for the getbet button that allows the user to type in a custom amount 
     @Override public void actionPerformed(ActionEvent e) { // create an actionperformed method 
     String input; //creates a variables called input of type string. This is what is displayed on the screen 
     int betInput; //creates a variables called betInput of type int 
     //"How much do you want to bet" is the text that appears in the bet amount window. 
     //"Enter your bet amount" is the header on the bet window 
     input = JOptionPane.showInputDialog(null, 
      "How Much Do You Want To Bet? ", "Enter Your Bet Amount", 
      JOptionPane.QUESTION_MESSAGE); 
     betInput = Integer.parseInt(input); //set the user input to the betInput 
     if (betInput > total) { //if the inputed value is more than the total amount of money they user has left 
      bet = total; //set the bet to equal the amount of money the user has 
     } else { //if the inputed bet amount is less than the total 
      bet = betInput; //set the bet input the value of the bet 
     } 
     betLabel.setText("Bet: $" + bet); //updates the bet label to show the bet value next to its label 
     } 
    }); 

    spin.addActionListener(new ActionListener() { //creates an actionlistener for the spin button 
     @Override 
     public void actionPerformed(ActionEvent e) { //creates a method called actionperformed 
     String[] rand = new String[3]; //creates an array of 3 strings 
     int addTotal; //creates a variable called addTotal 
     //generate 3 new slots 
     for (int i = 0; i < 3; i++) { //loops through the three panes 
      int choice; //creates a varaibe called choice 
      double random = Math.random(); //creates a random variable and assigns it a random value 
      random *= 8; //times the value by 8 
      choice = (int) random; //assigns the value of choice to be random with a cast of int 
      rand[i] = symbols[choice]; //assigns the value rand at index i to be symbols at index choice 
      slot[i].setIcon(new ImageIcon("/images/" + symbols[choice] + ".png")); //shows the image for the slot in the panel 
      // TODO 
     } 


     //check for matches 
     //three matches 
     if (rand[0] == rand[1] && rand[1] == rand[2]) { //if there were three matches 
      int index = 0; //creates a variable index of type int and assigns it to the value 0 and 
      for (int i = 0; i < symbols.length; i++) //a loop that runs the length of symbols 
      if (rand[0] == symbols[i]) { //if rand[0] is equal to symbols[i] 
       index = i; //assign index to equal i 
      } 
      addTotal = bet * gotThree[index]; //increase addTotal by bet times gotThree[index], which stores the bonuses for triple pairs 
      total += addTotal; //increment total by addTotal 
      //pop-up menu saying the bet amount, the amount won and the updated total 
      JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal + "\nMoney Left: $" + total, 
      "Three Matches", JOptionPane.INFORMATION_MESSAGE); 

      amount.setText("Total: $" + total); //set the label Total: to equal total 
      if (total > maxWin) { //if the new total is greater than maxWin 
      maxWin = total; //maxWin is now assigned to total 
      } 
     } else if (rand[0] == rand[1] || rand[1] == rand[2] || rand[2] == rand[0]) { //checks if there is a match between 2 tiles 
      int match, index = 0; //creates integer variables match and index, assigning it to 0 
      if (rand[0] == rand[1] || rand[0] == rand[2]) { //if the first box has a match 
      match = 0; //match is assigned the value of 0 
      } else { //if the second of third box has the match 
      match = 1; //assign match to the value of 1 
      } 

      //find the index 
      for (int i = 0; i < symbols.length; i++) { //A loop that iterates the amount of times of the length of symbols 
      if (rand[0] == symbols[i]) { //if rand at index 0 is equal to symbols at index i 
       index = i; //set index to equal i 
      } 
      } 
      addTotal = bet * gotTwo[index]; //increment addTotal by bet*gotTwo[index] 
      //Creates a pop-up window that displays the bet amount and the winnings 
      JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal, 
      "Two Matches", JOptionPane.INFORMATION_MESSAGE); 
      total += addTotal; //increments total by addTotal 
      amount.setText("Total: $" + total); //updates the text next to total 
      if (total > maxWin) { //if the total is greater than maxWin 
      maxWin = total; //set maxWin to total 
      } 


     } else { //run this is there are no matches 
      total -= bet; //decrement total by bet 
      //Creates a pop-up window that shows the lost amount (bet) and the money left 
      JOptionPane.showMessageDialog(null, "You Lost: $" + bet + "\nMoney Left: $" + total, "No Matches", JOptionPane.INFORMATION_MESSAGE); 
      amount.setText("Total: $" + total); //updates the total amount that will be seen on the screen 
      if (total > maxWin) { //if the total is greater than maxWin 
      maxWin = total; //sets maxWin to equal total 
      } 
      if (bet > total) { //if bet is greater than the amount of money left (total) than 
      bet = total; //set the bet to total 
      betLabel.setText("Bet: $" + bet); //create the betLabel and update the value of it 
      } 
      if (total == 0) { //if the total is the 
      bet = 0; //sets the bet value to equal 0 
      betLabel.setText("Bet: $" + bet); //updates the betLabel to the new bet value 
      //Creates a pop-up message saying "You Lost :(and it also displays the maximum amount that the user won 
      JOptionPane.showMessageDialog(null, "You Lost :(\nThe Maximum That You Won Was $" + maxWin, 
       "\nGame Over", JOptionPane.INFORMATION_MESSAGE); 
      System.exit(0); //exit the program 
      } 
     } 
     } 
    }); 

    JPanel panelOnBottom = new JPanel(new BorderLayout()); //creates a panel on the bottom 
    panelOnBottom.add(moneyPanel, BorderLayout.WEST); //adds the money panel to the west/left border of the window 
    panelOnBottom.add(buttonsPanel, BorderLayout.EAST); //adds the buttons panel to the east/right border of the window 

    // TODO 
    JPanel all = new JPanel(new GridLayout(2, 1)); //creates a panel called all with a grid layout of 2x1 
    slotPanel.setVisible(true); 
    slotPanel.setOpaque(true); 
    all.add(slotPanel); //adds slotPanel to the 'all' panel 
    all.add(panelOnBottom); //adds the panelOnBottom to the 'all' panel 
    add(all); //add all to the window 

    } 

    public static void main(String[] args) { //the main class 
    JFrame frame = new SlotMachineProgram(); //creates a new frame of type SlotMachineProgram 
    frame.setTitle("Gambling Time!"); //creates the title of the frame to be Gambling Time! 
    frame.setLocationRelativeTo(null); //sets the location of the window to null, meaning it opens in the middle 
    frame.setSize(400, 300); //sets the size of the window to be 400 by 300 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the default close method to exit 
    frame.setResizable(false); //makes the window not resizable 
    frame.setVisible(true); //makes the window visible 


    } 
} 

答えて

0

最初にslotPanelに画像付きのjlablesを追加する必要があります。

//set slots to their default image 
slot[0] = new JLabel(new ImageIcon("/images/left.png")); 
slot[1] = new JLabel(new ImageIcon("/images/middle.png")); 
slot[2] = new JLabel(new ImageIcon("/images/right.png")); 

slotPanel.add(slot[0]); 
slotPanel.add(slot[1]); 
slotPanel.add(slot[2]); 

はその後、あなたは、コードの最初の場所で宣言された全ての画像は、/画像ディレクトリに存在することを確認します。

private String[] symbols = { 
     "Cherry", "Bear", 
     "Diamond", "Penguin", 
     "Star", "Shoe", 
     "Banana", "Orange", 
     "Apple" }; // symbols that are available for the slot machine