-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFrame.java
168 lines (140 loc) · 4.2 KB
/
TFrame.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
public class TFrame extends JFrame implements KeyListener{
private static final long serialVersionUID = 1L;
public JLabel scoreLabel = new JLabel("Woohoo");
public JTextField textField = new JTextField();
public JPanel panel = new JPanel(new BorderLayout());
public TLabel label = new TLabel(300, 700);
public State s;
public int orient, slot;
public static final int MANUAL = 0;
public static final int NONE = 1;
public int mode = MANUAL;
//constructor
public TFrame (State s){
this.s = s;
s.label = label;
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows when this is closed
setTitle("Tetris BKW");
panel.add(label.draw, BorderLayout.CENTER);
panel.add(scoreLabel, BorderLayout.NORTH);
getContentPane().add(panel);
pack();
label.BORDER = .05;
label.setXscale(0, State.COLS);
label.setYscale(0, State.ROWS+5);
this.addKeyListener(this); //may be unnecessary (not certain)
setVisible(true);
}
//switches which state is attached to this TFrame
public void bindState(State s) {
if(s!= null) s.label = null;
this.s = s;
s.label = label;
}
///
/// ADDED BY DON (AKA Pimp Masta) 1/22/09
///
public TFrame (){
s.label = label;
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows when this is closed
setTitle("Eric Whitman's Tetris Simulator");
setContentPane(label.draw);
this.getContentPane().add(scoreLabel);
pack();
label.BORDER = .05;
label.setXscale(0, State.COLS);
label.setYscale(0, State.ROWS+5);
this.addKeyListener(this); //may be unnecessary (not certain)
setVisible(true);
}
public void setScoreLabel(int s) {
scoreLabel.setText("Score :" + s);
}
public void keyPressed(KeyEvent e) {
switch(mode) {
case(MANUAL): {
switch(e.getKeyCode()) {
case(KeyEvent.VK_RIGHT): {
if(slot < State.COLS-State.pWidth[s.nextPiece][orient]) slot++;
s.clearNext();
s.drawNext(slot, orient);
break;
}
case(KeyEvent.VK_LEFT): {
if(slot > 0) slot--;
s.clearNext();
s.drawNext(slot, orient);
break;
}
case(KeyEvent.VK_UP): {
orient++;
if(orient%State.pOrients[s.nextPiece]==0) orient = 0;
if(slot > State.COLS-State.pWidth[s.nextPiece][orient])
slot = State.COLS-State.pWidth[s.nextPiece][orient];
s.clearNext();
s.drawNext(slot, orient);
break;
}
case(KeyEvent.VK_DOWN): {
if(!s.makeMove(orient, slot)) mode = NONE;
if(orient >= State.pOrients[s.nextPiece]) orient = 0;
if(slot > State.COLS-State.pWidth[s.nextPiece][orient])
slot = State.COLS-State.pWidth[s.nextPiece][orient];
s.draw();
if(mode == NONE) {
label.text(State.COLS/2.0, State.ROWS/2.0, "You Lose");
}
s.clearNext();
s.drawNext(slot, orient);
break;
}
default:
break;
}
}
case(NONE): break;
default:
System.out.println("unknown mode");
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void save(String filename) {
File file = new File(filename);
String suffix = filename.substring(filename.lastIndexOf('.') + 1);
BufferedImage bImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = (Graphics2D)bImage.getGraphics();
paint(graphic);
graphic.drawImage(bImage, 0, 0, null);
// png files
if (suffix.toLowerCase().equals("png")) {
try { ImageIO.write(bImage, suffix, file); }
catch (IOException e) { e.printStackTrace(); }
}
else System.out.println("unknown extension");
}
public static void main(String[] args) {
State s = new State();
TFrame t = new TFrame(s);
s.draw();
s.drawNext(0,0);
//t.save("picture.png");
}
}