/
ImageFont.java
191 lines (164 loc) · 6.47 KB
/
ImageFont.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class ImageFont {
BufferedImage tilesImg; // entire image with all character tiles
BufferedImage[] tiles; // break down original image into array of individual
// easily indexible character tiles
int imgW, imgH; // width and height of image containing character tiles
int tW, tH; // individual tile width and height
int numCols, numRows; // number of columns and rows of tiles in the image
int numTiles; // total number of tiles, numCols * numRows
public ImageFont() {
this("CodePageTransparent.png");
}
public ImageFont(String pathToImage) {
this(pathToImage, 32, 8);
}
public ImageFont(String pathToImage, int numCols, int numRows) {
try {
tilesImg = convertToARGB(ImageIO.read(new File(pathToImage)));
this.imgW = tilesImg.getWidth();
this.imgH = tilesImg.getHeight();
this.numCols = numCols;
this.numRows = numRows;
this.numTiles = numCols * numRows;
this.tW = this.imgW / this.numCols;
this.tH = this.imgH / this.numRows;
this.tiles = new BufferedImage[this.numTiles];
for(int i = 0; i < numCols; i++) {
for(int j = 0; j < numRows; j++) {
tiles[(numCols * j) + i] = tilesImg.getSubimage(i * tW, j * tH, tW,tH);
}
}
} catch (IOException e) {
System.out.println("Couldn't load the image: " + e);
}
}
/* ------------------------------------------------------------------------------ *
* Drawing methods *
* ------------------------------------------------------------------------------ */
public void drawChar(char ch, int x, int y, Graphics g) {
int ascii = (int) ch;
drawTileNum(ascii, x, y, g);
}
public void drawChars(char[] chs, int x, int y, Graphics g) {
int len = chs.length;
for(int i = 0; i < len; i++) {
drawChar(chs[i], x + this.tW * i, y, g);
}
}
public void drawString(String s, int x, int y, Graphics g) {
drawChars(s.toCharArray(), x, y, g);
}
public void drawTileNum(int tileNum, int x, int y, Graphics g) {
drawTileNumC(tileNum, x, y, Color.WHITE, g);
}
public void drawTileNums(int[] tileNums, int x, int y, Graphics g) {
int len = tileNums.length;
for(int i = 0; i < len; i++) {
drawTileNum(tileNums[i], x + this.tW * i, y, g);
}
}
public void drawCharC(char ch, int x, int y, Color c, Graphics g) {
int ascii = (int) ch;
drawTileNumC(ascii, x, y, c, g);
}
public void drawCharsC(char[] chs, int x, int y, Color c, Graphics g) {
int len = chs.length;
for(int i = 0; i < len; i++) {
drawCharC(chs[i], x + this.tW * i, y, c, g);
}
}
public void drawCharsCs(char[] chs, int x, int y, Color[] cs, Graphics g) {
int len = chs.length;
for(int i = 0; i < len; i++) {
drawCharC(chs[i], x + this.tW * i, y, cs[i], g);
}
}
public void drawStringC(String s, int x, int y, Color c, Graphics g) {
drawCharsC(s.toCharArray(), x, y, c, g);
}
public void drawStringCs(String s, int x, int y, Color[] cs, Graphics g) {
drawCharsCs(s.toCharArray(), x, y, cs, g);
}
public void drawTileNumC(int tileNum, int x, int y, Color c, Graphics g) {
BufferedImage coloredTile = tiles[tileNum];
for(int i = 0; i < this.tW; i++) {
for(int j = 0; j < this.tH; j++) {
Color originalColor = new Color(coloredTile.getRGB(i, j), true);
Color nc = new Color(c.getRed(), c.getGreen(), c.getBlue(), originalColor.getAlpha());
coloredTile.setRGB(i, j, nc.getRGB());
}
}
g.drawImage(tiles[tileNum], x, y, null);
}
public void drawTileNumsC(int[] tileNums, int x, int y, Color c, Graphics g) {
int len = tileNums.length;
for(int i = 0; i < len; i++) {
drawTileNumC(tileNums[i], x + this.tW * i, y, c, g);
}
}
public void drawTileNumsCs(int[] tileNums, int x, int y, Color[] cs, Graphics g) {
int len = tileNums.length;
for(int i = 0; i < len; i++) {
drawTileNumC(tileNums[i], x + this.tW * i, y, cs[i], g);
}
}
/* ------------------------------------------------------------------------------ *
* Getters / Setters *
* ------------------------------------------------------------------------------ */
public int tileWidth() { return this.tW; }
public int tileHeight() { return this.tH; }
public static BufferedImage convertToARGB(BufferedImage image) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
/* ------------------------------------------------------------------------------ *
* Tile ID constants *
* ------------------------------------------------------------------------------ */
public static final int T_BLANK = 0;
public static final int T_SMILEY = 1;
public static final int T_INVERTED_SMILEY = 2;
public static final int T_HEART = 3;
public static final int T_DIAMOND = 4;
public static final int T_SPADE = 5;
public static final int T_CLUB = 6;
public static final int T_PEG = 7;
public static final int T_PEG_HOLE = 8;
public static final int T_TUBE = 9;
public static final int T_TUBE_HOLE = 10;
public static final int T_MALE = 11;
public static final int T_FEMALE = 12;
public static final int T_SIXTEENTH_NOTE = 13;
public static final int T_DOUBLE_SIXTEENTH_NOTE = 14;
public static final int T_SNOWFLAKE = 15;
public static final int T_RIGHT_TRAINGLE = 16;
public static final int T_LEFT_TRIANGLE = 17;
public static final int T_UP_AND_DOWN_ARROW = 18;
public static final int T_DOUBLE_EXCLAMATION = 19;
public static final int T_PARAGRAPH = 20;
public static final int T_SECTION = 21;
public static final int T_BOTTOM_BLOCK = 22;
public static final int T_UP_AND_DOWN_ARROW_BAR = 23;
public static final int T_UP_ARROW = 24;
public static final int T_DOWN_ARROW = 25;
public static final int T_RIGHT_ARROW = 26;
public static final int T_LEFT_ARROW = 27;
public static final int T_RIGHT_ANGLE = 28;
public static final int T_LEFT_AND_RIGHT_ARROW = 29;
public static final int T_UP_TRIANGLE = 30;
public static final int T_DOWN_TRIANGLE = 31;
public static final int T_VERT_BAR = 179;
public static final int T_UP_RIGHT_CORNER = 191;
public static final int T_BOT_LEFT_CORNER = 192;
public static final int T_HORIZONTAL_BAR = 196;
public static final int T_BOT_RIGHT_CORNER = 217;
public static final int T_UP_LEFT_CORNER = 218;
}