-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextPanel.java
160 lines (142 loc) · 4.01 KB
/
TextPanel.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
import javax.swing.*;
import java.awt.*;
import java.io.*;
/** A simple custom JPanel for use in text-based Java applications
*
* @author Charles Zinn
*/
public class TextPanel extends JPanel {
private char[][] ch; //chars
private CharCol[][] cc; //char colours
private char[][] dch; //display chars
private CharCol[][] dcc; //display char colours
private static Font font = new Font("Courier New", Font.PLAIN, 16);
private static int SIZE = 16;
public static void main(String[] args) {
JFrame f = new JFrame("Roguelike");
TextPanel p = new TextPanel(34, 82);
f.add(p);
f.pack();
f.setVisible(true);
p.drawBox('#', new CharCol(Color.BLACK, Color.RED), 0, 0, 34, 82);
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 16; j++) {
int n = j + i * 16;
String s = "" + n + (char)n;
while(s.length() < 4) s = "0" + s;
p.drawString(s, i + 1, j * 5 + 1);
}
}
}
TextPanel(int rows, int cols) {
//clear it
ch = new char[rows][cols];
cc = new CharCol[rows][cols];
clear();
//flip it
flip();
}
public int getRows() {
return ch.length;
}
public int getCols() {
return ch[0].length;
}
public int getWidth() {
return getCols() * SIZE;
}
public int getHeight() {
return getRows() * SIZE;
}
public void drawChar(char c, int row, int col) {
drawChar(c, new CharCol(), row, col);
}
public void drawChar(char c, CharCol cl, int row, int col) {
if(row >= 0 && row < getRows() && col >= 0 && col < getCols()) {
ch[row][col] = c;
cc[row][col] = cl;
}
}
public void drawString(String s, int row, int col) {
drawString(s, new CharCol(), row, col);
}
public void drawString(String s, CharCol cl, int row, int col) {
for(int i = 0; i < s.length(); i++) {
drawChar(s.charAt(i), cl, row, col + i);
}
}
public void drawBox(char c, int row, int col, int height, int width) {
drawBox(c, new CharCol(), row, col, height, width);
}
public void drawBox(char c, CharCol cl, int row, int col, int height, int width) {
for(int i = row; i < row + height; i++) {
for(int j = col; j < col + width; j++) {
if(i == row || i == row + height - 1 || j == col || j == col + width - 1)
drawChar(c, cl, i, j);
}
}
}
public void fillBox(char c, int row, int col, int height, int width) {
fillBox(c, new CharCol(), row, col, height, width);
}
public void fillBox(char c, CharCol cl, int row, int col, int height, int width) {
for(int i = row; i < row + height; i++) {
for(int j = col; j < col + width; j++) {
drawChar(c, cl, i, j);
}
}
}
public void clear() {
ch = new char[getRows()][getCols()];
cc = new CharCol[getRows()][getCols()];
//set all chars to a (white, black) ' '
for(int i = 0; i < getRows(); i++) {
for(int j = 0; j < getCols(); j++) {
ch[i][j] = ' ';
cc[i][j] = new CharCol();
}
}
}
/** Swaps the buffers and clears the hidden one */
public void flip() {
dch = ch.clone();
dcc = cc.clone();
clear();
}
@Override
protected void paintComponent(Graphics g) {
g.setFont(font);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
for(int i = 0; i < getRows(); i++) {
for(int j = 0; j < getCols(); j++) {
CharCol tcc = dcc[i][j];
if(tcc == null) tcc = new CharCol();
g.setColor(tcc.bg);
g.fillRect(j * SIZE, i * SIZE, SIZE, SIZE);
g.setColor(tcc.text);
char tch = dch[i][j];
if(tch == (char)0) tch = ' ';
g.drawString(tch + "", j * SIZE + 2, i * SIZE + 12);
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(getWidth(), getHeight());
}
}
class CharCol {
public Color text;
public Color bg;
CharCol() {
this(Color.WHITE);
}
CharCol(Color text) {
this(text, Color.BLACK);
}
CharCol(Color text, Color bg) {
this.text = text;
this.bg = bg;
}
}