⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtermthread.java

📁 用JAVA程序来上BBS?这就是一个这样的程序
💻 JAVA
字号:
package JTerm;

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.ResourceBundle;

class JTermThread
    implements Runnable {
  static public Object lock=new Object();
  static ResourceBundle res = ResourceBundle.getBundle("JTerm.Res");
  InputStreamReader reader;
  JTermBoard Area;
  int cursorX, cursorY;
  Color foreground, background;
  boolean fontBold = false, fontUnderLine = false, fontBlink = false,
      fontReverse = false;
  public JTermThread(InputStreamReader reader) {
    this.reader = reader;
  }

  public void setTerminal(JTermBoard Area) {
    this.Area = Area;
  }

  private void SaveMessage(){
  	
  }
  public void run() {

    char[] buf = new char[80 * 25];
    char[] fmt = new char[20];
    int j = 0;
    int len;
    int k = 0;
    int pos;
    cursorX = 0;
    cursorY = 0;
    boolean echo = true;
    try{
      InetAddress iAddr = InetAddress.getByName(Area.hostname);
      Area.sockClient = new Socket(iAddr, Area.hostport);
      Area.sockClient.setSendBufferSize(256);
      Area.sockClient.setReceiveBufferSize(2048);
      Area.reader = new InputStreamReader(Area.sockClient.getInputStream(),"GBK");
      Area.writer = new OutputStreamWriter(Area.sockClient.getOutputStream(),"GBK");
      Area.setActive(true);      
    }
    catch(Exception e){
      e.printStackTrace();
    }
   while (true) {  //是否一直处于连接状态
     try {
       len = Area.reader.read(buf, 0, 80 * 25);
       j = 0;
       if (len <= 0) {
         break;
       }
       while (j < len) {

         try {

           char ch = buf[j];
           if (ch == '\u001B') {
             echo = false;
             k = 0;
           }
           if (echo) {
             switch (ch) {
               case '\r':
                 cursorX = 0;
                 break;
               case '\n':
                 cursorY++;
                 fontBlink = false;
                 fontBold = false;
                 fontReverse = false;
                 fontUnderLine = false;
                 if (cursorY > 23) {
                   Area.ScrollUp();
                   cursorY = 23;
                 }
                 break;
               case '\b':
                 cursorX--;
                 break;
               case '\u0007':
                 Area.OnRecvMsg();
                 SaveMessage();
                 break;
               default:
                 Area.setCharAttr(cursorX, cursorY, fontBold, fontUnderLine,
                                  fontBlink, fontReverse);
                 Area.setCharColor(foreground, background, cursorX, cursorY);
                 Area.putChar(ch, cursorX, cursorY);
                 if (ch > '\u00ff') {
                   cursorX++;
                 }
                 cursorX++;

             }

           }
           else {
             fmt[k++] = ch;
             if (Character.isLetter(ch)) {
               echo = true;
               PerformControl(fmt, k);
             }
           }
           j++;
         }
         catch (Exception e) {
           e.printStackTrace();
           j++;
         }

       }
       Area.setCursorPos(cursorX, cursorY);
     }
     catch (Exception err) {
     	 //连接意外中断
         j = 0;        
         err.printStackTrace();
         break;
     }

   }
    try {
    
  if (Area.active==false){ //如果是主动中断
       synchronized(lock){
 		lock.notify();
 		}
  }
  else{
  		Area.active=false;
  }
  		Area.Clear();     
  }
    catch (Exception e) {
      e.printStackTrace();
       Area.active = false;
     }
  }

  public void PerformControl(char[] sequence, int len) {
    char act = sequence[len - 1];
    int sel;
    int x = 1, y = 1;
    int pos;
    String[] Pn;
    try {
      String para = String.valueOf(sequence).substring(2, len - 1);
      switch (act) {
        case 'H':
        case 'f':
          Pn = para.split(";");
          if (Pn.length == 2) {
            x = Integer.parseInt(Pn[1]);
            y = Integer.parseInt(Pn[0]);
          }
          cursorX = x -1;
          cursorY = y -1;
          break;
        case 'K':
 			foreground = Area.getForeground();
            background = Area.getBackground();
          if (para.length() == 0) {
            sel = 0;
          }
          else {
            sel = Integer.parseInt(para);
          }
          switch (sel) {
            case 0:
              Area.ClearLine(cursorY, cursorX, -1);
              break;
            case 1:
              Area.ClearLine(cursorY, 0, cursorX);
              break;
            case 2:
              Area.ClearLine(cursorY);
              break;
          }
          break;
        case 'J':
           foreground = Area.getForeground();
            background = Area.getBackground();
          if (para.length() == 0) {
            sel = 0;
          }
          else {
            sel = Integer.parseInt(para);
          }
          switch (sel) {
            case 0:
              Area.Clear(cursorX, cursorY, true);
              break;
            case 1:
              Area.Clear(cursorX, cursorY, false);
            case 2:
              Area.Clear();
          }
          break;

        case 'm':
          if (para.length() == 0) {
            foreground = Area.getForeground();
            background = Area.getBackground();
            this.fontBlink = false;
            this.fontBold = false;
            this.fontReverse = false;
            this.fontUnderLine = false;
            break;
          }
          Pn = para.split(";");
          for (x = 0; x < Pn.length; x++) {
            try{
              sel = Integer.parseInt(Pn[x]);
            }catch(Exception e){
              System.out.println(Pn[x]);
              sel=0;
            }
            switch (sel) {
              case 30:
                foreground = Color.DARK_GRAY;
                break;
              case 31:
                foreground = Color.RED;
                break;
              case 32:
                foreground = Color.GREEN;
                break;
              case 33:
                foreground = Color.YELLOW;
                break;
              case 34:
                foreground = Color.BLUE;
                break;
              case 35:
                foreground = Color.MAGENTA;
                break;
              case 36:
                foreground = Color.CYAN;
                break;
              case 37:
                foreground = Color.WHITE;
                break;
              case 40:
                background = Color.BLACK;
                break;
              case 41:
                background = Color.RED;
                break;
              case 42:
                background = Color.GREEN;
                break;
              case 43:
                background = Color.YELLOW;
                break;
              case 44:
                background = Color.BLUE;
                break;
              case 45:
                background = Color.MAGENTA;
                break;
              case 46:
                background = Color.CYAN;
                break;
              case 47:
                background = Color.WHITE;
                break;
              case 1:
                fontBold = true;
                break;
              case 4:
                fontUnderLine = true;
                break;
              case 5:
                fontBlink = true;
                break;
              case 7:
                fontReverse = true;
                break;
             default:
              foreground = Area.getForeground();
           	  background = Area.getBackground();
              this.fontBlink = false;
              this.fontBold = false;
              this.fontReverse = false;
              this.fontUnderLine = false;
                break;
            }
          }
          break;
        case 'A':
           if (para.length() == 0){
               cursorY--;
           }else{
               cursorY-=Integer.parseInt(para);
           }
          break;
        case 'B':
          if (para.length() == 0){
              cursorY++;
          }else{
              cursorY+=Integer.parseInt(para);
          }
          break;
        case 'C':
          if (para.length() == 0){
    cursorX++;
}else{
    cursorX+=Integer.parseInt(para);
}

          break;
        case 'D':
          if (para.length() == 0){
     cursorX--;
 }else{
     cursorX-=Integer.parseInt(para);
 }
 break;

      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -