2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.Cell
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.session
.AWTSessionInfo
;
37 import java
.awt
.Color
;
38 import java
.awt
.Cursor
;
40 import java
.awt
.FontMetrics
;
41 import java
.awt
.Frame
;
42 import java
.awt
.Graphics
;
43 import java
.awt
.Insets
;
44 import java
.awt
.Point
;
45 import java
.awt
.Rectangle
;
46 import java
.awt
.Toolkit
;
47 import java
.awt
.geom
.Rectangle2D
;
48 import java
.awt
.image
.BufferedImage
;
49 import java
.io
.InputStream
;
52 * This Screen implementation draws to a Java AWT Frame.
54 public final class AWTScreen
extends Screen
{
56 private static Color MYBLACK
;
57 private static Color MYRED
;
58 private static Color MYGREEN
;
59 private static Color MYYELLOW
;
60 private static Color MYBLUE
;
61 private static Color MYMAGENTA
;
62 private static Color MYCYAN
;
63 private static Color MYWHITE
;
65 private static Color MYBOLD_BLACK
;
66 private static Color MYBOLD_RED
;
67 private static Color MYBOLD_GREEN
;
68 private static Color MYBOLD_YELLOW
;
69 private static Color MYBOLD_BLUE
;
70 private static Color MYBOLD_MAGENTA
;
71 private static Color MYBOLD_CYAN
;
72 private static Color MYBOLD_WHITE
;
74 private static boolean dosColors
= false;
77 * Setup AWT colors to match DOS color palette.
79 private static void setDOSColors() {
83 MYBLACK
= new Color(0x00, 0x00, 0x00);
84 MYRED
= new Color(0xa8, 0x00, 0x00);
85 MYGREEN
= new Color(0x00, 0xa8, 0x00);
86 MYYELLOW
= new Color(0xa8, 0x54, 0x00);
87 MYBLUE
= new Color(0x00, 0x00, 0xa8);
88 MYMAGENTA
= new Color(0xa8, 0x00, 0xa8);
89 MYCYAN
= new Color(0x00, 0xa8, 0xa8);
90 MYWHITE
= new Color(0xa8, 0xa8, 0xa8);
91 MYBOLD_BLACK
= new Color(0x54, 0x54, 0x54);
92 MYBOLD_RED
= new Color(0xfc, 0x54, 0x54);
93 MYBOLD_GREEN
= new Color(0x54, 0xfc, 0x54);
94 MYBOLD_YELLOW
= new Color(0xfc, 0xfc, 0x54);
95 MYBOLD_BLUE
= new Color(0x54, 0x54, 0xfc);
96 MYBOLD_MAGENTA
= new Color(0xfc, 0x54, 0xfc);
97 MYBOLD_CYAN
= new Color(0x54, 0xfc, 0xfc);
98 MYBOLD_WHITE
= new Color(0xfc, 0xfc, 0xfc);
104 * AWTFrame is our top-level hook into the AWT system.
106 class AWTFrame
extends Frame
{
109 * The terminus font resource filename.
111 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
114 * The TUI Screen data.
119 * Width of a character cell.
121 private int textWidth
= 1;
124 * Height of a character cell.
126 private int textHeight
= 1;
129 * Descent of a character cell.
131 private int maxDescent
= 0;
136 private int top
= 30;
141 private int left
= 30;
144 * Convert a CellAttributes foreground color to an AWT Color.
146 * @param attr the text attributes
147 * @return the AWT Color
149 private Color
attrToForegroundColor(final CellAttributes attr
) {
156 if (attr
.getBold()) {
157 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
159 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
161 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
163 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
165 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
166 return MYBOLD_YELLOW
;
167 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
169 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
170 return MYBOLD_MAGENTA
;
171 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
175 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
177 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
179 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
181 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
183 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
185 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
187 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
189 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
193 throw new IllegalArgumentException("Invalid color: " + attr
.getForeColor().getValue());
197 * Convert a CellAttributes background color to an AWT Color.
199 * @param attr the text attributes
200 * @return the AWT Color
202 private Color
attrToBackgroundColor(final CellAttributes attr
) {
209 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
211 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
213 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
215 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
217 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
219 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
221 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
223 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
226 throw new IllegalArgumentException("Invalid color: " + attr
.getBackColor().getValue());
230 * Public constructor.
232 * @param screen the Screen that Backend talks to
234 public AWTFrame(final AWTScreen screen
) {
235 this.screen
= screen
;
238 setTitle("Jexer Application");
239 setBackground(java
.awt
.Color
.black
);
240 // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
241 // setFont(new Font("Liberation Mono", Font.BOLD, 16));
242 // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
245 // Always try to use Terminus, the one decent font.
246 ClassLoader loader
= Thread
.currentThread().getContextClassLoader();
247 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
248 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
249 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 22);
251 } catch (Exception e
) {
253 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
254 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
259 // Kill the X11 cursor
260 // Transparent 16 x 16 pixel cursor image.
261 BufferedImage cursorImg
= new BufferedImage(16, 16,
262 BufferedImage
.TYPE_INT_ARGB
);
264 // Create a new blank cursor.
265 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
266 cursorImg
, new Point(0, 0), "blank cursor");
267 setCursor(blankCursor
);
271 * Resize to font dimensions.
273 public void resizeToScreen() {
274 Graphics gr
= getGraphics();
275 FontMetrics fm
= gr
.getFontMetrics();
276 maxDescent
= fm
.getMaxDescent();
277 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
278 int leading
= fm
.getLeading();
279 textWidth
= (int)Math
.round(bounds
.getWidth());
280 textHeight
= (int)Math
.round(bounds
.getHeight()) - maxDescent
;
281 // This also produces the same number, but works better for ugly
283 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
285 // Figure out the thickness of borders and use that to set the
287 Insets insets
= getInsets();
291 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
292 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
295 System.err.printf("W: %d H: %d MD: %d L: %d\n", textWidth,
296 textHeight, maxDescent, leading);
301 * Update redraws the whole screen.
303 * @param gr the AWT Graphics context
306 public void update(final Graphics gr
) {
307 // The default update clears the area. Don't do that, instead
308 // just paint it directly.
313 * Paint redraws the whole screen.
315 * @param gr the AWT Graphics context
318 public void paint(final Graphics gr
) {
319 Rectangle bounds
= gr
.getClipBounds();
321 for (int y
= 0; y
< screen
.height
; y
++) {
322 for (int x
= 0; x
< screen
.width
; x
++) {
323 int xPixel
= x
* textWidth
+ left
;
324 int yPixel
= y
* textHeight
+ top
;
326 Cell lCell
= screen
.logical
[x
][y
];
327 Cell pCell
= screen
.physical
[x
][y
];
329 boolean inBounds
= true;
330 if (bounds
!= null) {
331 if (bounds
.contains(xPixel
, yPixel
)
332 || bounds
.contains(xPixel
+ textWidth
, yPixel
)
333 || bounds
.contains(xPixel
, yPixel
+ textHeight
)
334 || bounds
.contains(xPixel
+ textWidth
,
337 // This area is damaged and will definitely be
343 if (!lCell
.equals(pCell
) || inBounds
) {
344 // Draw the background rectangle, then the foreground
346 gr
.setColor(attrToBackgroundColor(lCell
));
347 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
348 gr
.setColor(attrToForegroundColor(lCell
));
349 char [] chars
= new char[1];
350 chars
[0] = lCell
.getChar();
351 gr
.drawChars(chars
, 0, 1, xPixel
,
352 yPixel
+ textHeight
- maxDescent
);
354 // Physical is always updated
355 physical
[x
][y
].setTo(lCell
);
360 // Draw the cursor if it is visible
362 && (cursorY
<= screen
.height
- 1)
363 && (cursorX
<= screen
.width
- 1)
365 int xPixel
= cursorX
* textWidth
+ left
;
366 int yPixel
= cursorY
* textHeight
+ top
;
367 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
368 gr
.setColor(attrToForegroundColor(lCell
));
369 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
375 * The raw AWT Frame. Note package private access.
380 * Public constructor.
383 frame
= new AWTFrame(this);
387 * Create the AWTSessionInfo. Note package private access.
389 * @return the sessionInfo
391 AWTSessionInfo
getSessionInfo() {
392 AWTSessionInfo sessionInfo
= new AWTSessionInfo(frame
, frame
.textWidth
,
398 * Push the logical screen to the physical device.
401 public void flushPhysical() {
402 // Request a repaint, let the frame's repaint/update methods do the
404 // Find the minimum-size damaged region.
405 int xMin
= frame
.getWidth();
407 int yMin
= frame
.getHeight();
409 for (int y
= 0; y
< height
; y
++) {
410 for (int x
= 0; x
< width
; x
++) {
411 Cell lCell
= logical
[x
][y
];
412 Cell pCell
= physical
[x
][y
];
414 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
415 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
417 if (!lCell
.equals(pCell
)
418 || ((x
== cursorX
) && (y
== cursorY
))
423 if (xPixel
+ frame
.textWidth
> xMax
) {
424 xMax
= xPixel
+ frame
.textWidth
;
429 if (yPixel
+ frame
.textHeight
> yMax
) {
430 yMax
= yPixel
+ frame
.textHeight
;
436 // Ask for a repaint sometime in the next 10 millis.
437 frame
.repaint(10, xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
441 * Put the cursor at (x,y).
443 * @param visible if true, the cursor should be visible
444 * @param x column coordinate to put the cursor on
445 * @param y row coordinate to put the cursor on
448 public void putCursor(final boolean visible
, final int x
, final int y
) {
450 && (cursorY
<= height
- 1)
451 && (cursorX
<= width
- 1)
453 // Make the current cursor position dirty
454 if (physical
[cursorX
][cursorY
].getChar() == ' ') {
455 physical
[cursorX
][cursorY
].setChar('X');
457 physical
[cursorX
][cursorY
].setChar(' ');
461 super.putCursor(visible
, x
, y
);