2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.Color
;
32 import java
.awt
.Cursor
;
34 import java
.awt
.FontMetrics
;
35 import java
.awt
.Graphics
;
36 import java
.awt
.Graphics2D
;
37 import java
.awt
.Insets
;
38 import java
.awt
.Point
;
39 import java
.awt
.Rectangle
;
40 import java
.awt
.Toolkit
;
41 import java
.awt
.geom
.Rectangle2D
;
42 import java
.awt
.image
.BufferedImage
;
43 import java
.awt
.image
.BufferStrategy
;
44 import java
.io
.InputStream
;
45 import java
.util
.Date
;
46 import java
.util
.HashMap
;
47 import javax
.swing
.JFrame
;
48 import javax
.swing
.SwingUtilities
;
50 import jexer
.bits
.Cell
;
51 import jexer
.bits
.CellAttributes
;
52 import jexer
.session
.SwingSessionInfo
;
55 * This Screen implementation draws to a Java Swing JFrame.
57 public final class SwingScreen
extends Screen
{
60 * If true, use triple buffering thread.
62 private static boolean tripleBuffer
= true;
65 * Cursor style to draw.
67 public enum CursorStyle
{
69 * Use an underscore for the cursor.
74 * Use a solid block for the cursor.
79 * Use an outlined block for the cursor.
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
;
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
;
102 private static boolean dosColors
= false;
105 * Setup Swing colors to match DOS color palette.
107 private static void setDOSColors() {
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);
132 * SwingFrame is our top-level hook into the Swing system.
134 class SwingFrame
extends JFrame
{
137 * Serializable version.
139 private static final long serialVersionUID
= 1;
142 * The terminus font resource filename.
144 private static final String FONTFILE
= "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
147 * The BufferStrategy object needed for triple-buffering.
149 private BufferStrategy bufferStrategy
;
152 * A cache of previously-rendered glyphs for blinking text, when it
155 private HashMap
<Cell
, BufferedImage
> glyphCacheBlink
;
158 * A cache of previously-rendered glyphs for non-blinking, or
159 * blinking-and-visible, text.
161 private HashMap
<Cell
, BufferedImage
> glyphCache
;
164 * The TUI Screen data.
169 * If true, we were successful getting Terminus.
171 private boolean gotTerminus
= false;
174 * Width of a character cell.
176 private int textWidth
= 1;
179 * Height of a character cell.
181 private int textHeight
= 1;
184 * Descent of a character cell.
186 private int maxDescent
= 0;
189 * System-dependent Y adjustment for text in the character cell.
191 private int textAdjustY
= 0;
194 * System-dependent X adjustment for text in the character cell.
196 private int textAdjustX
= 0;
199 * Top pixel absolute location.
201 private int top
= 30;
204 * Left pixel absolute location.
206 private int left
= 30;
209 * The cursor style to draw.
211 private CursorStyle cursorStyle
= CursorStyle
.UNDERLINE
;
214 * The number of millis to wait before switching the blink from
215 * visible to invisible.
217 private long blinkMillis
= 500;
220 * If true, the cursor should be visible right now based on the blink
223 private boolean cursorBlinkVisible
= true;
226 * The time that the blink last flipped from visible to invisible or
227 * from invisible to visible.
229 private long lastBlinkTime
= 0;
232 * Convert a CellAttributes foreground color to an Swing Color.
234 * @param attr the text attributes
235 * @return the Swing Color
237 private Color
attrToForegroundColor(final CellAttributes attr
) {
239 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
241 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
243 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
245 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
247 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
248 return MYBOLD_YELLOW
;
249 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
251 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
252 return MYBOLD_MAGENTA
;
253 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
257 if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLACK
)) {
259 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.RED
)) {
261 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.BLUE
)) {
263 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.GREEN
)) {
265 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.YELLOW
)) {
267 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.CYAN
)) {
269 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
271 } else if (attr
.getForeColor().equals(jexer
.bits
.Color
.WHITE
)) {
275 throw new IllegalArgumentException("Invalid color: " +
276 attr
.getForeColor().getValue());
280 * Convert a CellAttributes background color to an Swing Color.
282 * @param attr the text attributes
283 * @return the Swing Color
285 private Color
attrToBackgroundColor(final CellAttributes attr
) {
286 if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLACK
)) {
288 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.RED
)) {
290 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.BLUE
)) {
292 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.GREEN
)) {
294 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.YELLOW
)) {
296 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.CYAN
)) {
298 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.MAGENTA
)) {
300 } else if (attr
.getBackColor().equals(jexer
.bits
.Color
.WHITE
)) {
303 throw new IllegalArgumentException("Invalid color: " +
304 attr
.getBackColor().getValue());
308 * Public constructor.
310 * @param screen the Screen that Backend talks to
312 public SwingFrame(final SwingScreen screen
) {
313 this.screen
= screen
;
316 // Figure out my cursor style
317 String cursorStyleString
= System
.getProperty(
318 "jexer.Swing.cursorStyle", "underline").toLowerCase();
320 if (cursorStyleString
.equals("underline")) {
321 cursorStyle
= CursorStyle
.UNDERLINE
;
322 } else if (cursorStyleString
.equals("outline")) {
323 cursorStyle
= CursorStyle
.OUTLINE
;
324 } else if (cursorStyleString
.equals("block")) {
325 cursorStyle
= CursorStyle
.BLOCK
;
328 if (System
.getProperty("jexer.Swing.tripleBuffer") != null) {
329 if (System
.getProperty("jexer.Swing.tripleBuffer").
332 SwingScreen
.tripleBuffer
= false;
336 setTitle("Jexer Application");
337 setBackground(Color
.black
);
340 // Always try to use Terminus, the one decent font.
341 ClassLoader loader
= Thread
.currentThread().
342 getContextClassLoader();
343 InputStream in
= loader
.getResourceAsStream(FONTFILE
);
344 Font terminusRoot
= Font
.createFont(Font
.TRUETYPE_FONT
, in
);
345 Font terminus
= terminusRoot
.deriveFont(Font
.PLAIN
, 20);
348 } catch (Exception e
) {
350 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
351 setFont(new Font(Font
.MONOSPACED
, Font
.PLAIN
, 24));
355 // Kill the X11 cursor
356 // Transparent 16 x 16 pixel cursor image.
357 BufferedImage cursorImg
= new BufferedImage(16, 16,
358 BufferedImage
.TYPE_INT_ARGB
);
359 // Create a new blank cursor.
360 Cursor blankCursor
= Toolkit
.getDefaultToolkit().createCustomCursor(
361 cursorImg
, new Point(0, 0), "blank cursor");
362 setCursor(blankCursor
);
364 // Be capable of seeing Tab / Shift-Tab
365 setFocusTraversalKeysEnabled(false);
367 // Save the text cell width/height
370 // Cache glyphs as they are rendered
371 glyphCacheBlink
= new HashMap
<Cell
, BufferedImage
>();
372 glyphCache
= new HashMap
<Cell
, BufferedImage
>();
374 // Setup triple-buffering
375 if (SwingScreen
.tripleBuffer
) {
376 setIgnoreRepaint(true);
377 createBufferStrategy(3);
378 bufferStrategy
= getBufferStrategy();
383 * Figure out what textAdjustX and textAdjustY should be, based on
384 * the location of a vertical bar (to find textAdjustY) and a
385 * horizontal bar (to find textAdjustX).
387 * @return true if textAdjustX and textAdjustY were guessed at
390 private boolean getFontAdjustments() {
391 BufferedImage image
= null;
393 // What SHOULD happen is that the topmost/leftmost white pixel is
394 // at position (gr2x, gr2y). But it might also be off by a pixel
395 // in either direction.
397 Graphics2D gr2
= null;
400 image
= new BufferedImage(textWidth
* 2, textHeight
* 2,
401 BufferedImage
.TYPE_INT_ARGB
);
403 gr2
= image
.createGraphics();
404 gr2
.setFont(getFont());
405 gr2
.setColor(java
.awt
.Color
.BLACK
);
406 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
407 gr2
.setColor(java
.awt
.Color
.WHITE
);
408 char [] chars
= new char[1];
409 chars
[0] = jexer
.bits
.GraphicsChars
.VERTICAL_BAR
;
410 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
413 for (int x
= 0; x
< textWidth
; x
++) {
414 for (int y
= 0; y
< textHeight
; y
++) {
417 System.err.println("X: " + x + " Y: " + y + " " +
421 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
422 textAdjustY
= (gr2y
- y
);
424 // System.err.println("textAdjustY: " + textAdjustY);
431 gr2
= image
.createGraphics();
432 gr2
.setFont(getFont());
433 gr2
.setColor(java
.awt
.Color
.BLACK
);
434 gr2
.fillRect(0, 0, textWidth
* 2, textHeight
* 2);
435 gr2
.setColor(java
.awt
.Color
.WHITE
);
436 chars
[0] = jexer
.bits
.GraphicsChars
.SINGLE_BAR
;
437 gr2
.drawChars(chars
, 0, 1, gr2x
, gr2y
+ textHeight
- maxDescent
);
440 for (int x
= 0; x
< textWidth
; x
++) {
441 for (int y
= 0; y
< textHeight
; y
++) {
444 System.err.println("X: " + x + " Y: " + y + " " +
448 if ((image
.getRGB(x
, y
) & 0xFFFFFF) != 0) {
449 textAdjustX
= (gr2x
- x
);
451 // System.err.println("textAdjustX: " + textAdjustX);
457 // Something weird happened, don't rely on this function.
458 // System.err.println("getFontAdjustments: false");
464 * Figure out my font dimensions.
466 private void getFontDimensions() {
467 Graphics gr
= getGraphics();
468 FontMetrics fm
= gr
.getFontMetrics();
469 maxDescent
= fm
.getMaxDescent();
470 Rectangle2D bounds
= fm
.getMaxCharBounds(gr
);
471 int leading
= fm
.getLeading();
472 textWidth
= (int)Math
.round(bounds
.getWidth());
473 // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
475 // This produces the same number, but works better for ugly
477 textHeight
= fm
.getMaxAscent() + maxDescent
- leading
;
479 if (gotTerminus
== true) {
483 if (getFontAdjustments() == false) {
484 // We were unable to programmatically determine textAdjustX
485 // and textAdjustY, so try some guesses based on VM vendor.
486 String runtime
= System
.getProperty("java.runtime.name");
487 if ((runtime
!= null) && (runtime
.contains("Java(TM)"))) {
495 * Resize to font dimensions.
497 public void resizeToScreen() {
498 // Figure out the thickness of borders and use that to set the
500 Insets insets
= getInsets();
504 setSize(textWidth
* screen
.width
+ insets
.left
+ insets
.right
,
505 textHeight
* screen
.height
+ insets
.top
+ insets
.bottom
);
509 * Update redraws the whole screen.
511 * @param gr the Swing Graphics context
514 public void update(final Graphics gr
) {
515 // The default update clears the area. Don't do that, instead
516 // just paint it directly.
521 * Draw one glyph to the screen.
523 * @param gr the Swing Graphics context
524 * @param cell the Cell to draw
525 * @param xPixel the x-coordinate to render to. 0 means the
526 * left-most pixel column.
527 * @param yPixel the y-coordinate to render to. 0 means the top-most
530 private void drawGlyph(final Graphics gr
, final Cell cell
,
531 final int xPixel
, final int yPixel
) {
533 BufferedImage image
= null;
534 if (cell
.isBlink() && !cursorBlinkVisible
) {
535 image
= glyphCacheBlink
.get(cell
);
537 image
= glyphCache
.get(cell
);
540 gr
.drawImage(image
, xPixel
, yPixel
, this);
544 // Generate glyph and draw it.
545 Graphics2D gr2
= null;
549 image
= new BufferedImage(textWidth
, textHeight
,
550 BufferedImage
.TYPE_INT_ARGB
);
551 gr2
= image
.createGraphics();
552 gr2
.setFont(getFont());
556 gr2
= (Graphics2D
) gr
;
559 Cell cellColor
= new Cell();
560 cellColor
.setTo(cell
);
563 if (cell
.isReverse()) {
564 cellColor
.setForeColor(cell
.getBackColor());
565 cellColor
.setBackColor(cell
.getForeColor());
568 // Draw the background rectangle, then the foreground character.
569 gr2
.setColor(attrToBackgroundColor(cellColor
));
570 gr2
.fillRect(gr2x
, gr2y
, textWidth
, textHeight
);
572 // Handle blink and underline
574 || (cell
.isBlink() && cursorBlinkVisible
)
576 gr2
.setColor(attrToForegroundColor(cellColor
));
577 char [] chars
= new char[1];
578 chars
[0] = cell
.getChar();
579 gr2
.drawChars(chars
, 0, 1, gr2x
+ textAdjustX
,
580 gr2y
+ textHeight
- maxDescent
+ textAdjustY
);
582 if (cell
.isUnderline()) {
583 gr2
.fillRect(gr2x
, gr2y
+ textHeight
- 2, textWidth
, 2);
590 // We need a new key that will not be mutated by
592 Cell key
= new Cell();
594 if (cell
.isBlink() && !cursorBlinkVisible
) {
595 glyphCacheBlink
.put(key
, image
);
597 glyphCache
.put(key
, image
);
600 gr
.drawImage(image
, xPixel
, yPixel
, this);
606 * Check if the cursor is visible, and if so draw it.
608 * @param gr the Swing Graphics context
610 private void drawCursor(final Graphics gr
) {
613 && (cursorY
<= screen
.height
- 1)
614 && (cursorX
<= screen
.width
- 1)
615 && cursorBlinkVisible
617 int xPixel
= cursorX
* textWidth
+ left
;
618 int yPixel
= cursorY
* textHeight
+ top
;
619 Cell lCell
= screen
.logical
[cursorX
][cursorY
];
620 gr
.setColor(attrToForegroundColor(lCell
));
621 switch (cursorStyle
) {
625 gr
.fillRect(xPixel
, yPixel
+ textHeight
- 2, textWidth
, 2);
628 gr
.fillRect(xPixel
, yPixel
, textWidth
, textHeight
);
631 gr
.drawRect(xPixel
, yPixel
, textWidth
- 1, textHeight
- 1);
638 * Paint redraws the whole screen.
640 * @param gr the Swing Graphics context
643 public void paint(final Graphics gr
) {
644 // Do nothing until the screen reference has been set.
645 if (screen
== null) {
648 if (screen
.frame
== null) {
652 // See if it is time to flip the blink time.
653 long nowTime
= (new Date()).getTime();
654 if (nowTime
> blinkMillis
+ lastBlinkTime
) {
655 lastBlinkTime
= nowTime
;
656 cursorBlinkVisible
= !cursorBlinkVisible
;
660 int xCellMax
= screen
.width
;
662 int yCellMax
= screen
.height
;
664 Rectangle bounds
= gr
.getClipBounds();
665 if (bounds
!= null) {
666 // Only update what is in the bounds
667 xCellMin
= screen
.textColumn(bounds
.x
);
668 xCellMax
= screen
.textColumn(bounds
.x
+ bounds
.width
);
669 if (xCellMax
> screen
.width
) {
670 xCellMax
= screen
.width
;
672 if (xCellMin
>= xCellMax
) {
673 xCellMin
= xCellMax
- 2;
678 yCellMin
= screen
.textRow(bounds
.y
);
679 yCellMax
= screen
.textRow(bounds
.y
+ bounds
.height
);
680 if (yCellMax
> screen
.height
) {
681 yCellMax
= screen
.height
;
683 if (yCellMin
>= yCellMax
) {
684 yCellMin
= yCellMax
- 2;
690 // We need a total repaint
691 reallyCleared
= true;
694 // Prevent updates to the screen's data from the TApplication
696 synchronized (screen
) {
698 System.err.printf("bounds %s X %d %d Y %d %d\n",
699 bounds, xCellMin, xCellMax, yCellMin, yCellMax);
702 for (int y
= yCellMin
; y
< yCellMax
; y
++) {
703 for (int x
= xCellMin
; x
< xCellMax
; x
++) {
705 int xPixel
= x
* textWidth
+ left
;
706 int yPixel
= y
* textHeight
+ top
;
708 Cell lCell
= screen
.logical
[x
][y
];
709 Cell pCell
= screen
.physical
[x
][y
];
711 if (!lCell
.equals(pCell
)
715 drawGlyph(gr
, lCell
, xPixel
, yPixel
);
717 // Physical is always updated
718 physical
[x
][y
].setTo(lCell
);
725 reallyCleared
= false;
726 } // synchronized (screen)
729 } // class SwingFrame
732 * The raw Swing JFrame. Note package private access.
737 * Restore terminal to normal state.
739 public void shutdown() {
744 * Public constructor.
746 public SwingScreen() {
748 SwingUtilities
.invokeAndWait(new Runnable() {
750 SwingScreen
.this.frame
= new SwingFrame(SwingScreen
.this);
751 SwingScreen
.this.sessionInfo
=
752 new SwingSessionInfo(SwingScreen
.this.frame
,
756 SwingScreen
.this.setDimensions(sessionInfo
.getWindowWidth(),
757 sessionInfo
.getWindowHeight());
759 SwingScreen
.this.frame
.resizeToScreen();
760 SwingScreen
.this.frame
.setVisible(true);
763 } catch (Exception e
) {
771 private SwingSessionInfo sessionInfo
;
774 * Create the SwingSessionInfo. Note package private access.
776 * @return the sessionInfo
778 SwingSessionInfo
getSessionInfo() {
783 * Push the logical screen to the physical device.
786 public void flushPhysical() {
789 System.err.printf("flushPhysical(): reallyCleared %s dirty %s\n",
790 reallyCleared, dirty);
793 // If reallyCleared is set, we have to draw everything.
794 if ((frame
.bufferStrategy
!= null) && (reallyCleared
== true)) {
795 // Triple-buffering: we have to redraw everything on this thread.
796 Graphics gr
= frame
.bufferStrategy
.getDrawGraphics();
799 frame
.bufferStrategy
.show();
800 // sync() doesn't seem to help the tearing for me.
801 // Toolkit.getDefaultToolkit().sync();
803 } else if ((frame
.bufferStrategy
== null) && (reallyCleared
== true)) {
804 // Repaint everything on the Swing thread.
809 // Do nothing if nothing happened.
814 if (frame
.bufferStrategy
!= null) {
815 // See if it is time to flip the blink time.
816 long nowTime
= (new Date()).getTime();
817 if (nowTime
> frame
.blinkMillis
+ frame
.lastBlinkTime
) {
818 frame
.lastBlinkTime
= nowTime
;
819 frame
.cursorBlinkVisible
= !frame
.cursorBlinkVisible
;
822 Graphics gr
= frame
.bufferStrategy
.getDrawGraphics();
824 synchronized (this) {
825 for (int y
= 0; y
< height
; y
++) {
826 for (int x
= 0; x
< width
; x
++) {
827 Cell lCell
= logical
[x
][y
];
828 Cell pCell
= physical
[x
][y
];
830 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
831 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
833 if (!lCell
.equals(pCell
)
839 frame
.drawGlyph(gr
, lCell
, xPixel
, yPixel
);
840 physical
[x
][y
].setTo(lCell
);
844 frame
.drawCursor(gr
);
845 } // synchronized (this)
848 frame
.bufferStrategy
.show();
849 // sync() doesn't seem to help the tearing for me.
850 // Toolkit.getDefaultToolkit().sync();
854 // Swing thread version: request a repaint, but limit it to the area
857 // Find the minimum-size damaged region.
858 int xMin
= frame
.getWidth();
860 int yMin
= frame
.getHeight();
863 synchronized (this) {
864 for (int y
= 0; y
< height
; y
++) {
865 for (int x
= 0; x
< width
; x
++) {
866 Cell lCell
= logical
[x
][y
];
867 Cell pCell
= physical
[x
][y
];
869 int xPixel
= x
* frame
.textWidth
+ frame
.left
;
870 int yPixel
= y
* frame
.textHeight
+ frame
.top
;
872 if (!lCell
.equals(pCell
)
881 if (xPixel
+ frame
.textWidth
> xMax
) {
882 xMax
= xPixel
+ frame
.textWidth
;
887 if (yPixel
+ frame
.textHeight
> yMax
) {
888 yMax
= yPixel
+ frame
.textHeight
;
894 if (xMin
+ frame
.textWidth
>= xMax
) {
895 xMax
+= frame
.textWidth
;
897 if (yMin
+ frame
.textHeight
>= yMax
) {
898 yMax
+= frame
.textHeight
;
901 // Repaint the desired area
903 System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
906 if (frame
.bufferStrategy
!= null) {
907 // This path should never be taken, but is left here for
909 Graphics gr
= frame
.bufferStrategy
.getDrawGraphics();
910 Rectangle bounds
= new Rectangle(xMin
, yMin
, xMax
- xMin
,
915 frame
.bufferStrategy
.show();
916 // sync() doesn't seem to help the tearing for me.
917 // Toolkit.getDefaultToolkit().sync();
919 // Repaint on the Swing thread.
920 frame
.repaint(xMin
, yMin
, xMax
- xMin
, yMax
- yMin
);
925 * Put the cursor at (x,y).
927 * @param visible if true, the cursor should be visible
928 * @param x column coordinate to put the cursor on
929 * @param y row coordinate to put the cursor on
932 public void putCursor(final boolean visible
, final int x
, final int y
) {
934 if ((visible
== cursorVisible
) && ((x
== cursorX
) && (y
== cursorY
))) {
935 // See if it is time to flip the blink time.
936 long nowTime
= (new Date()).getTime();
937 if (nowTime
< frame
.blinkMillis
+ frame
.lastBlinkTime
) {
938 // Nothing has changed, so don't do anything.
944 && (cursorY
<= height
- 1)
945 && (cursorX
<= width
- 1)
947 // Make the current cursor position dirty
948 if (physical
[cursorX
][cursorY
].getChar() == 'Q') {
949 physical
[cursorX
][cursorY
].setChar('X');
951 physical
[cursorX
][cursorY
].setChar('Q');
955 super.putCursor(visible
, x
, y
);
959 * Convert pixel column position to text cell column position.
961 * @param x pixel column position
962 * @return text cell column position
964 public int textColumn(final int x
) {
965 return ((x
- frame
.left
) / frame
.textWidth
);
969 * Convert pixel row position to text cell row position.
971 * @param y pixel row position
972 * @return text cell row position
974 public int textRow(final int y
) {
975 return ((y
- frame
.top
) / frame
.textHeight
);
979 * Set the window title.
981 * @param title the new title
983 public void setTitle(final String title
) {
984 frame
.setTitle(title
);