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
;
36 import java
.awt
.Color
;
37 import java
.awt
.Cursor
;
39 import java
.awt
.FontMetrics
;
40 import java
.awt
.Frame
;
41 import java
.awt
.Graphics
;
42 import java
.awt
.geom
.Rectangle2D
;
45 * This Screen implementation draws to a Java AWT Frame.
47 public final class AWTScreen
extends Screen
{
50 * AWTFrame is our top-level hook into the AWT system.
52 class AWTFrame
extends Frame
{
55 * The TUI Screen data.
60 * Width of a character cell.
62 private int textWidth
= 1;
65 * Height of a character cell.
67 private int textHeight
= 1;
77 private int left
= 30;
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));
93 * Resize to font dimensions.
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
));
103 System
.err
.printf("W: %d H: %d\n", textWidth
, textHeight
);
107 * Paint redraws the whole screen.
109 * @param gr the AWT Graphics context
112 public void paint(Graphics gr
) {
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
];
119 int xPixel
= x
* (textWidth
+ 1) + left
;
120 int yPixel
= y
* (textHeight
+ 1) + top
- y
;
122 if (!lCell
.equals(pCell
)) {
123 // Draw the background rectangle, then the foreground
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
);
142 gr
.fillRect(xPixel
, yPixel
, textWidth
+ 1,
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
);
162 char [] chars
= new char[1];
163 chars
[0] = lCell
.getChar();
164 gr
.drawChars(chars
, 0, 1, xPixel
,
165 yPixel
+ textHeight
- 2);
167 // Physical is always updated
168 physical
[x
][y
].setTo(lCell
);
178 private AWTFrame frame
;
181 * Public constructor.
184 frame
= new AWTFrame();
186 frame
.resizeToScreen();
190 * Push the logical screen to the physical device.
193 public void flushPhysical() {
194 Graphics gr
= frame
.getGraphics();