-/**
+/*
* Jexer - Java Text User Interface
*
* License: LGPLv3 or later
*/
package jexer.io;
-import jexer.bits.Cell;
-import jexer.bits.CellAttributes;
-import jexer.session.SwingSessionInfo;
-
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
+import java.awt.image.BufferStrategy;
import java.io.InputStream;
+import java.util.Date;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
+import jexer.bits.Cell;
+import jexer.bits.CellAttributes;
+import jexer.session.SwingSessionInfo;
+
/**
* This Screen implementation draws to a Java Swing JFrame.
*/
public final class SwingScreen extends Screen {
+ /**
+ * If true, use double buffering thread.
+ */
+ private static final boolean doubleBuffer = true;
+
+ /**
+ * Cursor style to draw.
+ */
+ public enum CursorStyle {
+ /**
+ * Use an underscore for the cursor.
+ */
+ UNDERLINE,
+
+ /**
+ * Use a solid block for the cursor.
+ */
+ BLOCK,
+
+ /**
+ * Use an outlined block for the cursor.
+ */
+ OUTLINE
+ }
+
private static Color MYBLACK;
private static Color MYRED;
private static Color MYGREEN;
*/
private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
+ /**
+ * The BufferStrategy object needed for double-buffering.
+ */
+ private BufferStrategy bufferStrategy;
+
/**
* The TUI Screen data.
*/
private int maxDescent = 0;
/**
- * Top pixel value.
+ * System-dependent Y adjustment for text in the character cell.
+ */
+ private int textAdjustY = 0;
+
+ /**
+ * System-dependent X adjustment for text in the character cell.
+ */
+ private int textAdjustX = 0;
+
+ /**
+ * Top pixel absolute location.
*/
private int top = 30;
/**
- * Left pixel value.
+ * Left pixel absolute location.
*/
private int left = 30;
+ /**
+ * The cursor style to draw.
+ */
+ private CursorStyle cursorStyle = CursorStyle.UNDERLINE;
+
+ /**
+ * The number of millis to wait before switching the blink from
+ * visible to invisible.
+ */
+ private long blinkMillis = 500;
+
+ /**
+ * If true, the cursor should be visible right now based on the blink
+ * time.
+ */
+ private boolean cursorBlinkVisible = true;
+
+ /**
+ * The time that the blink last flipped from visible to invisible or
+ * from invisible to visible.
+ */
+ private long lastBlinkTime = 0;
+
/**
* Convert a CellAttributes foreground color to an Swing Color.
*
* @return the Swing Color
*/
private Color attrToForegroundColor(final CellAttributes attr) {
- /*
- * TODO:
- * reverse
- * blink
- * underline
- */
if (attr.isBold()) {
if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
return MYBOLD_BLACK;
* @return the Swing Color
*/
private Color attrToBackgroundColor(final CellAttributes attr) {
- /*
- * TODO:
- * reverse
- * blink
- * underline
- */
if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
return MYBLACK;
} else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
this.screen = screen;
setDOSColors();
+ // Figure out my cursor style
+ String cursorStyleString = System.getProperty("jexer.Swing.cursorStyle",
+ "underline").toLowerCase();
+
+ if (cursorStyleString.equals("underline")) {
+ cursorStyle = CursorStyle.UNDERLINE;
+ } else if (cursorStyleString.equals("outline")) {
+ cursorStyle = CursorStyle.OUTLINE;
+ } else if (cursorStyleString.equals("block")) {
+ cursorStyle = CursorStyle.BLOCK;
+ }
+
setTitle("Jexer Application");
setBackground(Color.black);
- // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
- // setFont(new Font("Liberation Mono", Font.BOLD, 16));
- // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
try {
// Always try to use Terminus, the one decent font.
// Save the text cell width/height
getFontDimensions();
+
+ // Setup double-buffering
+ if (SwingScreen.doubleBuffer) {
+ setIgnoreRepaint(true);
+ createBufferStrategy(2);
+ bufferStrategy = getBufferStrategy();
+ }
}
/**
// This also produces the same number, but works better for ugly
// monospace.
textHeight = fm.getMaxAscent() + maxDescent - leading;
+
+ if (System.getProperty("os.name").startsWith("Windows")) {
+ textAdjustY = -1;
+ textAdjustX = 0;
+ }
}
/**
return;
}
+ // See if it is time to flip the blink time.
+ long nowTime = (new Date()).getTime();
+ if (nowTime > blinkMillis + lastBlinkTime) {
+ lastBlinkTime = nowTime;
+ cursorBlinkVisible = !cursorBlinkVisible;
+ }
+
int xCellMin = 0;
int xCellMax = screen.width;
int yCellMin = 0;
Cell lCell = screen.logical[x][y];
Cell pCell = screen.physical[x][y];
- if (!lCell.equals(pCell) || reallyCleared) {
+ if (!lCell.equals(pCell)
+ || lCell.isBlink()
+ || reallyCleared) {
+
+ Cell lCellColor = new Cell();
+ lCellColor.setTo(lCell);
+
+ // Check for reverse
+ if (lCell.isReverse()) {
+ lCellColor.setForeColor(lCell.getBackColor());
+ lCellColor.setBackColor(lCell.getForeColor());
+ }
+
// Draw the background rectangle, then the
// foreground character.
- gr.setColor(attrToBackgroundColor(lCell));
+ gr.setColor(attrToBackgroundColor(lCellColor));
gr.fillRect(xPixel, yPixel, textWidth, textHeight);
- gr.setColor(attrToForegroundColor(lCell));
- char [] chars = new char[1];
- chars[0] = lCell.getChar();
- gr.drawChars(chars, 0, 1, xPixel,
- yPixel + textHeight - maxDescent);
+
+ // Handle blink and underline
+ if (!lCell.isBlink()
+ || (lCell.isBlink() && cursorBlinkVisible)
+ ) {
+ gr.setColor(attrToForegroundColor(lCellColor));
+ char [] chars = new char[1];
+ chars[0] = lCell.getChar();
+ gr.drawChars(chars, 0, 1, xPixel + textAdjustX,
+ yPixel + textHeight - maxDescent
+ + textAdjustY);
+
+ if (lCell.isUnderline()) {
+ gr.fillRect(xPixel, yPixel + textHeight - 2,
+ textWidth, 2);
+ }
+ }
// Physical is always updated
physical[x][y].setTo(lCell);
}
// Draw the cursor if it is visible
- if ((cursorVisible)
+ if (cursorVisible
&& (cursorY <= screen.height - 1)
&& (cursorX <= screen.width - 1)
+ && cursorBlinkVisible
) {
int xPixel = cursorX * textWidth + left;
int yPixel = cursorY * textHeight + top;
Cell lCell = screen.logical[cursorX][cursorY];
gr.setColor(attrToForegroundColor(lCell));
- gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2);
+ switch (cursorStyle) {
+ case UNDERLINE:
+ gr.fillRect(xPixel, yPixel + textHeight - 2,
+ textWidth, 2);
+ break;
+ case BLOCK:
+ gr.fillRect(xPixel, yPixel, textWidth, textHeight);
+ break;
+ case OUTLINE:
+ gr.drawRect(xPixel, yPixel, textWidth - 1,
+ textHeight - 1);
+ break;
+ }
}
dirty = false;
*/
SwingFrame frame;
+ /**
+ * Restore terminal to normal state.
+ */
+ public void shutdown() {
+ frame.dispose();
+ }
+
/**
* Public constructor.
*/
if (reallyCleared) {
// Really refreshed, do it all
- frame.repaint();
+ if (SwingScreen.doubleBuffer) {
+ Graphics gr = frame.bufferStrategy.getDrawGraphics();
+ frame.paint(gr);
+ gr.dispose();
+ frame.bufferStrategy.show();
+ Toolkit.getDefaultToolkit().sync();
+ } else {
+ frame.repaint();
+ }
return;
}
|| ((x == cursorX)
&& (y == cursorY)
&& cursorVisible)
+ || lCell.isBlink()
) {
if (xPixel < xMin) {
xMin = xPixel;
}
// Repaint the desired area
- frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
- // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax, yMin, yMax);
+ // System.err.printf("REPAINT X %d %d Y %d %d\n", xMin, xMax,
+ // yMin, yMax);
+ if (SwingScreen.doubleBuffer) {
+ Graphics gr = frame.bufferStrategy.getDrawGraphics();
+ Rectangle bounds = new Rectangle(xMin, yMin, xMax - xMin,
+ yMax - yMin);
+ gr.setClip(bounds);
+ frame.paint(gr);
+ gr.dispose();
+ frame.bufferStrategy.show();
+ Toolkit.getDefaultToolkit().sync();
+ } else {
+ frame.repaint(xMin, yMin, xMax - xMin, yMax - yMin);
+ }
}
/**
*/
@Override
public void putCursor(final boolean visible, final int x, final int y) {
- if ((cursorVisible)
+
+ if ((visible == cursorVisible) && ((x == cursorX) && (y == cursorY))) {
+ // See if it is time to flip the blink time.
+ long nowTime = (new Date()).getTime();
+ if (nowTime < frame.blinkMillis + frame.lastBlinkTime) {
+ // Nothing has changed, so don't do anything.
+ return;
+ }
+ }
+
+ if (cursorVisible
&& (cursorY <= height - 1)
&& (cursorX <= width - 1)
) {