📄 vdubuffer.java
字号:
/* * This file is part of "The Java Telnet Application". * * (c) Matthias L. Jugel, Marcus Mei遪er 1996-2002. All Rights Reserved. * * Please visit http://javatelnet.org/ for updates and contact. * * --LICENSE NOTICE-- * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * --LICENSE NOTICE-- * */package de.mud.terminal;/** * Implementation of a Video Display Unit (VDU) buffer. This class contains * all methods to manipulate the buffer that stores characters and their * attributes as well as the regions displayed. * * @author Matthias L. Jugel, Marcus Mei遪er * @version $Id: VDUBuffer.java,v 1.3 2003/04/27 16:36:45 marcus Exp $ */public class VDUBuffer { /** The current version id tag */ public final static String ID = "$Id: VDUBuffer.java,v 1.3 2003/04/27 16:36:45 marcus Exp $"; /** Enable debug messages. */ public final static int debug = 0; public int height, width; /* rows and columns */ public boolean[] update; /* contains the lines that need update */ public char[][] charArray; /* contains the characters */ public int[][] charAttributes; /* contains character attrs */ public int bufSize; public int maxBufSize; /* buffer sizes */ public int screenBase; /* the actual screen start */ public int windowBase; /* where the start displaying */ public int scrollMarker; /* marks the last line inserted */ private int topMargin; /* top scroll margin */ private int bottomMargin; /* bottom scroll margin */ // cursor variables protected boolean showcursor = true; protected int cursorX, cursorY; /** Scroll up when inserting a line. */ public final static boolean SCROLL_UP = false; /** Scroll down when inserting a line. */ public final static boolean SCROLL_DOWN = true; /** Make character normal. */ public final static int NORMAL = 0x00; /** Make character bold. */ public final static int BOLD = 0x01; /** Underline character. */ public final static int UNDERLINE = 0x02; /** Invert character. */ public final static int INVERT = 0x04; /** Lower intensity character. */ public final static int LOW = 0x08; /** Invisible character. */ public final static int INVISIBLE = 0x10; /** how much to left shift the foreground color */ public final static int COLOR_FG_SHIFT = 5; /** how much to left shift the background color */ public final static int COLOR_BG_SHIFT = 9; /** color mask */ public final static int COLOR = 0x1fe0; /** foreground color mask */ public final static int COLOR_FG = 0x1e0; /** background color mask */ public final static int COLOR_BG = 0x1e00; /** * Create a new video display buffer with the passed width and height in * characters. * @param width the length of the character lines * @param height the amount of lines on the screen */ public VDUBuffer(int width, int height) { // set the display screen size setScreenSize(width, height); } /** * Create a standard video display buffer with 80 columns and 24 lines. */ public VDUBuffer() { this(80, 24); } /** * Put a character on the screen with normal font and outline. * The character previously on that position will be overwritten. * You need to call redraw() to update the screen. * @param c x-coordinate (column) * @param l y-coordinate (line) * @param ch the character to show on the screen * @see #insertChar * @see #deleteChar * @see #redraw */ public void putChar(int c, int l, char ch) { putChar(c, l, ch, NORMAL); } /** * Put a character on the screen with specific font and outline. * The character previously on that position will be overwritten. * You need to call redraw() to update the screen. * @param c x-coordinate (column) * @param l y-coordinate (line) * @param ch the character to show on the screen * @param attributes the character attributes * @see #BOLD * @see #UNDERLINE * @see #INVERT * @see #INVISIBLE * @see #NORMAL * @see #LOW * @see #insertChar * @see #deleteChar * @see #redraw */ public void putChar(int c, int l, char ch, int attributes) { c = checkBounds(c, 0, width - 1); l = checkBounds(l, 0, height - 1); charArray[screenBase + l][c] = ch; charAttributes[screenBase + l][c] = attributes; markLine(l, 1); } /** * Get the character at the specified position. * @param c x-coordinate (column) * @param l y-coordinate (line) * @see #putChar */ public char getChar(int c, int l) { c = checkBounds(c, 0, width - 1); l = checkBounds(l, 0, height - 1); return charArray[screenBase + l][c]; } /** * Get the attributes for the specified position. * @param c x-coordinate (column) * @param l y-coordinate (line) * @see #putChar */ public int getAttributes(int c, int l) { c = checkBounds(c, 0, width - 1); l = checkBounds(l, 0, height - 1); return charAttributes[screenBase + l][c]; } /** * Insert a character at a specific position on the screen. * All character right to from this position will be moved one to the right. * You need to call redraw() to update the screen. * @param c x-coordinate (column) * @param l y-coordinate (line) * @param ch the character to insert * @param attributes the character attributes * @see #BOLD * @see #UNDERLINE * @see #INVERT * @see #INVISIBLE * @see #NORMAL * @see #LOW * @see #putChar * @see #deleteChar * @see #redraw */ public void insertChar(int c, int l, char ch, int attributes) { c = checkBounds(c, 0, width - 1); l = checkBounds(l, 0, height - 1); System.arraycopy(charArray[screenBase + l], c, charArray[screenBase + l], c + 1, width - c - 1); System.arraycopy(charAttributes[screenBase + l], c, charAttributes[screenBase + l], c + 1, width - c - 1); putChar(c, l, ch, attributes); } /** * Delete a character at a given position on the screen. * All characters right to the position will be moved one to the left. * You need to call redraw() to update the screen. * @param c x-coordinate (column) * @param l y-coordinate (line) * @see #putChar * @see #insertChar * @see #redraw */ public void deleteChar(int c, int l) { c = checkBounds(c, 0, width - 1); l = checkBounds(l, 0, height - 1); if (c < width - 1) { System.arraycopy(charArray[screenBase + l], c + 1, charArray[screenBase + l], c, width - c - 1); System.arraycopy(charAttributes[screenBase + l], c + 1, charAttributes[screenBase + l], c, width - c - 1); } putChar(width - 1, l, (char) 0); } /** * Put a String at a specific position. Any characters previously on that * position will be overwritten. You need to call redraw() for screen update. * @param c x-coordinate (column) * @param l y-coordinate (line) * @param s the string to be shown on the screen * @see #BOLD * @see #UNDERLINE * @see #INVERT * @see #INVISIBLE * @see #NORMAL * @see #LOW * @see #putChar * @see #insertLine * @see #deleteLine * @see #redraw */ public void putString(int c, int l, String s) { putString(c, l, s, NORMAL); } /** * Put a String at a specific position giving all characters the same * attributes. Any characters previously on that position will be * overwritten. You need to call redraw() to update the screen. * @param c x-coordinate (column) * @param l y-coordinate (line) * @param s the string to be shown on the screen * @param attributes character attributes * @see #BOLD * @see #UNDERLINE * @see #INVERT * @see #INVISIBLE * @see #NORMAL * @see #LOW * @see #putChar * @see #insertLine * @see #deleteLine * @see #redraw */ public void putString(int c, int l, String s, int attributes) { for (int i = 0; i < s.length() && c + i < width; i++) putChar(c + i, l, s.charAt(i), attributes); } /** * Insert a blank line at a specific position. * The current line and all previous lines are scrolled one line up. The * top line is lost. You need to call redraw() to update the screen. * @param l the y-coordinate to insert the line * @see #deleteLine * @see #redraw */ public void insertLine(int l) { insertLine(l, 1, SCROLL_UP); } /** * Insert blank lines at a specific position. * You need to call redraw() to update the screen * @param l the y-coordinate to insert the line * @param n amount of lines to be inserted * @see #deleteLine * @see #redraw */ public void insertLine(int l, int n) { insertLine(l, n, SCROLL_UP); } /** * Insert a blank line at a specific position. Scroll text according to * the argument. * You need to call redraw() to update the screen * @param l the y-coordinate to insert the line * @param scrollDown scroll down * @see #deleteLine * @see #SCROLL_UP * @see #SCROLL_DOWN * @see #redraw */ public void insertLine(int l, boolean scrollDown) { insertLine(l, 1, scrollDown); } /** * Insert blank lines at a specific position. * The current line and all previous lines are scrolled one line up. The * top line is lost. You need to call redraw() to update the screen. * @param l the y-coordinate to insert the line * @param n number of lines to be inserted * @param scrollDown scroll down * @see #deleteLine * @see #SCROLL_UP * @see #SCROLL_DOWN * @see #redraw */ public synchronized void insertLine(int l, int n, boolean scrollDown) { l = checkBounds(l, 0, height - 1); char cbuf[][] = null; int abuf[][] = null; int offset = 0; int oldBase = screenBase; if (l > bottomMargin) /* We do not scroll below bottom margin (below the scrolling region). */ return; int top = (l < topMargin ? 0 : (l > bottomMargin ? (bottomMargin + 1 < height ? bottomMargin + 1 : height - 1) : topMargin)); int bottom = (l > bottomMargin ? height - 1 : (l < topMargin ? (topMargin > 0 ? topMargin - 1 : 0) : bottomMargin)); // System.out.println("l is "+l+", top is "+top+", bottom is "+bottom+", bottomargin is "+bottomMargin+", topMargin is "+topMargin); if (scrollDown) { if (n > (bottom - top)) n = (bottom - top); cbuf = new char[bottom - l - (n - 1)][width]; abuf = new int[bottom - l - (n - 1)][width]; System.arraycopy(charArray, oldBase + l, cbuf, 0, bottom - l - (n - 1)); System.arraycopy(charAttributes, oldBase + l, abuf, 0, bottom - l - (n - 1)); System.arraycopy(cbuf, 0, charArray, oldBase + l + n, bottom - l - (n - 1)); System.arraycopy(abuf, 0, charAttributes, oldBase + l + n, bottom - l - (n - 1)); cbuf = charArray; abuf = charAttributes; } else { try { if (n > (bottom - top) + 1) n = (bottom - top) + 1; if (bufSize < maxBufSize) { if (bufSize + n > maxBufSize) { offset = n - (maxBufSize - bufSize); scrollMarker += offset; bufSize = maxBufSize; screenBase = maxBufSize - height - 1; windowBase = screenBase; } else { scrollMarker += n; screenBase += n; windowBase += n; bufSize += n; } cbuf = new char[bufSize][width]; abuf = new int[bufSize][width]; } else { offset = n; cbuf = charArray; abuf = charAttributes; } // copy anything from the top of the buffer (+offset) to the new top // up to the screenBase. if (oldBase > 0) { System.arraycopy(charArray, offset, cbuf, 0, oldBase - offset); System.arraycopy(charAttributes, offset, abuf, 0, oldBase - offset); } // copy anything from the top of the screen (screenBase) up to the // topMargin to the new screen if (top > 0) { System.arraycopy(charArray, oldBase, cbuf, screenBase, top); System.arraycopy(charAttributes, oldBase, abuf, screenBase, top); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -