2011-12-18 7 views
2

Javaでメモリゲームを作成しようとしています。このような何かが、はるかにsimplier - >http://www.zefrank.com/memory/Javaのリスナーを使用してボタンを画像にリンクします。

ここでは私のコードです:

import javax.swing.*; 

public class Memoriin { 

    public static void main(String[] args) { 
     JFrame frame = new MemoriinFrame(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

} 

そして:だから

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 

public class MemoriinFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 
    public static final int DEFAULT_HEIGHT = 600; 
    public static final int DEFAULT_WIDTH = 800; 
    public JButton button[] = new JButton[8]; 
    ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>(); 
    ImageIcon tail = new ImageIcon("foto.jpg"); 

    ImageIcon photo1 = new ImageIcon("foto1.jpg"); 
    ImageIcon photo2 = new ImageIcon("foto2.jpg"); 
    ImageIcon photo3 = new ImageIcon("foto3.jpg"); 
    ImageIcon photo4 = new ImageIcon("foto4.jpg"); 
    ImageIcon photo1copy = photo1; 
    ImageIcon photo2copy = photo2; 
    ImageIcon photo3copy = photo3; 
    ImageIcon photo4copy = photo4; 



    public MemoriinFrame() { 
      setTitle("Memory Game"); 
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
      setLayout(new GridLayout(2, 4)); 

      addIcons(); 
      for(int i = 0; i <= 7; i++) { 
       button[i] = new JButton(); 
       button[i].setIcon(tail); 
       button[i].addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         performActionEventHandler(); 
        } 
       }); 
       add(button[i]); 
      } 

    } 

    public void performActionEventHandler() { 
     // how can I link each button with a specific picture? 
    } 

    public void addIcons() { 
     icons.add(photo1); 
     icons.add(photo2); 
     icons.add(photo3); 
     icons.add(photo4); 
     icons.add(photo1copy); 
     icons.add(photo2copy); 
     icons.add(photo3copy); 
     icons.add(photo4copy); 
     Collections.shuffle(icons); 
    } 

    public void tailToImage(JButton button) { 
     button.setIcon(icons.get(0)); 
     icons.remove(0); 
    } 
} 

、私は特定の画像でボタンをリンクしようとしています。私はそれをしようとしましたが、私は不必要な結果を持っていました:ボタンをクリックすると、画像がランダムの画像に変わります。しかし、私は8つのボタンと8つの写真を持っているので、それぞれのボタンがすべて同じゲームの長い画像になるようにリンクしたい。

P.S.英語は母国語ではありません。

答えて

3

ボタンとピクチャを関連付けるには、それらの間にマッピングを持つことが賢明です。あなたは何かのように使うことができます。

Map<JButton, ImageIcon> 

これは、ボタンとアイコンとの間の非常に粗い関係です。あなたはこれを即興で行う必要があるかもしれません。このようなもの..

画像ソース:foto1からfoto4の場合は、Stackoverflowから上位4人のユーザーのアバターを取りました。

enter image description here

ImageIcon photo1 = new ImageIcon("foto1.jpg"); 
ImageIcon photo2 = new ImageIcon("foto2.jpg"); 
ImageIcon photo3 = new ImageIcon("foto3.jpg"); 
ImageIcon photo4 = new ImageIcon("foto4.jpg"); 
ImageIcon photo1copy = new ImageIcon("foto1.jpg"); 
ImageIcon photo2copy = new ImageIcon("foto2.jpg"); 
ImageIcon photo3copy = new ImageIcon("foto3.jpg"); 
ImageIcon photo4copy = new ImageIcon("foto4.jpg"); 

Map<JButton, ImageIcon> buttonImage = new HashMap<JButton, ImageIcon>(); 

public MemoriinFrame() { 
     setTitle("Memory Game"); 
     setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
     setLayout(new GridLayout(2, 4)); 

     for(int i = 0; i <= 7; i++) { 

      button[i] = new JButton(); 
      button[i].setIcon(tail); 
      button[i].addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        performActionEventHandler((JButton)e.getSource()); 
       } 
      }); 
      add(button[i]); 
     } 

     addIcons(); 

} 

public void performActionEventHandler(JButton clickedButton) { 
    clickedButton.setIcon(buttonImage.get(clickedButton)); 
} 

public void addIcons() { 
    icons.add(photo1); 
    icons.add(photo2); 
    icons.add(photo3); 
    icons.add(photo4); 
    icons.add(photo1copy); 
    icons.add(photo2copy); 
    icons.add(photo3copy); 
    icons.add(photo4copy); 
    Collections.shuffle(icons); 

    for(int i=0;i<icons.size();i++){ 
     buttonImage.put(button[i], icons.get(i)); 
    } 
} 

:私はちょうどそれで遊んでいたので、これはCOMPLETEバグ無料答えではありません。そして、それはリファクタリングされるべき範囲の多くを持っています。しかし、これはあなたを動かすのに十分なはずです。

+0

私は同じコードを書いていますが、performActionEventHandler()のリスナーを書くべきことを説明できますか? –

+0

@Dmitri:彼は匿名の内部リスナーを使用していて、このリスナーにインスタンスメソッド「performActionEventHandler」を呼び出させています。この例では1+です。 –

2

私自身、ImageIcon(ArrayList<ImageIcon>)のArrayListを作成し、各ImageIconに2つずつ追加しました。私はランダム化するリストのCollections.shuffle(...)を呼び出します。その後、HashMap<JButton, Icon>を使用して、各ボタンを画像に関連付けます。次に、ボタンが押されたら、JButtonのアイコンをマップ内のアイコンに設定します(間違っているとアイコンを削除する場合はnullを設定します)。

関連する問題