Commit | Line | Data |
---|---|---|
1ac2ccb1 KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * License: LGPLv3 or later | |
5 | * | |
6 | * This module is licensed under the GNU Lesser General Public License | |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
9 | * | |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
27 | * | |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
30 | */ | |
31 | package jexer.io; | |
32 | ||
33 | import jexer.bits.Cell; | |
34 | import jexer.bits.CellAttributes; | |
35 | ||
36 | import java.awt.Color; | |
37 | import java.awt.Cursor; | |
38 | import java.awt.Font; | |
39 | import java.awt.FontMetrics; | |
40 | import java.awt.Frame; | |
41 | import java.awt.Graphics; | |
42 | import java.awt.geom.Rectangle2D; | |
43 | ||
44 | /** | |
45 | * This Screen implementation draws to a Java AWT Frame. | |
46 | */ | |
47 | public final class AWTScreen extends Screen { | |
48 | ||
49 | /** | |
50 | * AWTFrame is our top-level hook into the AWT system. | |
51 | */ | |
52 | class AWTFrame extends Frame { | |
53 | ||
54 | /** | |
55 | * The TUI Screen data. | |
56 | */ | |
57 | AWTScreen screen; | |
58 | ||
59 | /** | |
60 | * Width of a character cell. | |
61 | */ | |
62 | private int textWidth = 1; | |
63 | ||
64 | /** | |
65 | * Height of a character cell. | |
66 | */ | |
67 | private int textHeight = 1; | |
68 | ||
69 | /** | |
70 | * Top pixel value. | |
71 | */ | |
72 | private int top = 30; | |
73 | ||
74 | /** | |
75 | * Left pixel value. | |
76 | */ | |
77 | private int left = 30; | |
78 | ||
79 | /** | |
80 | * Public constructor. | |
81 | */ | |
82 | public AWTFrame() { | |
83 | setTitle("Jexer Application"); | |
84 | setBackground(java.awt.Color.black); | |
85 | setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); | |
86 | setFont(new Font("Liberation Mono", Font.BOLD, 16)); | |
87 | // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16)); | |
88 | setSize(100, 100); | |
89 | setVisible(true); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Resize to font dimensions. | |
94 | */ | |
95 | public void resizeToScreen() { | |
96 | Graphics gr = getGraphics(); | |
97 | FontMetrics fm = gr.getFontMetrics(); | |
98 | textWidth = fm.charWidth('m'); | |
99 | textHeight = fm.getHeight(); | |
100 | setSize((textWidth + 1) * screen.width + (2 * left), | |
101 | (textHeight + 1) * screen.height + (2 * top)); | |
102 | ||
103 | System.err.printf("W: %d H: %d\n", textWidth, textHeight); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Paint redraws the whole screen. | |
108 | * | |
109 | * @param gr the AWT Graphics context | |
110 | */ | |
111 | @Override | |
112 | public void paint(Graphics gr) { | |
113 | ||
114 | for (int y = 0; y < screen.height; y++) { | |
115 | for (int x = 0; x < screen.width; x++) { | |
116 | Cell lCell = screen.logical[x][y]; | |
117 | Cell pCell = screen.physical[x][y]; | |
118 | ||
119 | int xPixel = x * (textWidth + 1) + left; | |
120 | int yPixel = y * (textHeight + 1) + top - y; | |
121 | ||
122 | if (!lCell.equals(pCell)) { | |
123 | // Draw the background rectangle, then the foreground | |
124 | // character. | |
125 | if (lCell.getBackColor().equals(jexer.bits.Color.BLACK)) { | |
126 | gr.setColor(Color.black); | |
127 | } else if (lCell.getBackColor().equals(jexer.bits.Color.RED)) { | |
128 | gr.setColor(Color.red); | |
129 | } else if (lCell.getBackColor().equals(jexer.bits.Color.BLUE)) { | |
130 | gr.setColor(Color.blue); | |
131 | } else if (lCell.getBackColor().equals(jexer.bits.Color.GREEN)) { | |
132 | gr.setColor(Color.green); | |
133 | } else if (lCell.getBackColor().equals(jexer.bits.Color.YELLOW)) { | |
134 | gr.setColor(Color.yellow); | |
135 | } else if (lCell.getBackColor().equals(jexer.bits.Color.CYAN)) { | |
136 | gr.setColor(Color.cyan); | |
137 | } else if (lCell.getBackColor().equals(jexer.bits.Color.MAGENTA)) { | |
138 | gr.setColor(Color.magenta); | |
139 | } else if (lCell.getBackColor().equals(jexer.bits.Color.WHITE)) { | |
140 | gr.setColor(Color.white); | |
141 | } | |
142 | gr.fillRect(xPixel, yPixel, textWidth + 1, | |
143 | textHeight + 2); | |
144 | ||
145 | if (lCell.getForeColor().equals(jexer.bits.Color.BLACK)) { | |
146 | gr.setColor(Color.black); | |
147 | } else if (lCell.getForeColor().equals(jexer.bits.Color.RED)) { | |
148 | gr.setColor(Color.red); | |
149 | } else if (lCell.getForeColor().equals(jexer.bits.Color.BLUE)) { | |
150 | gr.setColor(Color.blue); | |
151 | } else if (lCell.getForeColor().equals(jexer.bits.Color.GREEN)) { | |
152 | gr.setColor(Color.green); | |
153 | } else if (lCell.getForeColor().equals(jexer.bits.Color.YELLOW)) { | |
154 | gr.setColor(Color.yellow); | |
155 | } else if (lCell.getForeColor().equals(jexer.bits.Color.CYAN)) { | |
156 | gr.setColor(Color.cyan); | |
157 | } else if (lCell.getForeColor().equals(jexer.bits.Color.MAGENTA)) { | |
158 | gr.setColor(Color.magenta); | |
159 | } else if (lCell.getForeColor().equals(jexer.bits.Color.WHITE)) { | |
160 | gr.setColor(Color.white); | |
161 | } | |
162 | char [] chars = new char[1]; | |
163 | chars[0] = lCell.getChar(); | |
164 | gr.drawChars(chars, 0, 1, xPixel, | |
165 | yPixel + textHeight - 2); | |
166 | ||
167 | // Physical is always updated | |
168 | physical[x][y].setTo(lCell); | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * The raw AWT Frame. | |
177 | */ | |
178 | private AWTFrame frame; | |
179 | ||
180 | /** | |
181 | * Public constructor. | |
182 | */ | |
183 | public AWTScreen() { | |
184 | frame = new AWTFrame(); | |
185 | frame.screen = this; | |
186 | frame.resizeToScreen(); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Push the logical screen to the physical device. | |
191 | */ | |
192 | @Override | |
193 | public void flushPhysical() { | |
194 | Graphics gr = frame.getGraphics(); | |
195 | frame.paint(gr); | |
196 | } | |
197 | } |