2

Javaでメモリゲームを作成しようとしています。このようなものですが、はるかに単純です-> http://www.zefrank.com/memory/

これが私のコードです:

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つの画像があり、それらをリンクしたいので、各ボタンはゲーム全体で同じ画像になります。

PS英語は私の母国語ではありません。

4

2 に答える 2

3

ボタンと画像を関連付けるには、それらの間にマッピングを設定することをお勧めします。あなたはのようなものを使うことができます。

Map<JButton, ImageIcon>

上記は、ボタンとアイコンの間の非常に大雑把な関係です。これについて即興で演奏する必要があるかもしれません。このようなもの..

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

ここに画像の説明を入力してください

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));
    }
}

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

于 2011-12-18T22:30:24.823 に答える
2

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

于 2011-12-18T22:31:29.153 に答える