-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameForm.java
121 lines (106 loc) · 4.74 KB
/
GameForm.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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package tetris;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
/**
*
* @author alswo
*/
public class GameForm extends JFrame {
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
gameAreaPlaceholder = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
gameAreaPlaceholder.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
gameAreaPlaceholder.setPreferredSize(new java.awt.Dimension(150, 270));
javax.swing.GroupLayout gameAreaPlaceholderLayout = new javax.swing.GroupLayout(gameAreaPlaceholder);
gameAreaPlaceholder.setLayout(gameAreaPlaceholderLayout);
gameAreaPlaceholderLayout.setHorizontalGroup(
gameAreaPlaceholderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 148, Short.MAX_VALUE)
);
gameAreaPlaceholderLayout.setVerticalGroup(
gameAreaPlaceholderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 268, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(110, 110, 110)
.addComponent(gameAreaPlaceholder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(140, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(gameAreaPlaceholder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel gameAreaPlaceholder;
// End of variables declaration//GEN-END:variables
private GameArea ga;
public GameForm() {
initComponents();
ga = new GameArea(gameAreaPlaceholder,10);
this.add(ga); //프레임 안에 게임 화면 생성 , 열의 개수 지정
//게임화면 객체도 여기에서 생성
initControls();
startGame();
}
public void startGame(){
new GameThread(ga).start();
}
private void initControls(){ //키보드 입력받기
InputMap im = this.getRootPane().getInputMap();
ActionMap am = this.getRootPane().getActionMap();
im.put(KeyStroke.getKeyStroke("RIGHT"),"right");
im.put(KeyStroke.getKeyStroke("LEFT"),"left");
im.put(KeyStroke.getKeyStroke("UP"),"up");
im.put(KeyStroke.getKeyStroke("DOWN"),"down");
am.put("right",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
ga.moveBlockRight();
}
});
am.put("left",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
ga.moveBlockLeft();}});
am.put("up",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
ga.rotateBlock();}});
am.put("down",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
ga.dropBlock();}});
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GameForm().setVisible(true); //window
}
});
}
}