-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
139 lines (101 loc) · 3.13 KB
/
GameWindow.java
File metadata and controls
139 lines (101 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package MemoryGrid;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.io.*;
@SuppressWarnings("serial")
public class GameWindow extends JFrame {
private Card selectedCard;
private Card c1;
private Card c2;
private Timer t;
public Grid g1;
private int count;
public void begin(User u,Thread newt) throws FileNotFoundException
{
List<BufferedImage> cardVals = new ArrayList<BufferedImage>();
g1=new Grid();
count=g1.display(cardVals);
Collections.shuffle(cardVals);
for (BufferedImage val : cardVals)
{
Card c = new Card();
c.setfront(val);
c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
selectedCard = c;
mouseclicked();
}
});
g1.cards.add(c);
}
Time t1=new Time();
t = new Timer(750, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (c1.getfront() == c2.getfront()) //match condition
{
c1.setEnabled(false); //disables the button
c2.setEnabled(false);
count-=1;
if (count<=0)
{
hardexit(t1,u,newt);
}
}
else{
c1.setIcon(null); //'hides' text
c2.setIcon(null);
}
c1 = null; //reset c1 and c2
c2 = null;
}
});
t.setRepeats(false);
//set up the board itself
Container pane = getContentPane();
int temp=(Grid.dimension/Grid.cd);
pane.setLayout(new GridLayout(temp,temp));
for (Card c : g1.cards){
pane.add(c);
}
// some time passes
setTitle("Memory Match");
}
public void mouseclicked()
{
if (c1 == null && c2 == null)
{
c1 = selectedCard;
//c1.setText(String.valueOf(c1.getId()));
c1.setIcon(new ImageIcon(c1.getfront()));
}
if (c1 != null && c1 != selectedCard && c2 == null)
{
c2 = selectedCard;
//c2.setText(String.valueOf(c2.getId()));
c2.setIcon(new ImageIcon(c2.getfront()));
t.start();
}
}
public void hardexit(Time t1,User u,Thread t)
{
t1.timepassed=(System.nanoTime()-t1.timepassed)/1000000000;
JOptionPane.showMessageDialog(this, "You win!.Your time is "+t1.timepassed+"seconds");
try
{
GameSystem.reflect_data(t1,u);
}
catch (IOException e)
{
//null
}
}
}