Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2014 16:05
Show Gist options
  • Save anonymous/8270001 to your computer and use it in GitHub Desktop.
Save anonymous/8270001 to your computer and use it in GitHub Desktop.
package memorygame;
import javax.swing.JFrame;
/**
*
* @author
* @version 1.0
*
*/
public class Main {
public static void main(String[] args) {
GUI kasutajaLiides = new GUI();
kasutajaLiides.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Diplay kinnipanemisel l6ppeb ka programm
kasutajaLiides.setResizable(false); // Ei lase display suurust muuta
kasutajaLiides.setSize(600, 375); // Display suurus
kasutajaLiides.setVisible(true); //Display on n2htav
kasutajaLiides.setLocationRelativeTo(null); //Display on ekraani keskel automaatselt
} //main end
} // Main end
package memorygame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class GUI extends JFrame {
private static final long serialVersionUID = 1L; // GUI tahab, tegelikuses ei m6juta programmi
// JButton nupp0 = new JButton(); // Teen nupu objektid, rida 54
// JButton nupp1 = new JButton();
// JButton nupp2 = new JButton();
// JButton nupp3 = new JButton();
// JButton nupp4 = new JButton();
// JButton nupp5 = new JButton();
// JButton nupp6 = new JButton();
// JButton nupp7 = new JButton();
//
// public JButton nupud[] = { nupp0, nupp1, nupp2, nupp3, nupp4, nupp5, nupp6,
// nupp7 };
JButton[] nupud = new JButton[8];{
for (int i = 0 ; i < nupud.length; i++){
nupud[i] = new JButton();
}
// ImageIcon A = new ImageIcon(this.getClass().getResource("num1.png")); // annan v22rtustele pildid
// ImageIcon B = new ImageIcon(this.getClass().getResource("num2.png"));
// ImageIcon C = new ImageIcon(this.getClass().getResource("num3.png"));
// ImageIcon D = new ImageIcon(this.getClass().getResource("num4.png"));
}
ImageIcon A = new ImageIcon(this.getClass().getResource("num1.png")); // annan v22rtustele pildid
ImageIcon B = new ImageIcon(this.getClass().getResource("num2.png"));
ImageIcon C = new ImageIcon(this.getClass().getResource("num3.png"));
ImageIcon D = new ImageIcon(this.getClass().getResource("num4.png"));
public GUI() {
setTitle("MEMORY Mäng"); // Tiitel
JPanel paneel = new JPanel(); // Teeb põhipaneeli kõikide komponentide jaoks
paneel.setLayout(new BorderLayout()); // Määrab põhipaneeli paigutuse tüübi
JPanel nuppudePaneel = new JPanel(); // Teeb nuppude jaoks eraldi paneeli
nuppudePaneel.setLayout(new GridLayout(2, 4, 2, 2)); // Määrab nuppude paneeli paigutuse
JPanel alumineRiba = new JPanel(); // Teeb alumise riba jaoks eraldi paneeli
JButton uusM2ngButton = new JButton("UUS MÄNG"); // Teeb alumise riba jaoks nupu
alumineRiba.add(uusM2ngButton); // Lisab alumisele ribale Uus Mäng nupu
paneel.add(nuppudePaneel); // Lisab põhipaneelile nuppude paneeli
paneel.add(alumineRiba, BorderLayout.SOUTH); // Lisab põhipaneelile alumise riba
add(paneel); // Lisab põhipaneeli aknale
for(int i = 0;i < nupud.length; i++){ // lisab p6hinupud
nuppudePaneel.add(nupud[i]); // nuppudePaneel.add(nupp1);
}
nupuVajutus nV = new nupuVajutus(); // rida 88
for(int i = 0; i < nupud.length; i++){ // paneme objektid ootama nupuvajutust
nupud[i].addActionListener(nV); // nupp0.addActionListener(nV);
}
uusM2ngButton.addActionListener(nV);
} // GUI end
public void v6idukontroll() { // kontrollib kas k6ik paarid on leitud
if (nupud[0].getIcon() == A && nupud[1].getIcon() == C
&& nupud[2].getIcon() == D && nupud[3].getIcon() == A
&& nupud[4].getIcon() == D && nupud[5].getIcon() == B
&& nupud[6].getIcon() == C && nupud[7].getIcon() == B) {
JDialog v6idukast = new JDialog();
v6idukast.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
v6idukast.setVisible(true);
v6idukast.setLocationRelativeTo(null);
JTextArea v6idukastiKiri = new JTextArea();
v6idukastiKiri.setText(" VÕIT");
v6idukastiKiri.setEditable(false);
v6idukastiKiri.setSize(100,100);
v6idukast.add(v6idukastiKiri);
v6idukast.pack(); // automaatselt seab 6ige suuruse aknale
v6idukast.setResizable(false);
}
} // v6idukontroll end
class nupuVajutus implements ActionListener {
private static final long serialVersionUID = 1L;
public int m2lu = 0;
public ImageIcon piltm2lu1 = null;
public ImageIcon piltm2lu2 = null;
public void actionPerformed(ActionEvent event) {
String name = event.getActionCommand();
if (name.equals("UUS MÄNG")){ // kui UUS M2NG siis resetitakse pildid
for(int i = 0; i < nupud.length;i++){
nupud[i].setIcon(null);
}
}
if (event.getSource() == nupud[0]) { // Kui vajuttakse nuppu 0
m2lu++; // Mälule läheb 1 juurde
nupud[0].setIcon(A); // Paneb vajutatud nupule ikooni
if (m2lu == 1) { // Kui mälu on 1 siis piltmälu1 salvestatakse see pilt
piltm2lu1 = A;
}
if (m2lu == 2) { // Kui mälu on 2 siis salvestatakse piltmälu2 see pilt
piltm2lu2 = A;
if (piltm2lu1 == piltm2lu2) { // Kui salvestatakse piltmälu2 siis on esimene võimalus et need 2 avatud pilti on samad
m2lu = 0; // nullitakse mälud
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) { // Kui mälu on 3
if (piltm2lu1 == piltm2lu2) { // Piltmälude võrdlemine
m2lu = 0; // Sätitakse mälu nulli jälle
piltm2lu1 = null; // Piltmälud nullitakse
piltm2lu2 = null;
} else { // Kui kolmanda mälu korral piltmälud ei klapi siis tehakse terve laud tühjaks(Mäng hakkab otsast peale
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null); //nupp0.setIcon(null);
}
}
}
}// Nupp0 end
if (event.getSource() == nupud[1]) {
m2lu++;
nupud[1].setIcon(C);
if (m2lu == 1) {
piltm2lu1 = C;
}
if (m2lu == 2) {
piltm2lu2 = C;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp1 end
if (event.getSource() == nupud[2]) {
m2lu++;
nupud[2].setIcon(D);
if (m2lu == 1) {
piltm2lu1 = D;
}
if (m2lu == 2) {
piltm2lu2 = D;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp2 end
if (event.getSource() == nupud[3]) {
m2lu++;
nupud[3].setIcon(A);
if (m2lu == 1) {
piltm2lu1 = A;
}
if (m2lu == 2) {
piltm2lu2 = A;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp3 end
if (event.getSource() == nupud[4]) {
m2lu++;
nupud[4].setIcon(D);
if (m2lu == 1) {
piltm2lu1 = D;
}
if (m2lu == 2) {
piltm2lu2 = D;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp4 end
if (event.getSource() == nupud[5]) {
m2lu++;
nupud[5].setIcon(B);
if (m2lu == 1) {
piltm2lu1 = B;
}
if (m2lu == 2) {
piltm2lu2 = B;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp5 end
if (event.getSource() == nupud[6]) {
m2lu++;
nupud[6].setIcon(C);
if (m2lu == 1) {
piltm2lu1 = C;
}
if (m2lu == 2) {
piltm2lu2 = C;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp6 end
if (event.getSource() == nupud[7]) {
m2lu++;
nupud[7].setIcon(B);
if (m2lu == 1) {
piltm2lu1 = B;
}
if (m2lu == 2) {
piltm2lu2 = B;
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
}
}
if (m2lu == 3) {
if (piltm2lu1 == piltm2lu2) {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
} else {
m2lu = 0;
piltm2lu1 = null;
piltm2lu2 = null;
for(int i = 0 ; i < nupud.length ; i++ ){
nupud[i].setIcon(null);
}
}
}
}// Nupp7 end
System.out.println(piltm2lu1); // ei
System.out.println(piltm2lu2); // ole
System.out.println(m2lu); // vajalik
v6idukontroll();
} // actionPerformed end
} // nupuvajutus end
} // GUI extends JFrame end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment