com.googlecode.lanterna.input.KeyType Java Examples
The following examples show how to use
com.googlecode.lanterna.input.KeyType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Issue312.java From lanterna with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) throws IOException { Terminal terminal = new DefaultTerminalFactory().createTerminal(); TextGraphics textGraphics = terminal.newTextGraphics(); int row = 0; while(true) { KeyStroke keyStroke = terminal.pollInput(); if (keyStroke == null) { terminal.setCursorPosition(0, 0); textGraphics.putString(0, terminal.getCursorPosition().getRow(), " > "); terminal.flush(); keyStroke = terminal.readInput(); row = 1; terminal.clearScreen(); } if (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF) { break; } textGraphics.putString(0, row++, "Read KeyStroke: " + keyStroke + "\n"); terminal.flush(); } terminal.close(); }
Example #2
Source File: AbstractTextGUI.java From lanterna with GNU Lesser General Public License v3.0 | 6 votes |
@Override public synchronized boolean processInput() throws IOException { boolean gotInput = false; KeyStroke keyStroke = readKeyStroke(); if(keyStroke != null) { gotInput = true; do { if (keyStroke.getKeyType() == KeyType.EOF) { throw new EOFException(); } boolean handled = handleInput(keyStroke); if(!handled) { handled = fireUnhandledKeyStroke(keyStroke); } dirty = handled || dirty; keyStroke = pollInput(); } while(keyStroke != null); } return gotInput; }
Example #3
Source File: TerminalScreen.java From lanterna with GNU Lesser General Public License v3.0 | 6 votes |
public synchronized void stopScreen(boolean flushInput) throws IOException { if(!isStarted) { return; } if (flushInput) { //Drain the input queue KeyStroke keyStroke; do { keyStroke = pollInput(); } while(keyStroke != null && keyStroke.getKeyType() != KeyType.EOF); } getTerminal().exitPrivateMode(); isStarted = false; }
Example #4
Source File: AWTTerminalFrame.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public KeyStroke pollInput() { if(disposed) { return new KeyStroke(KeyType.EOF); } KeyStroke keyStroke = awtTerminal.pollInput(); if(autoCloseTriggers.contains(TerminalEmulatorAutoCloseTrigger.CloseOnEscape) && keyStroke != null && keyStroke.getKeyType() == KeyType.Escape) { dispose(); } return keyStroke; }
Example #5
Source File: PrivateModeTest.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
public static void main(String[] args) throws IOException, InterruptedException { Terminal terminal = new TestTerminalFactory(args) .setTerminalEmulatorFrameAutoCloseTrigger(null) .createTerminal(); boolean normalTerminal = true; printNormalTerminalText(terminal); KeyStroke keyStroke = null; while(keyStroke == null || keyStroke.getKeyType() != KeyType.Escape) { keyStroke = terminal.pollInput(); if(keyStroke != null && keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ') { normalTerminal = !normalTerminal; if(normalTerminal) { terminal.exitPrivateMode(); printNormalTerminalText(terminal); } else { terminal.enterPrivateMode(); printPrivateModeTerminalText(terminal); } } else { Thread.sleep(1); } } if(!normalTerminal) { terminal.exitPrivateMode(); } terminal.putCharacter('\n'); if(terminal instanceof Window) { ((Window) terminal).dispose(); } }
Example #6
Source File: TerminalInputTest.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
public static void main(String[] args) throws InterruptedException, IOException { // For IDE users: either set runtime arguments or uncomment this line: //args = new String[] { "--mouse-move", "--telnet-port=1024", "--with-timeout=12" }; final Terminal rawTerminal = new TestTerminalFactory(args).createTerminal(); rawTerminal.enterPrivateMode(); int currentRow = 0; rawTerminal.setCursorPosition(0, 0); while(true) { KeyStroke key = rawTerminal.pollInput(); if(key == null) { Thread.sleep(1); continue; } if(key.getKeyType() == KeyType.Escape || key.getKeyType() == KeyType.EOF) { break; } if(currentRow == 0) { rawTerminal.clearScreen(); } rawTerminal.setCursorPosition(0, currentRow++); putString(rawTerminal, key.toString()); if(currentRow >= rawTerminal.getTerminalSize().getRows()) { currentRow = 0; } } rawTerminal.exitPrivateMode(); }
Example #7
Source File: VirtualScreenTest.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
public VirtualScreenTest(String[] args) throws InterruptedException, IOException { Screen screen = new TestTerminalFactory(args).createScreen(); screen = new VirtualScreen(screen); screen.startScreen(); TextGraphics textGraphics = screen.newTextGraphics(); textGraphics.setBackgroundColor(TextColor.ANSI.GREEN); textGraphics.fillTriangle(new TerminalPosition(40, 0), new TerminalPosition(25,19), new TerminalPosition(65, 19), ' '); textGraphics.setBackgroundColor(TextColor.ANSI.RED); textGraphics.drawRectangle(TerminalPosition.TOP_LEFT_CORNER, screen.getTerminalSize(), ' '); screen.refresh(); while(true) { KeyStroke keyStroke = screen.pollInput(); if(keyStroke != null) { if(keyStroke.getKeyType() == KeyType.Escape) { break; } } else if(screen.doResizeIfNecessary() != null) { screen.refresh(); } else { Thread.sleep(1); } } screen.stopScreen(); }
Example #8
Source File: FullScreenTextGUITest.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
public static void main(String[] args) throws IOException, InterruptedException { Screen screen = new TestTerminalFactory(args).setInitialTerminalSize(new TerminalSize(80, 25)).createScreen(); screen.startScreen(); final AtomicBoolean stop = new AtomicBoolean(false); MultiWindowTextGUI textGUI = new MultiWindowTextGUI(screen); textGUI.addListener((textGUI1, key) -> { if(key.getKeyType() == KeyType.Escape) { stop.set(true); return true; } return false; }); try { textGUI.getBackgroundPane().setComponent(new BIOS()); while(!stop.get()) { if(!textGUI.getGUIThread().processEventsAndUpdate()) { Thread.sleep(1); } } } catch (EOFException ignore) { // Terminal closed } finally { screen.stopScreen(); } }
Example #9
Source File: MultiWindowManagerTest.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void init(final WindowBasedTextGUI textGUI) { textGUI.getBackgroundPane().setComponent(new BackgroundComponent()); final Window mainWindow = new BasicWindow("Multi Window Test"); Panel contentArea = new Panel(); contentArea.setLayoutManager(new LinearLayout(Direction.VERTICAL)); contentArea.addComponent(new Button("Add new window", () -> onNewWindow(textGUI))); buttonToggleVirtualScreen = new Button("Virtual Screen: Enabled", () -> { virtualScreenEnabled = !virtualScreenEnabled; textGUI.setVirtualScreenEnabled(virtualScreenEnabled); buttonToggleVirtualScreen.setLabel("Virtual Screen: " + (virtualScreenEnabled ? "Enabled" : "Disabled")); }); contentArea.addComponent(buttonToggleVirtualScreen); contentArea.addComponent(new EmptySpace(TerminalSize.ONE)); contentArea.addComponent(new Button("Close", mainWindow::close)); mainWindow.setComponent(contentArea); textGUI.addListener((textGUI1, keyStroke) -> { if((keyStroke.isCtrlDown() && keyStroke.getKeyType() == KeyType.Tab) || keyStroke.getKeyType() == KeyType.F6) { ((WindowBasedTextGUI) textGUI1).cycleActiveWindow(false); } else if((keyStroke.isCtrlDown() && keyStroke.getKeyType() == KeyType.ReverseTab) || keyStroke.getKeyType() == KeyType.F7) { ((WindowBasedTextGUI) textGUI1).cycleActiveWindow(true); } else { return false; } return true; }); textGUI.addWindow(mainWindow); }
Example #10
Source File: GraphicalTerminalImplementation.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
private void injectStringAsKeyStrokes(String string) { StringReader stringReader = new StringReader(string); InputDecoder inputDecoder = new InputDecoder(stringReader); inputDecoder.addProfile(new DefaultKeyDecodingProfile()); try { KeyStroke keyStroke = inputDecoder.getNextCharacter(false); while (keyStroke != null && keyStroke.getKeyType() != KeyType.EOF) { keyQueue.add(keyStroke); keyStroke = inputDecoder.getNextCharacter(false); } } catch(IOException ignore) { } }
Example #11
Source File: GraphicalTerminalImplementation.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public KeyStroke readInput() { // Synchronize on keyQueue here so only one thread is inside keyQueue.take() synchronized(keyQueue) { if(!enableInput) { return new KeyStroke(KeyType.EOF); } try { return keyQueue.take(); } catch(InterruptedException ignore) { throw new RuntimeException("Blocking input was interrupted"); } } }
Example #12
Source File: GraphicalTerminalImplementation.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public KeyStroke pollInput() { if(!enableInput) { return new KeyStroke(KeyType.EOF); } return keyQueue.poll(); }
Example #13
Source File: GraphicalTerminalImplementation.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
synchronized void onDestroyed() { stopBlinkTimer(); enableInput = false; // If a thread is blocked, waiting on something in the keyQueue... keyQueue.add(new KeyStroke(KeyType.EOF)); }
Example #14
Source File: SwingTerminalFrame.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public KeyStroke pollInput() { if(disposed) { return new KeyStroke(KeyType.EOF); } KeyStroke keyStroke = swingTerminal.pollInput(); if(autoCloseTriggers.contains(TerminalEmulatorAutoCloseTrigger.CloseOnEscape) && keyStroke != null && keyStroke.getKeyType() == KeyType.Escape) { dispose(); } return keyStroke; }
Example #15
Source File: WindowsTerminal.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected KeyDecodingProfile getDefaultKeyDecodingProfile() { ArrayList<CharacterPattern> keyDecodingProfile = new ArrayList<CharacterPattern>(); // handle Key Code 13 as ENTER keyDecodingProfile.add(new BasicCharacterPattern(new KeyStroke(KeyType.Enter), '\r')); // handle everything else as per default keyDecodingProfile.addAll(super.getDefaultKeyDecodingProfile().getPatterns()); return () -> keyDecodingProfile; }
Example #16
Source File: VirtualScreen.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
private KeyStroke filter(KeyStroke keyStroke) throws IOException { if(keyStroke == null) { return null; } else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowLeft) { if(viewportTopLeft.getColumn() > 0) { viewportTopLeft = viewportTopLeft.withRelativeColumn(-1); refresh(); return null; } } else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowRight) { if(viewportTopLeft.getColumn() + viewportSize.getColumns() < getTerminalSize().getColumns()) { viewportTopLeft = viewportTopLeft.withRelativeColumn(1); refresh(); return null; } } else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowUp) { if(viewportTopLeft.getRow() > 0) { viewportTopLeft = viewportTopLeft.withRelativeRow(-1); realScreen.scrollLines(0,viewportSize.getRows()-1,-1); refresh(); return null; } } else if(keyStroke.isAltDown() && keyStroke.getKeyType() == KeyType.ArrowDown) { if(viewportTopLeft.getRow() + viewportSize.getRows() < getTerminalSize().getRows()) { viewportTopLeft = viewportTopLeft.withRelativeRow(1); realScreen.scrollLines(0,viewportSize.getRows()-1,1); refresh(); return null; } } return keyStroke; }
Example #17
Source File: ComboBox.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public synchronized boolean handleInput(KeyStroke keyStroke) { if (keyStroke.getKeyType() == KeyType.Escape) { close(); popupWindow = null; return true; } return super.handleInput(keyStroke); }
Example #18
Source File: MultiWindowTextGUI.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected synchronized KeyStroke readKeyStroke() throws IOException { KeyStroke keyStroke = super.pollInput(); if(hadWindowAtSomePoint && eofWhenNoWindows && keyStroke == null && windows.isEmpty()) { return new KeyStroke(KeyType.EOF); } else if(keyStroke != null) { return keyStroke; } else { return super.readKeyStroke(); } }
Example #19
Source File: RadioBoxList.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public synchronized Result handleKeyStroke(KeyStroke keyStroke) { if (isKeyboardActivationStroke(keyStroke)) { setCheckedIndex(getSelectedIndex()); } else if (keyStroke.getKeyType() == KeyType.MouseEvent) { MouseAction mouseAction = (MouseAction) keyStroke; MouseActionType actionType = mouseAction.getActionType(); if (isMouseMove(keyStroke) || actionType == MouseActionType.CLICK_RELEASE || actionType == MouseActionType.SCROLL_UP || actionType == MouseActionType.SCROLL_DOWN) { return super.handleKeyStroke(keyStroke); } // includes mouse drag int existingIndex = getSelectedIndex(); int newIndex = getIndexByMouseAction(mouseAction); if (existingIndex != newIndex || !isFocused()) { Result result = super.handleKeyStroke(keyStroke); setCheckedIndex(getSelectedIndex()); return result; } setCheckedIndex(getSelectedIndex()); return Result.HANDLED; } return super.handleKeyStroke(keyStroke); }
Example #20
Source File: AbstractWindow.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public boolean handleInput(KeyStroke key) { boolean handled = super.handleInput(key); if(!handled && closeWindowWithEscape && key.getKeyType() == KeyType.Escape) { close(); return true; } return handled; }
Example #21
Source File: CheckBoxList.java From lanterna with GNU Lesser General Public License v3.0 | 5 votes |
@Override public synchronized Result handleKeyStroke(KeyStroke keyStroke) { if (isKeyboardActivationStroke(keyStroke)) { toggleChecked(getSelectedIndex()); return Result.HANDLED; } else if (keyStroke.getKeyType() == KeyType.MouseEvent) { MouseAction mouseAction = (MouseAction) keyStroke; MouseActionType actionType = mouseAction.getActionType(); if (isMouseMove(keyStroke) || actionType == MouseActionType.CLICK_RELEASE || actionType == MouseActionType.SCROLL_UP || actionType == MouseActionType.SCROLL_DOWN) { return super.handleKeyStroke(keyStroke); } Result result = super.handleKeyStroke(keyStroke); int newIndex = getIndexByMouseAction(mouseAction); if (actionType == MouseActionType.CLICK_DOWN) { stateForMouseDragged = !isChecked(newIndex); setChecked(newIndex, stateForMouseDragged); minIndexForMouseDragged = newIndex; maxIndexForMouseDragged = newIndex; } minIndexForMouseDragged = Math.min(minIndexForMouseDragged, newIndex); maxIndexForMouseDragged = Math.max(maxIndexForMouseDragged, newIndex); if (actionType == MouseActionType.DRAG) { for (int i = minIndexForMouseDragged; i <= maxIndexForMouseDragged; i++) { setChecked(i, stateForMouseDragged); } } return result; } return super.handleKeyStroke(keyStroke); }
Example #22
Source File: SessionStatePrinter.java From bt with Apache License 2.0 | 4 votes |
public static SessionStatePrinter createKeyInputAwarePrinter(Collection<KeyStrokeBinding> bindings) { return new SessionStatePrinter() { private Thread t; { t = new Thread(() -> { while (!isShutdown()) { try { // don't intercept input when paused if (super.supressOutput) { Thread.sleep(1000); continue; } KeyStroke keyStroke = pollKeyInput(); if (keyStroke == null) { Thread.sleep(100); } else if (keyStroke.isCtrlDown() && keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter().equals('c')) { shutdown(); System.exit(0); } else { bindings.forEach(binding -> { if (keyStroke.equals(binding.getKeyStroke())) { binding.getBinding().run(); } }); } } catch (Throwable e) { LOGGER.error("Unexpected error when reading user input", e); } } }); t.setDaemon(true); t.start(); Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown)); } @Override public void shutdown() { if (!isShutdown()) { super.shutdown(); t.interrupt(); } } }; }
Example #23
Source File: AbstractInteractableComponent.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
public boolean isMouseMove(KeyStroke keyStroke) { return keyStroke.getKeyType() == KeyType.MouseEvent && ((MouseAction)keyStroke).getActionType() == MouseActionType.MOVE; }
Example #24
Source File: AbstractInteractableComponent.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
public boolean isKeyboardActivationStroke(KeyStroke keyStroke) { boolean isKeyboardActivation = (keyStroke.getKeyType() == KeyType.Character && keyStroke.getCharacter() == ' ') || keyStroke.getKeyType() == KeyType.Enter; return isFocused() && isKeyboardActivation; }
Example #25
Source File: Table.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
@Override public Result handleKeyStroke(KeyStroke keyStroke) { switch(keyStroke.getKeyType()) { case ArrowUp: if(selectedRow > 0) { selectedRow--; } else if(escapeByArrowKey) { return Result.MOVE_FOCUS_UP; } break; case ArrowDown: if(selectedRow < tableModel.getRowCount() - 1) { selectedRow++; } else if(escapeByArrowKey) { return Result.MOVE_FOCUS_DOWN; } break; case PageUp: if(getRenderer().getVisibleRowsOnLastDraw() > 0 && selectedRow > 0) { selectedRow -= Math.min(getRenderer().getVisibleRowsOnLastDraw() - 1, selectedRow); } break; case PageDown: if(getRenderer().getVisibleRowsOnLastDraw() > 0 && selectedRow < tableModel.getRowCount() - 1) { int toEndDistance = tableModel.getRowCount() - 1 - selectedRow; selectedRow += Math.min(getRenderer().getVisibleRowsOnLastDraw() - 1, toEndDistance); } break; case Home: selectedRow = 0; break; case End: selectedRow = tableModel.getRowCount() - 1; break; case ArrowLeft: if(cellSelection && selectedColumn > 0) { selectedColumn--; } else if(escapeByArrowKey) { return Result.MOVE_FOCUS_LEFT; } break; case ArrowRight: if(cellSelection && selectedColumn < tableModel.getColumnCount() - 1) { selectedColumn++; } else if(escapeByArrowKey) { return Result.MOVE_FOCUS_RIGHT; } break; case Character: case Enter: if (isKeyboardActivationStroke(keyStroke)) { Runnable runnable = selectAction; //To avoid synchronizing if(runnable != null) { runnable.run(); } else { return Result.HANDLED; } break; } else { return super.handleKeyStroke(keyStroke); } case MouseEvent: MouseAction action = (MouseAction)keyStroke; MouseActionType actionType = action.getActionType(); if (actionType == MouseActionType.MOVE) { // do nothing return Result.UNHANDLED; } if (!isFocused()) { super.handleKeyStroke(keyStroke); } int mouseRow = getRowByMouseAction((MouseAction) keyStroke); int mouseColumn = getColumnByMouseAction((MouseAction) keyStroke); boolean isDifferentCell = mouseRow != selectedRow || mouseColumn != selectedColumn; selectedRow = mouseRow; selectedColumn = mouseColumn; if (isDifferentCell) { return handleKeyStroke(new KeyStroke(KeyType.Enter)); } break; default: return super.handleKeyStroke(keyStroke); } invalidate(); return Result.HANDLED; }
Example #26
Source File: ScreenTriangleTest.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
public static void main(String[] args) throws IOException, InterruptedException { boolean useAnsiColors = false; boolean useFilled = false; boolean slow = false; boolean rotating = false; boolean square = false; for(String arg : args) { if(arg.equals("--ansi-colors")) { useAnsiColors = true; } if(arg.equals("--filled")) { useFilled = true; } if(arg.equals("--slow")) { slow = true; } if(arg.equals("--rotating")) { rotating = true; } if(arg.equals("--square")) { square = true; } } Screen screen = new TestTerminalFactory(args).createScreen(); screen.startScreen(); TextGraphics graphics = new ScreenTextGraphics(screen); if(square) { graphics = new DoublePrintingTextGraphics(graphics); } Random random = new Random(); TextColor color = null; double rad = 0.0; while(true) { KeyStroke keyStroke = screen.pollInput(); if(keyStroke != null && (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) { break; } screen.doResizeIfNecessary(); TerminalSize size = graphics.getSize(); if(useAnsiColors) { if(color == null || !rotating) { color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)]; } } else { if(color == null || !rotating) { //Draw a rectangle in random indexed color color = new TextColor.Indexed(random.nextInt(256)); } } TerminalPosition p1; TerminalPosition p2; TerminalPosition p3; if(rotating) { screen.clear(); double triangleSize = 15.0; int x0 = (size.getColumns() / 2) + (int) (Math.cos(rad) * triangleSize); int y0 = (size.getRows() / 2) + (int) (Math.sin(rad) * triangleSize); int x1 = (size.getColumns() / 2) + (int) (Math.cos(rad + oneThirdOf2PI) * triangleSize); int y1 = (size.getRows() / 2) + (int) (Math.sin(rad + oneThirdOf2PI) * triangleSize); int x2 = (size.getColumns() / 2) + (int) (Math.cos(rad + twoThirdsOf2PI) * triangleSize); int y2 = (size.getRows() / 2) + (int) (Math.sin(rad + twoThirdsOf2PI) * triangleSize); p1 = new TerminalPosition(x0, y0); p2 = new TerminalPosition(x1, y1); p3 = new TerminalPosition(x2, y2); rad += Math.PI / 90.0; } else { p1 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); p2 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); p3 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); } graphics.setBackgroundColor(color); if(useFilled) { graphics.fillTriangle(p1, p2, p3, ' '); } else { graphics.drawTriangle(p1, p2, p3, ' '); } screen.refresh(Screen.RefreshType.DELTA); if(slow) { Thread.sleep(500); } } screen.stopScreen(); }
Example #27
Source File: ScreenRectangleTest.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
public static void main(String[] args) throws IOException, InterruptedException { boolean useAnsiColors = false; boolean useFilled = false; boolean slow = false; for(String arg: args) { if(arg.equals("--ansi-colors")) { useAnsiColors = true; } if(arg.equals("--filled")) { useFilled = true; } if(arg.equals("--slow")) { slow = true; } } Screen screen = new TestTerminalFactory(args).createScreen(); screen.startScreen(); TextGraphics textGraphics = new ScreenTextGraphics(screen); Random random = new Random(); long startTime = System.currentTimeMillis(); while(System.currentTimeMillis() - startTime < 1000 * 20) { KeyStroke keyStroke = screen.pollInput(); if(keyStroke != null && (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) { break; } screen.doResizeIfNecessary(); TerminalSize size = textGraphics.getSize(); TextColor color; if(useAnsiColors) { color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)]; } else { //Draw a rectangle in random indexed color color = new TextColor.Indexed(random.nextInt(256)); } TerminalPosition topLeft = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); TerminalSize rectangleSize = new TerminalSize(random.nextInt(size.getColumns() - topLeft.getColumn()), random.nextInt(size.getRows() - topLeft.getRow())); textGraphics.setBackgroundColor(color); if(useFilled) { textGraphics.fillRectangle(topLeft, rectangleSize, ' '); } else { textGraphics.drawRectangle(topLeft, rectangleSize, ' '); } screen.refresh(Screen.RefreshType.DELTA); if(slow) { Thread.sleep(500); } } screen.stopScreen(); }
Example #28
Source File: ScreenLineTest.java From lanterna with GNU Lesser General Public License v3.0 | 4 votes |
public static void main(String[] args) throws IOException, InterruptedException { boolean useAnsiColors = false; boolean slow = false; boolean circle = false; for(String arg: args) { if(arg.equals("--ansi-colors")) { useAnsiColors = true; } if(arg.equals("--slow")) { slow = true; } if(arg.equals("--circle")) { circle = true; } } Screen screen = new TestTerminalFactory(args).createScreen(); screen.startScreen(); TextGraphics textGraphics = new ScreenTextGraphics(screen); Random random = new Random(); while(true) { KeyStroke keyStroke = screen.pollInput(); if(keyStroke != null && (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) { break; } screen.doResizeIfNecessary(); TerminalSize size = textGraphics.getSize(); TextColor color; if(useAnsiColors) { color = TextColor.ANSI.values()[random.nextInt(TextColor.ANSI.values().length)]; } else { //Draw a rectangle in random indexed color color = new TextColor.Indexed(random.nextInt(256)); } TerminalPosition p1; TerminalPosition p2; if(circle) { p1 = new TerminalPosition(size.getColumns() / 2, size.getRows() / 2); if(CIRCLE_LAST_POSITION == null) { CIRCLE_LAST_POSITION = new TerminalPosition(0, 0); } else if(CIRCLE_LAST_POSITION.getRow() == 0) { if(CIRCLE_LAST_POSITION.getColumn() < size.getColumns() - 1) { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeColumn(1); } else { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(1); } } else if(CIRCLE_LAST_POSITION.getRow() < size.getRows() - 1) { if(CIRCLE_LAST_POSITION.getColumn() == 0) { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(-1); } else { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(1); } } else { if(CIRCLE_LAST_POSITION.getColumn() > 0) { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeColumn(-1); } else { CIRCLE_LAST_POSITION = CIRCLE_LAST_POSITION.withRelativeRow(-1); } } p2 = CIRCLE_LAST_POSITION; } else { p1 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); p2 = new TerminalPosition(random.nextInt(size.getColumns()), random.nextInt(size.getRows())); } textGraphics.setBackgroundColor(color); textGraphics.drawLine(p1, p2, ' '); textGraphics.setBackgroundColor(TextColor.ANSI.BLACK); textGraphics.setForegroundColor(TextColor.ANSI.WHITE); textGraphics.putString(4, size.getRows() - 1, "P1 " + p1 + " -> P2 " + p2); screen.refresh(Screen.RefreshType.DELTA); if(slow) { Thread.sleep(500); } } screen.stopScreen(); }