Skip to content

Commit 9eff557

Browse files
author
Nolin Reddy
committed
it works
1 parent a12aa82 commit 9eff557

File tree

5 files changed

+421
-35
lines changed

5 files changed

+421
-35
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.example.javafx;
2+
3+
import java.awt.Graphics;
4+
import java.awt.event.ActionEvent;
5+
import java.awt.event.ActionListener;
6+
import java.awt.event.KeyEvent;
7+
import java.awt.event.KeyListener;
8+
import java.awt.image.BufferedImage;
9+
import java.io.IOException;
10+
11+
import javax.imageio.ImageIO;
12+
import javax.swing.JPanel;
13+
import javax.swing.Timer;
14+
15+
public class Board extends JPanel implements KeyListener{
16+
17+
private BufferedImage blocks;
18+
19+
private final int blockSize = 30;
20+
21+
private final int boardWidth = 10, boardHeight = 20;
22+
23+
private int[][] board = new int[boardHeight][boardWidth];
24+
25+
private Shape[] shapes = new Shape[7];
26+
27+
private Shape currentShape;
28+
29+
private Timer timer;
30+
31+
private final int FPS = 60;
32+
33+
private final int delay = 1000/FPS;
34+
35+
private boolean gameOver = false;
36+
37+
public Board(){
38+
39+
try {
40+
blocks = ImageIO.read(Board.class.getResource("/tiles.png"));
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
45+
timer = new Timer(delay, new ActionListener(){
46+
47+
@Override
48+
public void actionPerformed(ActionEvent e) {
49+
update();
50+
repaint();
51+
}
52+
});
53+
54+
timer.start();
55+
56+
// shapes
57+
58+
shapes[0] = new Shape(blocks.getSubimage(0, 0, blockSize, blockSize), new int[][]{
59+
{1, 1, 1, 1} // IShape
60+
}, this, 1);
61+
62+
shapes[1] = new Shape(blocks.getSubimage(blockSize, 0, blockSize, blockSize), new int[][]{
63+
{1, 1, 0},
64+
{0, 1, 1} // ZShape
65+
}, this, 2);
66+
67+
shapes[2] = new Shape(blocks.getSubimage(blockSize*2, 0, blockSize, blockSize), new int[][]{
68+
{0, 1, 1},
69+
{1, 1, 0} // S-Shape
70+
}, this, 3);
71+
72+
shapes[3] = new Shape(blocks.getSubimage(blockSize*3, 0, blockSize, blockSize), new int[][]{
73+
{1, 1, 1},
74+
{0, 0, 1} // J-Shape
75+
}, this, 4);
76+
77+
shapes[4] = new Shape(blocks.getSubimage(blockSize*4, 0, blockSize, blockSize), new int[][]{
78+
{1, 1, 1},
79+
{1, 0, 0} // L-Shape
80+
}, this, 5);
81+
82+
shapes[5] = new Shape(blocks.getSubimage(blockSize*5, 0, blockSize, blockSize), new int[][]{
83+
{1, 1, 1},
84+
{0, 1, 0} // T-Shape
85+
}, this, 6);
86+
87+
shapes[6] = new Shape(blocks.getSubimage(blockSize*6, 0, blockSize, blockSize), new int[][]{
88+
{1, 1},
89+
{1, 1} // O-Shape
90+
}, this, 7);
91+
92+
setNextShape();
93+
94+
}
95+
96+
public void update(){
97+
currentShape.update();
98+
if(gameOver)
99+
timer.stop();
100+
}
101+
102+
103+
public void paintComponent(Graphics g){
104+
super.paintComponent(g);
105+
106+
currentShape.render(g);
107+
108+
for(int row = 0; row < board.length; row++)
109+
for(int col = 0; col < board[row].length; col++)
110+
if(board[row][col] != 0)
111+
g.drawImage(blocks.getSubimage((board[row][col]-1)*blockSize, 0, blockSize, blockSize),
112+
col*blockSize, row*blockSize, null);
113+
114+
115+
116+
for(int i = 0; i < boardHeight; i++){
117+
g.drawLine(0, i*blockSize, boardWidth*blockSize, i*blockSize);
118+
}
119+
for(int j = 0; j < boardWidth; j++){
120+
g.drawLine(j*blockSize, 0, j*blockSize, boardHeight*blockSize);
121+
}
122+
123+
}
124+
125+
public void setNextShape(){
126+
127+
int index = (int)(Math.random()*shapes.length);
128+
129+
Shape newShape = new Shape(shapes[index].getBlock(), shapes[index].getCoords(),
130+
this, shapes[index].getColor());
131+
132+
currentShape = newShape;
133+
134+
for(int row = 0; row < currentShape.getCoords().length; row++)
135+
for(int col = 0; col < currentShape.getCoords()[row].length; col++)
136+
if(currentShape.getCoords()[row][col] != 0){
137+
138+
if(board[row][col + 3] != 0)
139+
gameOver = true;
140+
}
141+
142+
143+
144+
}
145+
146+
147+
public int getBlockSize(){
148+
return blockSize;
149+
}
150+
151+
public int[][] getBoard(){
152+
return board;
153+
}
154+
155+
@Override
156+
public void keyPressed(KeyEvent e) {
157+
if(e.getKeyCode() == KeyEvent.VK_LEFT)
158+
currentShape.setDeltaX(-1);
159+
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
160+
currentShape.setDeltaX(1);
161+
if(e.getKeyCode() == KeyEvent.VK_DOWN)
162+
currentShape.speedDown();
163+
if(e.getKeyCode() == KeyEvent.VK_UP)
164+
currentShape.rotate();
165+
}
166+
167+
@Override
168+
public void keyReleased(KeyEvent e) {
169+
if(e.getKeyCode() == KeyEvent.VK_DOWN)
170+
currentShape.normalSpeed();
171+
}
172+
173+
@Override
174+
public void keyTyped(KeyEvent e) {
175+
// TODO Auto-generated method stub
176+
177+
}
178+
}

src/main/java/com/example/javafx/JavafxApplication.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,7 @@
1515
public class JavafxApplication extends JFrame {
1616

1717
public JavafxApplication() {
18-
19-
initUI();
20-
}
21-
22-
private void initUI() {
23-
24-
var quitButton = new JButton("Quit");
25-
26-
quitButton.addActionListener((ActionEvent event) -> {
27-
System.exit(0);
28-
});
29-
30-
createLayout(quitButton);
31-
32-
setTitle("Quit button");
33-
setSize(300, 200);
34-
setLocationRelativeTo(null);
35-
setDefaultCloseOperation(EXIT_ON_CLOSE);
36-
}
37-
38-
private void createLayout(JComponent... arg) {
39-
40-
var pane = getContentPane();
41-
var gl = new GroupLayout(pane);
42-
pane.setLayout(gl);
43-
44-
gl.setAutoCreateContainerGaps(true);
45-
46-
gl.setHorizontalGroup(gl.createSequentialGroup()
47-
.addComponent(arg[0])
48-
);
49-
50-
gl.setVerticalGroup(gl.createSequentialGroup()
51-
.addComponent(arg[0])
52-
);
18+
new Window();
5319
}
5420

5521
public static void main(String[] args) {

0 commit comments

Comments
 (0)