clean readme
[nikiroo-utils.git] / src / jexer / io / SwingScreen.java
CommitLineData
daa4106c 1/*
1ac2ccb1
KL
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 */
31package jexer.io;
32
1ac2ccb1
KL
33import java.awt.Color;
34import java.awt.Cursor;
35import java.awt.Font;
36import java.awt.FontMetrics;
1ac2ccb1 37import java.awt.Graphics;
84614868 38import java.awt.Insets;
30bd4abd
KL
39import java.awt.Point;
40import java.awt.Rectangle;
41import java.awt.Toolkit;
1ac2ccb1 42import java.awt.geom.Rectangle2D;
30bd4abd 43import java.awt.image.BufferedImage;
e3dfbd23 44import java.awt.image.BufferStrategy;
84614868 45import java.io.InputStream;
b5f2a6db 46import java.util.Date;
aed33687
KL
47import javax.swing.JFrame;
48import javax.swing.SwingUtilities;
1ac2ccb1 49
b5f2a6db
KL
50import jexer.bits.Cell;
51import jexer.bits.CellAttributes;
52import jexer.session.SwingSessionInfo;
53
1ac2ccb1 54/**
a4406f4e 55 * This Screen implementation draws to a Java Swing JFrame.
1ac2ccb1 56 */
a4406f4e 57public final class SwingScreen extends Screen {
1ac2ccb1 58
e3dfbd23
KL
59 /**
60 * If true, use double buffering thread.
61 */
62 private static final boolean doubleBuffer = true;
63
b5f2a6db
KL
64 /**
65 * Cursor style to draw.
66 */
67 public enum CursorStyle {
68 /**
69 * Use an underscore for the cursor.
70 */
71 UNDERLINE,
72
73 /**
74 * Use a solid block for the cursor.
75 */
76 BLOCK,
77
78 /**
79 * Use an outlined block for the cursor.
80 */
81 OUTLINE
82 }
83
84614868
KL
84 private static Color MYBLACK;
85 private static Color MYRED;
86 private static Color MYGREEN;
87 private static Color MYYELLOW;
88 private static Color MYBLUE;
89 private static Color MYMAGENTA;
90 private static Color MYCYAN;
91 private static Color MYWHITE;
92
93 private static Color MYBOLD_BLACK;
94 private static Color MYBOLD_RED;
95 private static Color MYBOLD_GREEN;
96 private static Color MYBOLD_YELLOW;
97 private static Color MYBOLD_BLUE;
98 private static Color MYBOLD_MAGENTA;
99 private static Color MYBOLD_CYAN;
100 private static Color MYBOLD_WHITE;
101
102 private static boolean dosColors = false;
103
104 /**
a4406f4e 105 * Setup Swing colors to match DOS color palette.
84614868
KL
106 */
107 private static void setDOSColors() {
108 if (dosColors) {
109 return;
110 }
111 MYBLACK = new Color(0x00, 0x00, 0x00);
112 MYRED = new Color(0xa8, 0x00, 0x00);
113 MYGREEN = new Color(0x00, 0xa8, 0x00);
114 MYYELLOW = new Color(0xa8, 0x54, 0x00);
115 MYBLUE = new Color(0x00, 0x00, 0xa8);
116 MYMAGENTA = new Color(0xa8, 0x00, 0xa8);
117 MYCYAN = new Color(0x00, 0xa8, 0xa8);
118 MYWHITE = new Color(0xa8, 0xa8, 0xa8);
119 MYBOLD_BLACK = new Color(0x54, 0x54, 0x54);
120 MYBOLD_RED = new Color(0xfc, 0x54, 0x54);
121 MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54);
122 MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54);
123 MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc);
124 MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc);
125 MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc);
126 MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc);
127
128 dosColors = true;
129 }
130
1ac2ccb1 131 /**
a4406f4e 132 * SwingFrame is our top-level hook into the Swing system.
1ac2ccb1 133 */
a4406f4e 134 class SwingFrame extends JFrame {
1ac2ccb1 135
cf9af8df
KL
136 /**
137 * Serializable version.
138 */
139 private static final long serialVersionUID = 1;
140
84614868
KL
141 /**
142 * The terminus font resource filename.
143 */
144 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
145
e3dfbd23
KL
146 /**
147 * The BufferStrategy object needed for double-buffering.
148 */
149 private BufferStrategy bufferStrategy;
150
1ac2ccb1
KL
151 /**
152 * The TUI Screen data.
153 */
a4406f4e 154 SwingScreen screen;
1ac2ccb1
KL
155
156 /**
157 * Width of a character cell.
158 */
159 private int textWidth = 1;
160
161 /**
162 * Height of a character cell.
163 */
164 private int textHeight = 1;
165
84614868
KL
166 /**
167 * Descent of a character cell.
168 */
169 private int maxDescent = 0;
170
847a4bc5
KL
171 /**
172 * System-dependent Y adjustment for text in the character cell.
173 */
174 private int textAdjustY = 0;
175
176 /**
177 * System-dependent X adjustment for text in the character cell.
178 */
179 private int textAdjustX = 0;
180
1ac2ccb1 181 /**
b5f2a6db 182 * Top pixel absolute location.
1ac2ccb1
KL
183 */
184 private int top = 30;
84614868 185
1ac2ccb1 186 /**
b5f2a6db 187 * Left pixel absolute location.
1ac2ccb1
KL
188 */
189 private int left = 30;
84614868 190
b5f2a6db
KL
191 /**
192 * The cursor style to draw.
193 */
194 private CursorStyle cursorStyle = CursorStyle.UNDERLINE;
195
196 /**
197 * The number of millis to wait before switching the blink from
198 * visible to invisible.
199 */
200 private long blinkMillis = 500;
201
202 /**
203 * If true, the cursor should be visible right now based on the blink
204 * time.
205 */
206 private boolean cursorBlinkVisible = true;
207
208 /**
209 * The time that the blink last flipped from visible to invisible or
210 * from invisible to visible.
211 */
212 private long lastBlinkTime = 0;
213
84614868 214 /**
a4406f4e 215 * Convert a CellAttributes foreground color to an Swing Color.
84614868
KL
216 *
217 * @param attr the text attributes
a4406f4e 218 * @return the Swing Color
84614868
KL
219 */
220 private Color attrToForegroundColor(final CellAttributes attr) {
7c870d89 221 if (attr.isBold()) {
84614868
KL
222 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
223 return MYBOLD_BLACK;
224 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
225 return MYBOLD_RED;
226 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
227 return MYBOLD_BLUE;
228 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
229 return MYBOLD_GREEN;
230 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
231 return MYBOLD_YELLOW;
232 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
233 return MYBOLD_CYAN;
234 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
235 return MYBOLD_MAGENTA;
236 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
237 return MYBOLD_WHITE;
238 }
239 } else {
240 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
241 return MYBLACK;
242 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
243 return MYRED;
244 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
245 return MYBLUE;
246 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
247 return MYGREEN;
248 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
249 return MYYELLOW;
250 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
251 return MYCYAN;
252 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
253 return MYMAGENTA;
254 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
255 return MYWHITE;
256 }
257 }
258 throw new IllegalArgumentException("Invalid color: " + attr.getForeColor().getValue());
259 }
260
261 /**
a4406f4e 262 * Convert a CellAttributes background color to an Swing Color.
84614868
KL
263 *
264 * @param attr the text attributes
a4406f4e 265 * @return the Swing Color
84614868
KL
266 */
267 private Color attrToBackgroundColor(final CellAttributes attr) {
84614868
KL
268 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
269 return MYBLACK;
270 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
271 return MYRED;
272 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
273 return MYBLUE;
274 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
275 return MYGREEN;
276 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
277 return MYYELLOW;
278 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
279 return MYCYAN;
280 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
281 return MYMAGENTA;
282 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
283 return MYWHITE;
284 }
285 throw new IllegalArgumentException("Invalid color: " + attr.getBackColor().getValue());
286 }
287
1ac2ccb1
KL
288 /**
289 * Public constructor.
84614868
KL
290 *
291 * @param screen the Screen that Backend talks to
1ac2ccb1 292 */
a4406f4e 293 public SwingFrame(final SwingScreen screen) {
84614868
KL
294 this.screen = screen;
295 setDOSColors();
296
b5f2a6db
KL
297 // Figure out my cursor style
298 String cursorStyleString = System.getProperty("jexer.Swing.cursorStyle",
299 "underline").toLowerCase();
300
301 if (cursorStyleString.equals("underline")) {
302 cursorStyle = CursorStyle.UNDERLINE;
303 } else if (cursorStyleString.equals("outline")) {
304 cursorStyle = CursorStyle.OUTLINE;
305 } else if (cursorStyleString.equals("block")) {
306 cursorStyle = CursorStyle.BLOCK;
307 }
308
1ac2ccb1 309 setTitle("Jexer Application");
aed33687 310 setBackground(Color.black);
84614868
KL
311
312 try {
30bd4abd 313 // Always try to use Terminus, the one decent font.
84614868
KL
314 ClassLoader loader = Thread.currentThread().getContextClassLoader();
315 InputStream in = loader.getResourceAsStream(FONTFILE);
316 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
317 Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22);
318 setFont(terminus);
319 } catch (Exception e) {
320 e.printStackTrace();
321 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
322 setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
323 }
aed33687 324 pack();
30bd4abd
KL
325
326 // Kill the X11 cursor
327 // Transparent 16 x 16 pixel cursor image.
328 BufferedImage cursorImg = new BufferedImage(16, 16,
329 BufferedImage.TYPE_INT_ARGB);
30bd4abd
KL
330 // Create a new blank cursor.
331 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
332 cursorImg, new Point(0, 0), "blank cursor");
333 setCursor(blankCursor);
091d8a06
KL
334
335 // Be capable of seeing Tab / Shift-Tab
336 setFocusTraversalKeysEnabled(false);
337
338 // Save the text cell width/height
bd8d51fa 339 getFontDimensions();
e3dfbd23
KL
340
341 // Setup double-buffering
0d47c546 342 if (SwingScreen.doubleBuffer) {
e3dfbd23
KL
343 setIgnoreRepaint(true);
344 createBufferStrategy(2);
345 bufferStrategy = getBufferStrategy();
346 }
1ac2ccb1
KL
347 }
348
349 /**
bd8d51fa 350 * Figure out my font dimensions.
1ac2ccb1 351 */
bd8d51fa 352 private void getFontDimensions() {
84614868
KL
353 Graphics gr = getGraphics();
354 FontMetrics fm = gr.getFontMetrics();
355 maxDescent = fm.getMaxDescent();
356 Rectangle2D bounds = fm.getMaxCharBounds(gr);
357 int leading = fm.getLeading();
358 textWidth = (int)Math.round(bounds.getWidth());
359 textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
360 // This also produces the same number, but works better for ugly
361 // monospace.
362 textHeight = fm.getMaxAscent() + maxDescent - leading;
847a4bc5
KL
363
364 if (System.getProperty("os.name").startsWith("Windows")) {
365 textAdjustY = -1;
366 textAdjustX = 0;
367 }
bd8d51fa 368 }
84614868 369
bd8d51fa
KL
370 /**
371 * Resize to font dimensions.
372 */
373 public void resizeToScreen() {
84614868
KL
374 // Figure out the thickness of borders and use that to set the
375 // final size.
376 Insets insets = getInsets();
377 left = insets.left;
378 top = insets.top;
379
380 setSize(textWidth * screen.width + insets.left + insets.right,
381 textHeight * screen.height + insets.top + insets.bottom);
1ac2ccb1
KL
382 }
383
30bd4abd
KL
384 /**
385 * Update redraws the whole screen.
386 *
a4406f4e 387 * @param gr the Swing Graphics context
30bd4abd
KL
388 */
389 @Override
390 public void update(final Graphics gr) {
391 // The default update clears the area. Don't do that, instead
392 // just paint it directly.
393 paint(gr);
394 }
395
1ac2ccb1
KL
396 /**
397 * Paint redraws the whole screen.
398 *
a4406f4e 399 * @param gr the Swing Graphics context
1ac2ccb1
KL
400 */
401 @Override
84614868 402 public void paint(final Graphics gr) {
87a17f3c
KL
403 // Do nothing until the screen reference has been set.
404 if (screen == null) {
405 return;
406 }
407 if (screen.frame == null) {
408 return;
409 }
410
b5f2a6db
KL
411 // See if it is time to flip the blink time.
412 long nowTime = (new Date()).getTime();
413 if (nowTime > blinkMillis + lastBlinkTime) {
414 lastBlinkTime = nowTime;
415 cursorBlinkVisible = !cursorBlinkVisible;
416 }
417
87a17f3c
KL
418 int xCellMin = 0;
419 int xCellMax = screen.width;
420 int yCellMin = 0;
421 int yCellMax = screen.height;
422
30bd4abd 423 Rectangle bounds = gr.getClipBounds();
87a17f3c
KL
424 if (bounds != null) {
425 // Only update what is in the bounds
426 xCellMin = screen.textColumn(bounds.x);
bd8d51fa 427 xCellMax = screen.textColumn(bounds.x + bounds.width);
87a17f3c
KL
428 if (xCellMax > screen.width) {
429 xCellMax = screen.width;
430 }
431 if (xCellMin >= xCellMax) {
432 xCellMin = xCellMax - 2;
433 }
434 if (xCellMin < 0) {
435 xCellMin = 0;
436 }
437 yCellMin = screen.textRow(bounds.y);
bd8d51fa 438 yCellMax = screen.textRow(bounds.y + bounds.height);
87a17f3c
KL
439 if (yCellMax > screen.height) {
440 yCellMax = screen.height;
441 }
442 if (yCellMin >= yCellMax) {
443 yCellMin = yCellMax - 2;
444 }
445 if (yCellMin < 0) {
446 yCellMin = 0;
447 }
bd8d51fa
KL
448 } else {
449 // We need a total repaint
450 reallyCleared = true;
87a17f3c 451 }
1ac2ccb1 452
87a17f3c
KL
453 // Prevent updates to the screen's data from the TApplication
454 // threads.
455 synchronized (screen) {
456 /*
457 System.err.printf("bounds %s X %d %d Y %d %d\n",
458 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
459 */
460
461 for (int y = yCellMin; y < yCellMax; y++) {
462 for (int x = xCellMin; x < xCellMax; x++) {
463
464 int xPixel = x * textWidth + left;
465 int yPixel = y * textHeight + top;
466
467 Cell lCell = screen.logical[x][y];
468 Cell pCell = screen.physical[x][y];
469
55b4f29b
KL
470 if (!lCell.equals(pCell)
471 || lCell.isBlink()
472 || reallyCleared) {
e3dfbd23 473
55b4f29b
KL
474 Cell lCellColor = new Cell();
475 lCellColor.setTo(lCell);
476
477 // Check for reverse
478 if (lCell.isReverse()) {
479 lCellColor.setForeColor(lCell.getBackColor());
480 lCellColor.setBackColor(lCell.getForeColor());
481 }
e3dfbd23 482
87a17f3c
KL
483 // Draw the background rectangle, then the
484 // foreground character.
55b4f29b 485 gr.setColor(attrToBackgroundColor(lCellColor));
87a17f3c 486 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
55b4f29b
KL
487
488 // Handle blink and underline
489 if (!lCell.isBlink()
490 || (lCell.isBlink() && cursorBlinkVisible)
491 ) {
492 gr.setColor(attrToForegroundColor(lCellColor));
493 char [] chars = new char[1];
494 chars[0] = lCell.getChar();
847a4bc5
KL
495 gr.drawChars(chars, 0, 1, xPixel + textAdjustX,
496 yPixel + textHeight - maxDescent
497 + textAdjustY);
498
55b4f29b
KL
499 if (lCell.isUnderline()) {
500 gr.fillRect(xPixel, yPixel + textHeight - 2,
501 textWidth, 2);
502 }
503 }
87a17f3c
KL
504
505 // Physical is always updated
506 physical[x][y].setTo(lCell);
30bd4abd
KL
507 }
508 }
87a17f3c 509 }
84614868 510
87a17f3c 511 // Draw the cursor if it is visible
b5f2a6db 512 if (cursorVisible
87a17f3c
KL
513 && (cursorY <= screen.height - 1)
514 && (cursorX <= screen.width - 1)
b5f2a6db 515 && cursorBlinkVisible
87a17f3c
KL
516 ) {
517 int xPixel = cursorX * textWidth + left;
518 int yPixel = cursorY * textHeight + top;
519 Cell lCell = screen.logical[cursorX][cursorY];
520 gr.setColor(attrToForegroundColor(lCell));
b5f2a6db
KL
521 switch (cursorStyle) {
522 case UNDERLINE:
523 gr.fillRect(xPixel, yPixel + textHeight - 2,
524 textWidth, 2);
525 break;
526 case BLOCK:
527 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
528 break;
529 case OUTLINE:
530 gr.drawRect(xPixel, yPixel, textWidth - 1,
531 textHeight - 1);
532 break;
533 }
1ac2ccb1 534 }
30bd4abd 535
87a17f3c
KL
536 dirty = false;
537 reallyCleared = false;
538 } // synchronized (screen)
1ac2ccb1 539 }
bd8d51fa 540
a4406f4e 541 } // class SwingFrame
1ac2ccb1
KL
542
543 /**
a4406f4e 544 * The raw Swing JFrame. Note package private access.
1ac2ccb1 545 */
a4406f4e 546 SwingFrame frame;
1ac2ccb1 547
e3dfbd23
KL
548 /**
549 * Restore terminal to normal state.
550 */
551 public void shutdown() {
552 frame.dispose();
553 }
554
1ac2ccb1
KL
555 /**
556 * Public constructor.
557 */
a4406f4e 558 public SwingScreen() {
aed33687
KL
559 try {
560 SwingUtilities.invokeAndWait(new Runnable() {
561 public void run() {
a4406f4e
KL
562 SwingScreen.this.frame = new SwingFrame(SwingScreen.this);
563 SwingScreen.this.sessionInfo =
564 new SwingSessionInfo(SwingScreen.this.frame,
bd8d51fa
KL
565 frame.textWidth,
566 frame.textHeight);
567
a4406f4e 568 SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(),
bd8d51fa
KL
569 sessionInfo.getWindowHeight());
570
a4406f4e
KL
571 SwingScreen.this.frame.resizeToScreen();
572 SwingScreen.this.frame.setVisible(true);
aed33687
KL
573 }
574 } );
575 } catch (Exception e) {
576 e.printStackTrace();
577 }
1ac2ccb1
KL
578 }
579
bd8d51fa
KL
580 /**
581 * The sessionInfo.
582 */
a4406f4e 583 private SwingSessionInfo sessionInfo;
bd8d51fa 584
30bd4abd 585 /**
a4406f4e 586 * Create the SwingSessionInfo. Note package private access.
30bd4abd
KL
587 *
588 * @return the sessionInfo
589 */
a4406f4e 590 SwingSessionInfo getSessionInfo() {
30bd4abd
KL
591 return sessionInfo;
592 }
593
1ac2ccb1
KL
594 /**
595 * Push the logical screen to the physical device.
596 */
597 @Override
598 public void flushPhysical() {
87a17f3c
KL
599
600 if (reallyCleared) {
601 // Really refreshed, do it all
0d47c546 602 if (SwingScreen.doubleBuffer) {
e3dfbd23
KL
603 Graphics gr = frame.bufferStrategy.getDrawGraphics();
604 frame.paint(gr);
605 gr.dispose();
606 frame.bufferStrategy.show();
607 Toolkit.getDefaultToolkit().sync();
608 } else {
609 frame.repaint();
610 }
87a17f3c
KL
611 return;
612 }
613
614 // Do nothing if nothing happened.
615 if (!dirty) {
616 return;
617 }
618
30bd4abd
KL
619 // Request a repaint, let the frame's repaint/update methods do the
620 // right thing.
87a17f3c 621
30bd4abd
KL
622 // Find the minimum-size damaged region.
623 int xMin = frame.getWidth();
624 int xMax = 0;
625 int yMin = frame.getHeight();
626 int yMax = 0;
30bd4abd 627
87a17f3c
KL
628 synchronized (this) {
629 for (int y = 0; y < height; y++) {
630 for (int x = 0; x < width; x++) {
631 Cell lCell = logical[x][y];
632 Cell pCell = physical[x][y];
633
634 int xPixel = x * frame.textWidth + frame.left;
635 int yPixel = y * frame.textHeight + frame.top;
636
637 if (!lCell.equals(pCell)
638 || ((x == cursorX)
639 && (y == cursorY)
640 && cursorVisible)
55b4f29b 641 || lCell.isBlink()
87a17f3c
KL
642 ) {
643 if (xPixel < xMin) {
644 xMin = xPixel;
645 }
646 if (xPixel + frame.textWidth > xMax) {
647 xMax = xPixel + frame.textWidth;
648 }
649 if (yPixel < yMin) {
650 yMin = yPixel;
651 }
652 if (yPixel + frame.textHeight > yMax) {
653 yMax = yPixel + frame.textHeight;
654 }
30bd4abd
KL
655 }
656 }
657 }
658 }
87a17f3c
KL
659 if (xMin + frame.textWidth >= xMax) {
660 xMax += frame.textWidth;
661 }
662 if (yMin + frame.textHeight >= yMax) {
663 yMax += frame.textHeight;
664 }
30bd4abd 665
87a17f3c 666 // Repaint the desired area
e3dfbd23
KL
667 // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
668 // yMin, yMax);
0d47c546 669 if (SwingScreen.doubleBuffer) {
e3dfbd23
KL
670 Graphics gr = frame.bufferStrategy.getDrawGraphics();
671 Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin,
672 yMax - yMin);
673 gr.setClip(bounds);
674 frame.paint(gr);
675 gr.dispose();
676 frame.bufferStrategy.show();
677 Toolkit.getDefaultToolkit().sync();
678 } else {
679 frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
680 }
1ac2ccb1 681 }
30bd4abd
KL
682
683 /**
684 * Put the cursor at (x,y).
685 *
686 * @param visible if true, the cursor should be visible
687 * @param x column coordinate to put the cursor on
688 * @param y row coordinate to put the cursor on
689 */
690 @Override
691 public void putCursor(final boolean visible, final int x, final int y) {
b5f2a6db
KL
692
693 if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) {
694 // See if it is time to flip the blink time.
695 long nowTime = (new Date()).getTime();
696 if (nowTime < frame.blinkMillis + frame.lastBlinkTime) {
697 // Nothing has changed, so don't do anything.
698 return;
699 }
700 }
701
702 if (cursorVisible
30bd4abd
KL
703 && (cursorY <= height - 1)
704 && (cursorX <= width - 1)
705 ) {
706 // Make the current cursor position dirty
87a17f3c 707 if (physical[cursorX][cursorY].getChar() == 'Q') {
30bd4abd
KL
708 physical[cursorX][cursorY].setChar('X');
709 } else {
87a17f3c 710 physical[cursorX][cursorY].setChar('Q');
30bd4abd
KL
711 }
712 }
713
714 super.putCursor(visible, x, y);
715 }
716
87a17f3c
KL
717 /**
718 * Convert pixel column position to text cell column position.
719 *
720 * @param x pixel column position
721 * @return text cell column position
722 */
723 public int textColumn(final int x) {
724 return ((x - frame.left) / frame.textWidth);
725 }
726
727 /**
728 * Convert pixel row position to text cell row position.
729 *
730 * @param y pixel row position
731 * @return text cell row position
732 */
733 public int textRow(final int y) {
734 return ((y - frame.top) / frame.textHeight);
735 }
736
1ac2ccb1 737}