2012-06-07 44 views
-1

我工作的一個策劃遊戲的Java面向對象編程和我所擁有的一切,除了代碼的一部分工作是策劃遊戲的Java Swing,MouseListener的

if(uG[0].getIcon() == compGuess[0].getIcon() && uG[1].getIcon() == compGuess[1].getIcon() && uG[2].getIcon() == compGuess[2].getIcon() && uG[3].getIcon() == compGuess[3].getIcon()) 
     { 
      JOptionPane.showMessageDialog(f1, "You have guessed them all coorect!", "Mastermind: Test Your Mind", JOptionPane.INFORMATION_MESSAGE); 

      submit.setEnabled(false); 

      compGuess[0].setEnabled(false); 
      compGuess[1].setEnabled(false); 
      compGuess[2].setEnabled(false); 
      compGuess[3].setEnabled(false); 

      compGuess[0].setVisible(true); 
      compGuess[1].setVisible(true); 
      compGuess[2].setVisible(true); 
      compGuess[3].setVisible(true); 

      uG[0].setEnabled(false); 
      uG[1].setEnabled(false); 
      uG[2].setEnabled(false); 
      uG[3].setEnabled(false); 

      System.exit(0); 
     } 

我加入的全部代碼,因爲上述代碼^^ show是我的問題所在,但是你無法弄清楚它爲什麼不按照它應有的方式工作,除非你查看原始的完整代碼。是的,通讀可能需要一兩分鐘的時間,但它會幫助解決手頭的問題。您可以將音頻部分註釋掉並更改圖標。當它運行時,計算機猜測不會顯示,直到你猜對了所有的錯誤,但這就是問題所在。即使你猜對了,遊戲也不會直到你猜到了10次。我無法弄清楚爲什麼會發生這種情況。任何人都可以幫忙嗎?

import java.awt.*; 
    import java.io.*; 
    import javax.swing.*; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.MouseListener; 
    import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.*; 

    public class Mastermind extends JFrame 
    { 

    public static int found = 0; 
    public static int guesses = 0; 
    public final JTextField guess = new JTextField(20); 
    public static JFormattedTextField nG = new JFormattedTextField(); 
public Mastermind() 
{ 

    final JFrame f1 = new JFrame("Mastermind: Can you win?"); 
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //runs a song/midi upon start of the game 
    int replys = JOptionPane.showConfirmDialog(f1, "Welcome to Mastermind! \n T he game to test your brain." + 
                     "\n\n\nWould you like to play?", "Mastermind: Test Your Brain!", JOptionPane.YES_NO_OPTION); 
    if(replys == JOptionPane.YES_OPTION) 
    { 
     try 
     { 

      //AudioFormat audioFormat; 
      //AudioInputStream audioInputStream; 
      //SourceDataLine sourceDataLine; 

      File soundFile = new File("swfinal.mid"); 
          AudioInputStream ai =  AudioSystem.getAudioInputStream(soundFile); 
      Clip clip = AudioSystem.getClip(); 
      clip.open(ai); 
      clip.start(); 
     } 
     catch (Exception f) 
     {} 
    } 
    else 
    { 
     System.exit(0); 
    } 

    //makes the panel to display the computer's guess 
    final JPanel gamePanel = new JPanel(new GridLayout(1,4,5,5)); 
    f1.add(gamePanel); 
    gamePanel.setBorder(BorderFactory.createMatteBorder(5,5,5,5, Color.GREEN)); 
    gamePanel.setBackground(Color.BLACK); 

    //the rules on how to play 
    JOptionPane.showMessageDialog(f1, "Welcome to Mastermind! \n The game to test your brain.", "Mastermind: Test Your Brain!", JOptionPane.INFORMATION_MESSAGE); 
    JOptionPane.showMessageDialog(f1, "You have to guess what color is behind the circle the White cirlce" + 
                "\n This will display on the right side of the f1 for you." + 
                "\n Your guesses will display on the left side of the f1 for you." + 
                "\n You have 10 attempts at getting it correct" + 
                "after all 10 attmepts are done \nand you did not guess correctly, the game will end." + 
                "\n\n\n Now, good luck on Mastermind. May you have a very fun time!", "Mastermind Rules", J OptionPane.ERROR_MESSAGE); 


    //makes the panel to display the heading 
    final JPanel Gs = new JPanel(); 
    f1.add(Gs); 
    Gs.setBorder(BorderFactory.createMatteBorder(5,5,5,5, Color.GREEN)); 
    Gs.setBackground(Color.BLACK); 

    //makes the panel where you control everything 
    final JPanel p2 = new JPanel(); 
    f1.add(p2); 
    p2.setBorder(BorderFactory.createMatteBorder(5,5,5,5, Color.GREEN)); 
    p2.setBackground(Color.BLACK); 

    final JLabel line = new JLabel(""); 
    p2.add(line); 
    line.setForeground(Color.GREEN); 

    //creates the submit button 
    final JButton submit = new JButton("Submit"); 
    p2.add(submit); 

    //adds a text field to display the number of guesses 
    final JFormattedTextField tf1 = new JFormattedTextField(); 
    p2.add(tf1); 
    tf1.setColumns(2); 
    tf1.setEditable(false); 
    tf1.setForeground(Color.RED); 
    tf1.setValue(new Integer(guesses)); 

    JLabel here = new JLabel("Take a guess: "); 
      p2.add(here); 
    here.setForeground(Color.GREEN); 

    final JLabel master = new JLabel("MASTERMIND: TEST YOUR MIND!"); 
    master.setForeground(Color.green); 
    Font newf = new Font("SansSerif", Font.BOLD + Font.ITALIC, 32); 
    master.setFont(newf); 
    Gs.add(master); 

    //add and initializes the computers guess buttons 
    //and its guesses 
    final String[] colored = new String[4]; 
    colored[0] = "circle-red.png"; 
    colored[1] = "circle-blue.png"; 
    colored[2] = "circle-green.png"; 
    colored[3] = "circle-yellow.png"; 

    final JButton[] compGuess = new JButton[4]; 
    compGuess[0] = new JButton(); 
    compGuess[1] = new JButton(); 
    compGuess[2] = new JButton(); 
    compGuess[3] = new JButton(); 

    gamePanel.add(compGuess[0]); 
    gamePanel.add(compGuess[1]); 
    gamePanel.add(compGuess[2]); 
    gamePanel.add(compGuess[3]); 

    compGuess[0].setVisible(false); 
    compGuess[1].setVisible(false); 
    compGuess[2].setVisible(false); 
    compGuess[3].setVisible(false); 

    final String[] colored2 = new String[4]; 
    final int[] checkFilled = new int[4]; 
    for(int fill = 0; fill < 4; fill++) 
    { 
     checkFilled[fill] = 0; 
    } 
    int counter = 0; 
    do 
    { 
     int Random = (int)(Math.random()*4); 

     if(checkFilled[Random] == 0) 
     { 
      colored2[counter] = colored[Random]; 
      checkFilled[Random] = 1; 
      counter++; 
     } 
    } 
    while(counter < 4); 

    final ImageIcon cell1 = new ImageIcon(colored2[0]); 
    final ImageIcon cell2 = new ImageIcon(colored2[1]); 
    final ImageIcon cell3 = new ImageIcon(colored2[2]); 
    final ImageIcon cell4 = new ImageIcon(colored2[3]); 

    compGuess[0].setIcon(cell1); 
    compGuess[1].setIcon(cell2); 
    compGuess[2].setIcon(cell3); 
    compGuess[3].setIcon(cell4); 

    //adding a label to tell user where their guessing at 
    JLabel user = new JLabel("Your Guess: "); 
    user.setForeground(Color.green); 
    p2.add(user); 

    //adding your guessing buttons 
    final JButton[] uG = new JButton[4]; 
    uG[0] = new JButton(); 
    uG[1] = new JButton(); 
    uG[2] = new JButton(); 
    uG[3] = new JButton(); 

    p2.add(uG[0]); 
    p2.add(uG[1]); 
    p2.add(uG[2]); 
    p2.add(uG[3]); 

    //This is the mouse event handler that make it change colors 
    //to the corresponding color that you want with the click of 
    //the mouse on the button 
    uG[0].addMouseListener(new MouseListener() 
    { 
     @Override 
        public void mouseClicked(MouseEvent e) 
     { 
      if(e.getClickCount() == 1) 
      { 
       uG[0].setIcon(new ImageIcon("circle-red.png")); 
      } 
      else if(e.getClickCount() == 2) 
      { 
       uG[0].setIcon(new ImageIcon("circle-blue.png")); 
      } 
      else if(e.getClickCount() == 3) 
      { 
       uG[0].setIcon(new ImageIcon("circle-green.png")); 
      } 
      else if(e.getClickCount() == 4) 
      { 
       uG[0].setIcon(new ImageIcon("circle-yellow.png")); 
      } 
     } 

     @Override 
     public void mousePressed(MouseEvent e) { 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { } 

     @Override 
     public void mouseEntered(MouseEvent e) {} 

     @Override 
     public void mouseExited(MouseEvent e) {} 
    }); 

    uG[1].addMouseListener(new MouseListener() 
    { 
     @Override 
        public void mouseClicked(MouseEvent e) 
     { 
      if(e.getClickCount() == 1) 
      { 
       uG[1].setIcon(new ImageIcon("circle-red.png")); 
      } 
      else if(e.getClickCount() == 2) 
      { 
       uG[1].setIcon(new ImageIcon("circle-blue.png")); 
      } 
      else if(e.getClickCount() == 3) 
      { 
       uG[1].setIcon(new ImageIcon("circle-green.png")); 
      } 
      else if(e.getClickCount() == 4) 
      { 
       uG[1].setIcon(new ImageIcon("circle-yellow.png")); 
      } 
     } 

     @Override 
     public void mousePressed(MouseEvent e) { 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { } 

     @Override 
     public void mouseEntered(MouseEvent e) {} 

     @Override 
     public void mouseExited(MouseEvent e) {} 
    }); 

    uG[2].addMouseListener(new MouseListener() 
    { 
     @Override 
        public void mouseClicked(MouseEvent e) 
     { 
      if(e.getClickCount() == 1) 
      { 
       uG[2].setIcon(new ImageIcon("circle-red.png")); 
      } 
      else if(e.getClickCount() == 2) 
      { 
       uG[2].setIcon(new ImageIcon("circle-blue.png")); 
      } 
      else if(e.getClickCount() == 3) 
      { 
       uG[2].setIcon(new ImageIcon("circle-green.png")); 
      } 
      else if(e.getClickCount() == 4) 
      { 
       uG[2].setIcon(new ImageIcon("circle-yellow.png")); 
      } 
     } 

     @Override 
     public void mousePressed(MouseEvent e) { 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { } 

     @Override 
     public void mouseEntered(MouseEvent e) {} 

     @Override 
     public void mouseExited(MouseEvent e) {} 
    }); 

    uG[3].addMouseListener(new MouseListener() 
    {  
        @Override 
     public void mouseClicked(MouseEvent e) 
     { 
      if(e.getClickCount() == 1) 
      { 
       uG[3].setIcon(new ImageIcon("circle-red.png")); 
      } 
      else if(e.getClickCount() == 2) 
      { 
       uG[3].setIcon(new ImageIcon("circle-blue.png")); 
      } 
      else if(e.getClickCount() == 3) 
      { 
       uG[3].setIcon(new ImageIcon("circle-green.png")); 
      } 
      else if(e.getClickCount() == 4) 
      { 
       uG[3].setIcon(new ImageIcon("circle-yellow.png")); 
      } 
     } 

     @Override 
     public void mousePressed(MouseEvent e) { 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { } 

     @Override 
     public void mouseEntered(MouseEvent e) {} 

     @Override 
     public void mouseExited(MouseEvent e) {} 
    }); 

    submit.addMouseListener(new MouseListener() 
    { 

    @Override 
public void mouseClicked(MouseEvent e) 
{ 
    if(e.getSource() == submit) 
    { 
     int s = 0; 

     System.out.println(uG[0].getIcon()); 
     System.out.println(uG[1].getIcon()); 
     System.out.println(uG[2].getIcon()); 
     System.out.println(uG[3].getIcon()); 
     System.out.println(compGuess[0].getIcon()); 
     System.out.println(compGuess[1].getIcon()); 
     System.out.println(compGuess[2].getIcon()); 
     System.out.println(compGuess[3].getIcon()); 

     if(uG[0].getIcon() == compGuess[0].getIcon() && uG[1].getIcon() == compGuess[1].getIcon() && uG[2].getIcon() == compGuess[2].getIcon() && uG[3].getIcon() == compGuess[3].getIcon()) 
     { 
      JOptionPane.showMessageDialog(f1, "You have guessed them all coorect!", "Mastermind: Test Your Mind", JOptionPane.INFORMATION_MESSAGE); 
      submit.setEnabled(false); 
      compGuess[0].setEnabled(false); 
      compGuess[1].setEnabled(false); 
      compGuess[2].setEnabled(false); 
      compGuess[3].setEnabled(false); 
      uG[0].setEnabled(false); 
      uG[1].setEnabled(false); 
      uG[2].setEnabled(false); 
      uG[3].setEnabled(false); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(f1, "Please try again!", "Mastermind: Test Your Mind", JOptionPane.INFORMATION_MESSAGE); 
     } 


     guesses++; 
     tf1.setValue(new Integer(guesses)); 

     if(guesses == 10) 
     { 
      JOptionPane.showMessageDialog(f1, "Thank you for playing! \n\n\n Have a wonderful day.", "Mastermind: Test Your Mind", JOptionPane.INFORMATION_MESSAGE); 
      System.exit(0); 
     } 

     int x = 0; 
     int y = 0; 

     if(uG[0].getIcon() == compGuess[0].getIcon()) 
     { 
      x = x + 1; 
     } 
     if(uG[1].getIcon() == compGuess[1].getIcon()) 
     { 
      x = x + 1; 
     } 
     if(uG[2].getIcon() == compGuess[2].getIcon()) 
     { 
      x = x + 1; 
     } 
     if(uG[3].getIcon() == compGuess[2].getIcon()) 
     { 
      x = x + 1; 
     } 

     if(uG[0].getIcon() != compGuess[0].getIcon()) 
     { 
      y = y + 1; 
     } 
     if(uG[1].getIcon() != compGuess[1].getIcon()) 
     { 
      y = y + 1; 
     } 
     if(uG[2].getIcon() != compGuess[2].getIcon()) 
     { 
      y = y + 1; 
     } 
     if(uG[3].getIcon()!= compGuess[2].getIcon()) 
     { 
      y = y + 1; 
     } 

     JOptionPane.showMessageDialog(f1, "You have " + x + " colors correct and in the right position."+ 
                "\nYou have " + y + " colors correct but in the wrong position.", "Mastermind: Test Your  Mind", JOptionPane.INFORMATION_MESSAGE); 
    } 
} 

    @Override 
    public void mousePressed(MouseEvent e) {} 

    @Override 
    public void mouseReleased(MouseEvent e) {} 

    @Override 
    public void mouseEntered(MouseEvent e) {} 

    @Override 
    public void mouseExited(MouseEvent e) {} 

    }); 
    //set the component of the game on the f1 
    //and also sets the size of the fame and makes 
    // its visible to the user 
    f1.add(Gs, BorderLayout.NORTH); 
    f1.add(gamePanel, BorderLayout.CENTER); 
    f1.add(p2, BorderLayout.SOUTH); 
    f1.setSize(650, 350); 
    f1.setVisible(true); 
    f1.setResizable(false); 
    f1.setLocationRelativeTo(null); 
} 

public static void main(String[] args) 
{ 
    Mastermind mastermind = new Mastermind(); 
} 
    } 

在此先感謝傑伊!

+2

「您可以將音頻部分註釋掉並更改圖標。」這不是[SSCCE](http://www.sscce.org/)的工作方式。 SSCCE的想法實際上是爲你的助手提供儘可能小的代碼,只顯示你的問題。這樣,助手不必浪費時間閱讀整個代碼,只需通過小代碼即可閱讀,甚至可以將其粘貼到他的IDE中以重現您的問題。 – brimborium

+0

這個[示例](http://stackoverflow.com/a/3072979/230513)顯示瞭如何讓你的圖標自成一體,並且它可能會提出一種不同的方式來組織你的代碼。 – trashgod

回答

0

您描述的問題是由比較不同的參考引起的。

compGuess陣列由像初始化(分佈式翻過你的SORCE代碼,但基本上是這樣的):

// colored2[0] contains filename 
final ImageIcon cell1 = new ImageIcon(colored2[0]); 
compGuess[0] = new JButton(); 
compGuess[0].setIcon(cell1); 

即創建一個JButton並分配一個新創建的ImageIcon到該按鈕。

在另一方面爲uG我們的代碼

uG[0] = new JButton(); 
uG[0].setIcon(new ImageIcon("circle-red.png")); // inside mouse listener 

這意味着你再創建一個按鈕,併爲其分配的ImageIcon另一個實例是一個。

所以,當你比較

uG[0].getIcon() == compGuess[0].getIcon() 

你其實比較這當然無法匹配,因爲他們獨立創作,並因此總是不同的對象的圖標實例。

要解決這個問題,您可以創建每個不同的圖標一次,併爲所有組件共享/重用它。但我更喜歡不同的方式,並將表示(圖標)與底層數據模型分開,並將它們與合適的(與視圖無關的)數據類型(例如enum DiskColor { RED, BLUE, GREEN, YELLOW };)存儲起來,您可以輕鬆地比較它們,但也可以轉換爲您的視圖表示。

+0

謝謝,我用紅色,藍色,綠色和黃色將圖標放在最上面。然後將它們設置爲等於相應的圖標,例如public ImageIcon RED = new ImageIcon(「circle-red.png」);.只需將使用該顏色的所有內容都設置爲紅色即可。現在它起作用了。 – Jaybro90

-1

將是這樣的:

Thread.sleep(1000); 
System.exit(0); 

在代碼顯示,數猜中用戶。我認爲問題是在後臺運行沒有守護線程,並且你沒有指定你想要程序結束。

+2

-1不要睡在[EDT](http://docs.oracle.com。COM/JavaSE的/教程/ uiswing /併發/ dispatch.html)。 – Howard