?? jtermboard.java
字號:
package JTerm;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.print.attribute.*;
import javax.sound.sampled.*;
import javax.sound.sampled.spi.*;
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import java.net.*;
import java.util.ResourceBundle;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class JTermBoard
extends JComponent {
static ResourceBundle res = ResourceBundle.getBundle("JTerm.Res");
int BoardWidth = 82;
int BoardHeight = 25;
int xPos = 5, yPos = 20;
int fwidth = 8, fheight = 15;
int prevLine = -1;
int cursorX = 0, cursorY = 0;
int startPos = -1, endPos = -1;
String actionLetter;
boolean active = false;
Cursor curBack, curPgDn, curPgUp;
// Connect Data
String hostname;
int hostport;
Socket sockClient;
InputStreamReader reader;
OutputStreamWriter writer;
JTermThread term;
// Link Thread
Thread TermThread;
// Refresh Schedule
Timer mytimer;
routine routine;
// Auto Return Control
private int index = 0;
//Color foreground=Color.WHITE,background=Color.BLACK;
//boolean fontBold=false,fontUnderLine=false,fontBlink=false,fontReverse=false;
JTermData[][] buffer = new JTermData[BoardWidth][BoardHeight];
ImageIcon image = new ImageIcon(JTermFrm.class.getResource("JTerm.gif"));
public JTermBoard() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void updateUI() {
setUI(UIManager.getUI(this));
}
public void sendCommand(String data) {
if (active) {
try {
writer.write(data.toCharArray());
writer.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void putChar(char ch, int x, int y) {
if (isInBoard(x, y) == false)
return;
Graphics g = this.getGraphics();
buffer[x][y].setData(ch);
if (ch > '\u00ff') {
buffer[x + 1][y].setData(res.getString("EmptyChar").charAt(0));
}
buffer[x][y].put(g);
}
// Terminate the terminal
public void Terminate() {
// 如果處于連接狀態,則提示斷開
if (sockClient != null && active) {
try {
active = false;
sockClient.close();
if (mytimer != null) {
routine.cancel();
mytimer.cancel();
}
synchronized (term.lock) {
term.lock.wait();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean isActive() {
return this.active;
}
public Dimension setJTermFont(Font font) {
int width, height;
super.setFont(font);
fheight = font.getSize();
fwidth = (int)this.getFontMetrics(font).getMaxCharBounds(this.getGraphics()).
getWidth() / 2;
height = fheight * BoardHeight + yPos * 2;
width = fwidth * BoardWidth + xPos;
this.setSize(width, height);
int i, j;
for (i = 0; i < BoardHeight; i++) {
for (j = 0; j < BoardWidth; j++) {
buffer[j][i].setSize(xPos + j * fwidth, yPos + i * fheight, fwidth,
fheight);
}
}
return new Dimension(width, height);
}
// Clear The Hole Screean
public void Clear() {
int i, j;
for (i = 0; i < BoardWidth; i++) {
for (j = 0; j < BoardHeight; j++) {
buffer[i][j].setData(res.getString("EmptyChar").charAt(0));
}
}
repaint();
}
public void setJTermColor(Color foreground, Color background) {
this.setForeground(foreground);
this.setBackground(background);
buffer[0][0].setDefaultColor(foreground, background);
this.repaint();
}
// connect to hostname at hostport
public void connect(String hostname, int hostport) {
try {
Graphics g = this.getGraphics();
g.setColor(Color.blue);
g.drawString(res.getString("Wait_Msg") + hostname + ":" + hostport, xPos,
yPos);
this.hostname = hostname;
this.hostport = hostport;
term = new JTermThread(reader);
term.setTerminal(this);
TermThread = new Thread(term, res.getString("term"));
TermThread.start();
mytimer = new Timer();
routine = new routine(this);
mytimer.schedule(routine, 0, 500);
}
catch (Exception err) {
err.printStackTrace();
}
}
// Clear according to cursor position
// forward=true : from cursor to end of screen
// forward=false: from beginning to cursor
public void Clear(int x, int y, boolean forward) {
int i = x, j = y;
if (forward) { // from cursor to end of screen
for (; j < BoardHeight; j++) {
for (; i < BoardWidth; i++) {
buffer[i][j].setData(res.getString("EmptyChar").charAt(0));
}
i = 0;
}
}
else { // from beginning to cursor
for (j = 0; j < BoardHeight - 1; j++) {
for (i = 0; i < BoardWidth; i++) {
if (i == x && j == y) {
break;
}
buffer[i][j].setData(res.getString("EmptyChar").charAt(0));
}
if (i == x && j == y) {
break;
}
}
}
repaint();
}
// clear current line
public void ClearLine(int line) {
int i;
for (i = 0; i < BoardWidth; i++) {
buffer[i][line].setData(res.getString("EmptyChar").charAt(0));
}
repaint();
}
// clear from start to end in this line
public void ClearLine(int line, int start, int end) {
int i;
if (end == -1) {
end = BoardWidth;
}
for (i = start; i < end; i++) {
buffer[i][line].setData(res.getString("EmptyChar").charAt(0));
}
repaint();
}
// refresh board
protected void refresh(Graphics2D g2) {
int i, j;
int xoff, yoff = yPos;
g2.setBackground(this.getBackground());
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setColor(this.getForeground());
int width;
for (i = 0; i < BoardHeight - 1; i++) {
xoff = xPos;
// g2.drawString(String.valueOf(i),xoff,yoff);
for (j = 0; j < BoardWidth; j++) {
if (buffer[j][i].getData() != res.getString("EmptyChar").charAt(0)) {
if (isSelected(j, i)) {
g2.setXORMode(Color.LIGHT_GRAY);
}
else if (i == prevLine) { // the cursor line
g2.setXORMode(Color.pink);
}
else {
g2.setPaintMode();
}
width = buffer[j][i].put(g2);
}
xoff += fwidth;
}
yoff += fheight;
}
g2.setColor(Color.white);
g2.drawLine(xPos + cursorX * fwidth, yPos + cursorY * fheight,
xPos + cursorX * fwidth + fwidth, yPos + cursorY * fheight);
}
protected void paintComponent(Graphics g) {
Image img = createImage(this.getWidth(), this.getHeight());
Graphics2D g2 = (Graphics2D) img.getGraphics();
g2.setFont(g.getFont());
if (active == false) {
Font font = g2.getFont();
g2.setBackground(Color.black);
g2.setColor(Color.RED);
g2.clearRect(0, 0, getWidth(), getHeight());
int x, y;
x = (getWidth() - image.getIconWidth()) / 2;
y = (getHeight() - image.getIconHeight()) / 2;
g2.drawImage(image.getImage(), x, y, Color.WHITE, null);
}
else {
refresh(g2);
}
g.drawImage(img, 0, 0, Color.WHITE, null);
}
// scroll the screen up one line
public void ScrollUp() {
int i, j;
for (i = 0; i < BoardHeight - 1; i++) {
for (j = 0; j < BoardWidth; j++) {
buffer[j][i].copyData(buffer[j][i + 1]);
}
}
repaint();
}
//
private void jbInit() throws Exception {
this.setDoubleBuffered(true);
this.setPreferredSize(new Dimension(1, 1));
this.setVerifyInputWhenFocusTarget(true);
this.setFocusTraversalKeysEnabled(true);
this.addHierarchyBoundsListener(new JTermBoard_this_hierarchyBoundsAdapter(this));
this.addMouseWheelListener(new JTermBoard_this_mouseWheelAdapter(this));
this.addMouseWheelListener(new JTermBoard_this_mouseWheelAdapter(this));
this.addKeyListener(new JTermBoard_this_keyAdapter(this));
this.addMouseMotionListener(new JTermBoard_this_mouseMotionAdapter(this));
this.addMouseListener(new JTermBoard_this_mouseAdapter(this));
this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.
getCurrentKeyboardFocusManager().
getDefaultFocusTraversalKeys(
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS));
this.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.
getCurrentKeyboardFocusManager().
getDefaultFocusTraversalKeys(
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS));
ImageIcon image = new ImageIcon(JTerm.class.getResource("back.gif"));
curBack = Toolkit.getDefaultToolkit().createCustomCursor(image.getImage(),
new Point(0, 0),
res.getString("Back_Cursor"));
image = new ImageIcon(JTerm.class.getResource("pgdn.gif"));
curPgDn = Toolkit.getDefaultToolkit().createCustomCursor(image.getImage(),
new Point(0, 0),
res.getString("PgDn_Cursor"));
image = new ImageIcon(JTerm.class.getResource("pgup.gif"));
curPgUp = Toolkit.getDefaultToolkit().createCustomCursor(image.getImage(),
new Point(0, 0),
res.getString("PgUp_Cursor"));
int i, j;
for (i = 0; i < BoardHeight; i++) {
for (j = 0; j < BoardWidth; j++) {
buffer[j][i] = new JTermData(res.getString("EmptyChar").charAt(0));
}
}
}
public void setCharColor(Color foreground, Color background, int x, int y) {
if (isInBoard(x, y) == false)
return;
buffer[x][y].setForeground(foreground);
buffer[x][y].setBackground(background);
}
public void setCharAttr(int x, int y, boolean fontBold,
boolean fontUnderLine,
boolean fontBlink,
boolean fontReverse) {
if (isInBoard(x, y) == false)
return;
buffer[x][y].setAttr(fontBold, fontUnderLine, fontBlink, fontReverse);
}
boolean isEmptyPos(int x, int y) {
char ch = buffer[x][y].getData();
if (ch == res.getString("EmptyChar").charAt(0)) {
if (x > 0) {
ch = buffer[x - 1][y].getData();
if (ch >= '\u00ff') {
return false;
}
}
return true;
}
return false;
}
// Check if highlight this line
void CheckLine(int x, int y) {
prevLine = -1;
actionLetter = "";
if (active) { // if active
int ox = x, oy = y;
x = (x - xPos) / fwidth;
y = (y - yPos) / fheight + 1; // cursor Pos
if (x >= 0 && x < BoardWidth && y < BoardHeight && y >= 0) { //if inside the terminal
if (!isEmptyPos(x, y)) { // is a print char
char ch;
if (cursorY >= 3 && cursorY <= 22) {
if (y >= 3 && y <= 22) {
prevLine = y;
if (y > cursorY) {
for (int i = 0; i < y - cursorY; i++) {
actionLetter += res.getString("KEY_DOWN");
}
}
else {
for (int i = 0; i < cursorY - y; i++) {
actionLetter += res.getString("KEY_UP");
}
}
actionLetter += "\r";
}
}
else {
boolean find = false;
int i;
while (x >= 0) {
ch = buffer[x][y].getData();
if (isLetterOrDigit(ch)) {
actionLetter = "" + ch;
find = true;
break;
}
x--;
}
if (find) {
if (actionLetter.length() == 1) {
actionLetter += "\r";
}
prevLine = y;
}
}
}
}
}
if (prevLine > 0) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
repaint();
}
else {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
repaint();
}
}
void this_mouseMoved(MouseEvent e) {
if (active) {
prevLine = -1;
if (e.getX() < xPos + 3 * fwidth) {
actionLetter = res.getString("KEY_LEFT");
this.setCursor(curBack);
}
else
if (e.getY() < yPos + 2 * fheight) {
actionLetter = res.getString("KEY_PGUP");
this.setCursor(curPgUp);
}
else
if (e.getY() > yPos + fheight * (BoardHeight - 3)) {
actionLetter = res.getString("KEY_PGDN");
this.setCursor(curPgDn);
}
else {
this.CheckLine(e.getX(), e.getY());
}
}
}
public void setActive(boolean active) {
this.active = active;
}
String getActionLetter() {
return this.actionLetter;
}
public boolean isInBoard(int x, int y) {
if (x >= 0 && x < BoardWidth && y >= 0 && y < BoardHeight)
return true;
return false;
}
//Drag start
public void setSelectStart(int x, int y) {
if (active) {
x = (x - xPos + fwidth / 2) / fwidth;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -