StefanoFrena
08-11-2008, 17:23
Mi scuso se prima ho sbagliato a postare :doh: , spero che ora vada bene.
Mi quoto comunque:
Prima di tutto ciao a tutti , dopodichè avrei qualche domanda(o forse di più) da porvi.
Allora, ho deciso di creare un gioco di nome Memory, ecco il codice:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Memory extends JFrame
{
public Memory()
{
MemoryListener mem1 = new MemoryListener();
Container lay = this.getContentPane();
lay.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 6));
for (int i = 0; i < 36; i++)
{
CharButton b = new CharButton(this.getCharacter());
buttonPanel.add(b);
b.addActionListener(mem1);
b.cover();
}
setTitle("Memory");
lay.add(buttonPanel, BorderLayout.CENTER);
setSize(400, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private String[] labels = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "1", "2",
"3", "4", "5", "6", "7", "8", "9","10", "11", "12", "13", "14",
"15", "16", "17", "18" };
private int groesse = labels.length;
private String getCharacter()
{
int ran = (int) (Math.random() * 100) % groesse;
String ran1 = labels[ran];
for (int i = ran + 1; i < groesse; i++)
{
labels[i - 1] = labels[i];
}
groesse--;
return ran1;
}
public static void main(String[] args) throws Exception
{
Memory m = new Memory();
}
}
import java.io.Serializable;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class MemoryListener implements ActionListener
{
private CharButton erste = null;
private CharButton zweite = null;
private int Paare = 0;
public MemoryListener()
{
super();
}
public void actionPerformed(ActionEvent e)
{
CharButton mem2 = (CharButton) e.getSource();
if (erste == null)
{
erste = mem2;
mem2.setEnabled(false);
mem2.uncover();
}
else if (zweite == null)
{
mem2.setEnabled(false);
mem2.uncover();
zweite = mem2;
if (erste.getChar().equals(zweite.getChar()))
{
erste.setBackground(Color.BLACK);
zweite.setBackground(Color.BLACK);
erste = null;
zweite = null;
Paare++;
if (Paare == 18)
{
JOptionPane.showMessageDialog(null,
"Du hasst gewonnen!!", "Gut gemacht",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
}
}
else
{
mem2.setEnabled(false);
mem2.uncover();
erste.cover();
erste.setEnabled(true);
erste = mem2;
zweite.cover();
zweite.setEnabled(true);
zweite = null;
}
}
}
import java.io.Serializable;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class CharButton extends JButton
{
private String werte;
CharButton(String lab)
{
super(lab);
werte = lab;
}
void uncover()
{
setText(werte);
}
void cover()
{
setText("");
}
String getChar()
{
return werte;
}
}
Ora vorrei mettere al posto dei numeri, delle immagini, ma purtroppo non so come fare :(
Vorrei anche che quando si finisce il gioco, compaia un finestra, nella quale inserire il nome del giocatore eppoi successivamente poter vedere la lista dei migliori giocatori, duque il gioco dovrebbe essere a tempo...
Lo so che vi sto chiedendo molto, però ci ho messo tanto per creare questo gioco e vorrei che fosse il più bello possibile :D
e ripeto anche:
lol è che io sono di madrelingua tedesca, frequento un scuola tedesca e dunque(visto che lo dobbiamo fare per compito) i nomi delle variabili, delle classi, ecc è in tedesco.
se proprio non mi credi posso "tradurre" tutto il italiano e posso anche aggiungere qualche commento al programma °°
Scusate ancora.
Mi quoto comunque:
Prima di tutto ciao a tutti , dopodichè avrei qualche domanda(o forse di più) da porvi.
Allora, ho deciso di creare un gioco di nome Memory, ecco il codice:
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Memory extends JFrame
{
public Memory()
{
MemoryListener mem1 = new MemoryListener();
Container lay = this.getContentPane();
lay.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 6));
for (int i = 0; i < 36; i++)
{
CharButton b = new CharButton(this.getCharacter());
buttonPanel.add(b);
b.addActionListener(mem1);
b.cover();
}
setTitle("Memory");
lay.add(buttonPanel, BorderLayout.CENTER);
setSize(400, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private String[] labels = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "1", "2",
"3", "4", "5", "6", "7", "8", "9","10", "11", "12", "13", "14",
"15", "16", "17", "18" };
private int groesse = labels.length;
private String getCharacter()
{
int ran = (int) (Math.random() * 100) % groesse;
String ran1 = labels[ran];
for (int i = ran + 1; i < groesse; i++)
{
labels[i - 1] = labels[i];
}
groesse--;
return ran1;
}
public static void main(String[] args) throws Exception
{
Memory m = new Memory();
}
}
import java.io.Serializable;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class MemoryListener implements ActionListener
{
private CharButton erste = null;
private CharButton zweite = null;
private int Paare = 0;
public MemoryListener()
{
super();
}
public void actionPerformed(ActionEvent e)
{
CharButton mem2 = (CharButton) e.getSource();
if (erste == null)
{
erste = mem2;
mem2.setEnabled(false);
mem2.uncover();
}
else if (zweite == null)
{
mem2.setEnabled(false);
mem2.uncover();
zweite = mem2;
if (erste.getChar().equals(zweite.getChar()))
{
erste.setBackground(Color.BLACK);
zweite.setBackground(Color.BLACK);
erste = null;
zweite = null;
Paare++;
if (Paare == 18)
{
JOptionPane.showMessageDialog(null,
"Du hasst gewonnen!!", "Gut gemacht",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
}
}
else
{
mem2.setEnabled(false);
mem2.uncover();
erste.cover();
erste.setEnabled(true);
erste = mem2;
zweite.cover();
zweite.setEnabled(true);
zweite = null;
}
}
}
import java.io.Serializable;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class CharButton extends JButton
{
private String werte;
CharButton(String lab)
{
super(lab);
werte = lab;
}
void uncover()
{
setText(werte);
}
void cover()
{
setText("");
}
String getChar()
{
return werte;
}
}
Ora vorrei mettere al posto dei numeri, delle immagini, ma purtroppo non so come fare :(
Vorrei anche che quando si finisce il gioco, compaia un finestra, nella quale inserire il nome del giocatore eppoi successivamente poter vedere la lista dei migliori giocatori, duque il gioco dovrebbe essere a tempo...
Lo so che vi sto chiedendo molto, però ci ho messo tanto per creare questo gioco e vorrei che fosse il più bello possibile :D
e ripeto anche:
lol è che io sono di madrelingua tedesca, frequento un scuola tedesca e dunque(visto che lo dobbiamo fare per compito) i nomi delle variabili, delle classi, ecc è in tedesco.
se proprio non mi credi posso "tradurre" tutto il italiano e posso anche aggiungere qualche commento al programma °°
Scusate ancora.