tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
999
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    andreas2000 andreas2000 ist offline Mitglied Silber
    Registriert seit
    Nov 2005
    Beiträge
    76
    Hallo ich habe ein Problem,

    Idee ist folgende: Ein Memory Spiel mit 8 Bilderkarten mit Bildern von Personen und 8 Namenskarten mit den Namen der Personen. Leider kann ich mit untenstehendem Code nur Bilddateien als Spielkarten (hier mit ImageIcon und populate()) hochladen, und keine Spielkarten mit eigenen Texten versehen. Ist es möglich, ImageIcon einen String zu übergeben, den dieser anzeigt (wenn die Karte angeklickt wird)?

    Vielleicht kann mir jemand helfen.
    Gruß,
    Andreas.

    hier der funktionierende Code für ein Memory Spiel:

    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;

    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class Main extends JFrame {

    private ImageIcon backIcon;

    private CardGame game;

    public Main() {
    super("Main");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container c = getContentPane();
    game = new CardGame();
    backIcon = new ImageIcon(game.loadImage("e:/imgs/back.jpg"));
    game.init();

    CardPanel cp = new CardPanel();
    cp.populate(game.getCards());

    c.add(cp);

    pack();
    setVisible(true);
    }

    public static void main(String[] args) {
    new Main();
    }

    class Card extends JLabel {

    private ImageIcon icon;

    protected int id;

    protected boolean stillInGame = true;

    public Card(ImageIcon icon, int id) {
    this.icon = icon;
    this.id = id;
    setIcon(backIcon);
    setBorder(BorderFactory.createEtchedBorder());

    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent evt) {
    game.selectCard(Card.this);
    game.checkCards();
    }
    });

    }

    public void showFront() {
    setIcon(this.icon);
    }

    public void showBack() {
    setIcon(backIcon);
    }
    }

    class CardPanel extends JPanel {

    final int MAX_ROWS = 4;

    final int MAX_COLUMNS = 4;

    public CardPanel() {
    setLayout(new GridLayout(MAX_ROWS, MAX_COLUMNS));
    }

    public void populate(List list) {
    for (Iterator iter = list.iterator(); iter.hasNext() {
    add((Card) iter.next());
    }
    }
    }

    class CardGame {
    private List cards;

    private int selectionCount = 0;

    private Card current;

    private Card previous;

    public CardGame() {
    cards = new ArrayList();
    }

    public void init() {
    cards.clear();
    loadCards();
    Collections.shuffle(cards);
    }

    public Image loadImage(String imgPath) {

    final int MAX_WIDTH = 60;
    final int MAX_HEIGHT = 120;
    try {
    Image img = ImageIO.read(new File(imgPath)).getScaledInstance(
    MAX_WIDTH, MAX_HEIGHT, BufferedImage.SCALE_SMOOTH);
    return img;
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    }

    private void loadCards() {

    for (int i = 0; i < 8; i++) {
    ImageIcon icon = new ImageIcon(loadImage("e:/imgs/" + i
    + ".jpg"));

    Card c = new Card(icon, i);
    cards.add(c);
    c = new Card(icon, i);
    cards.add(c);
    }
    }

    public void selectCard(Card c) {

    if (current == c)
    return;
    if (!c.stillInGame)
    return;
    c.showFront();
    if (selectionCount++ % 2 == 0) {
    if (current != null && previous != null) {
    current.showBack();
    previous.showBack();
    }
    current = null;
    previous = null;
    }

    previous = current;
    current = c;
    }

    public void checkCards() {
    if (current != null && previous != null)
    if (current.id == previous.id) {
    current.stillInGame = false;
    previous.stillInGame = false;
    current = null;
    previous = null;
    System.out.println("match");
    }
    }

    public List getCards() {
    return cards;
    }
    }
    }
     

  2. #2
    Registriert seit
    Apr 2004
    Ort
    Ruhrgebiet
    Beiträge
    1.582
    Hi,
    nimm JButton, dann kannst Du auf Klick auf die Karte (JButton) reagieren und das anzuzeigende Bild austauschen.
     

  3. #3
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

Ähnliche Themen

  1. kleines Memory Spiel
    Von Yuri-Li im Forum Java Grundlagen
    Antworten: 9
    Letzter Beitrag: 02.06.10, 23:43
  2. Memory-Spiel
    Von Manda im Forum Borland CBuilder und VCL
    Antworten: 18
    Letzter Beitrag: 21.11.06, 17:45
  3. Java memory spiel
    Von 'ka im Forum Java
    Antworten: 5
    Letzter Beitrag: 07.12.04, 17:49
  4. HILFE bei Memory Spiel
    Von tugi im Forum Java
    Antworten: 5
    Letzter Beitrag: 12.07.04, 08:21
  5. suche hilfe für memory spiel
    Von toewee im Forum Delphi, Kylix, Pascal
    Antworten: 6
    Letzter Beitrag: 03.06.04, 17:54