-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJPieceTest.java
More file actions
123 lines (105 loc) · 3.62 KB
/
JPieceTest.java
File metadata and controls
123 lines (105 loc) · 3.62 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
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
/**
* Debugging client for the Piece class.
*
* The JPieceTest component draws all the rotations of a tetris piece.
* JPieceTest.main() creates a frame with one JPieceTest for each of the 7
* standard tetris pieces.
*
* @author Nick Parlante
* @version 1.0, Mar 1, 2001
*/
class JPieceTest extends JComponent
{
public final int MAX_ROTATIONS = 4;
private Piece root;
public JPieceTest(Piece piece, int width, int height)
{
super();
setPreferredSize(new Dimension(width, height));
this.root = piece;
}
/**
* Draws the rotations from left to right.
* Each piece goes in its own little box.
*/
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Piece piece = this.root;
double rectWidth = this.getWidth() / MAX_ROTATIONS;
Rectangle2D.Double rect = new Rectangle2D.Double(0, 0,
rectWidth, this.getHeight());
do
{
this.drawPiece(g2, piece, rect);
piece = piece.nextRotation();
// remove this for final test of the Piece class
if(piece == null)
break;
rect.setRect(rect.getX() + rectWidth, rect.getY(),
rect.getWidth(), rect.getHeight());
}
while(piece != this.root);
}
/**
* Draw the piece inside the given rectangle.
*/
private void drawPiece(Graphics2D g2, Piece piece, Rectangle2D.Double r)
{
double pixelSize = Math.min(r.getWidth(), r.getHeight()) / MAX_ROTATIONS;
for(Point point : piece.getBody())
{
/*
* leave a 1 pixel border around the block
*
* adjust the y value as (0,0) is in the upper-left but the piece
* is placed at the lower-left of the rectangle
*/
Rectangle2D.Double pixel = new Rectangle2D.Double(
r.getX() + (point.x * pixelSize) + 1,
r.getY() + ((3 - point.y) * pixelSize) + 1,
pixelSize - 2, pixelSize - 2);
// draw skirt pixels in yellow
if(point.y == piece.getSkirt()[point.x])
{
g2.setColor(Color.YELLOW);
}
g2.fill(pixel);
// draw a description of the piece along the bottom
g2.setColor(Color.RED);
String desc = "w:" + piece.getWidth() + " h:" + piece.getHeight() +
" sw:" + piece.getSkirt().length;
g2.drawString(desc, (float)(r.getX() + 1),
(float)(r.getY() + (4 * pixelSize) - 2));
g2.setColor(Color.BLACK);
}
}
/**
* Draws all the pieces by creating a JPieceTest for each piece, and putting
* them all in a frame.
*/
static public void main(String[] args)
{
JFrame frame = new JFrame("Piece Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent container = (JComponent)frame.getContentPane();
// put in a BoxLayout to make a vertical list
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
Piece[] pieces = Piece.getPieces();
for (int i = 0; i < pieces.length; i++)
{
JPieceTest test = new JPieceTest(pieces[i], 375, 75);
container.add(test);
}
// size the window and show it on screen
frame.pack();
frame.setVisible(true);
}
}