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

📄 jguitermv2.java

📁 java版Term
💻 JAVA
字号:
import java.io.*; 
import java.net.Socket; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Arrays; 
  
public class JGuiTermV2 implements WindowListener,WindowFocusListener,KeyListener,InputMethodListener,Runnable{ 
 static int BUFFER_SIZE=1024; 
 static int FONT_SIZE=24; 
 static int XS=80,YS=24; 
 static Color[][] partten=new Color[][]{ 
 { 
   new Color(0,0,0),new Color(132,0,0),new Color(0,130,0),new Color(132,130,0), 
   new Color(0,0,132),new Color(132,0,132),new Color(0,130,132),new Color(198,195,198) 
  },
  { 
   new Color(132,130,132),new Color(255,0,0),new Color(0,255,0),new Color(255,255,0), 
   new Color(0,0,255),new Color(255,0,255),new Color(0,255,255),new Color(255,255,255) 
  } 
 }; 
 static byte[] loginqueue=new byte[]{'y',10,'y',10,'y',10,10,10,'q','q','q','q','i',10,'l',10,'q','q','q','q','i',10,'l',10}; 
 static byte[] logoutqueue=new byte[]{'q','q','q','q','q','g',10,10,10}; 
 static int MaxRetry=5; 
 char[] readBuffer,lineBuffer; 
 int[] syntaxBuffer; 
 int syntaxPointer,linePointer,CharWidth; 
 Socket sock; 
 Reader in; 
 PrintStream out; 
 boolean ansiflag,underline; 
int highlight,blink,opposite;int len,random;int x,y;int memX,memY;int Height,ByteWidth;int maxX,maxY; 
 Frame main; 
 Canvas canvas; 
 Panel pane; 
 Image currentForeImage; 
 Graphics fore,currentForeGraphics; 
 int currentFore,currentBack; 
 public static void main(String[] args) { 
  new JGuiTermV2(); 
 } 
 JGuiTermV2(){ 
  try{ 
   readBuffer=new char[BUFFER_SIZE]; 
   lineBuffer=new char[BUFFER_SIZE]; 
   syntaxBuffer=new int[32]; 
   Arrays.fill(syntaxBuffer,0); 
   syntaxPointer=0; 
   sock=new Socket("ytht.net",23); 
   in=new InputStreamReader(sock.getInputStream()); 
   out=new PrintStream(sock.getOutputStream()); 
//   in=new FileReader("C:\\TermTest.txt"); 
//   out=System.out; 
   ansiflag=false; 
   highlight=0; 
   underline=false; 
   blink=opposite=0; 
   linePointer=0; 
   len=0; 
   x=y=0; 
   memX=memY=0; 
   CharWidth=0; 
   main=new Frame("JGuiTermV2"); 
   pane=new Panel(); 
   canvas=new Canvas(); 
   main.add(pane); 
   pane.add(canvas); 
   main.setBackground(Color.black); 
   main.setVisible(true); 
   fore=canvas.getGraphics(); 
   this.prepareGraphics(fore,Color.lightGray,false); 
   FontMetrics fm=fore.getFontMetrics(); 
   Height=fm.getHeight()-2; 
   ByteWidth=fm.stringWidth("我")/2; 
   maxX=(XS+1)*ByteWidth;maxY=(YS+2)*Height; 
   canvas.setSize(maxX,maxY); 
   main.pack(); 
   main.setResizable(false); 
   main.addWindowFocusListener(this); 
   main.addWindowListener(this); 
   main.addKeyListener(this); 
   main.enableInputMethods(true); 
   canvas.addKeyListener(this); 
   canvas.enableInputMethods(true); 
   main.addInputMethodListener(this); 
   main.requestFocus(); 
   this.prepareGraphics(fore,Color.lightGray,false); 
   Thread a=new Thread(this); 
   a.setDaemon(true); 
   a.start(); 
  }catch(FileNotFoundException fnfe){ 
   fnfe.printStackTrace(System.err); 
  }catch(IOException ioe){ 
   ioe.printStackTrace(System.err); 
  } 
 } 
 public void keyTyped(KeyEvent ke){ 
  out.print(ke.getKeyChar()); 
 } 
 public void keyPressed(KeyEvent ke){ 
  if(ke.getSource()!=main){ 
   ke.setSource(main); 
  } 
 } 
 public void keyReleased(KeyEvent ke){ 
  if(ke.getSource()!=main){ 
   ke.setSource(main); 
  } 
 } 
 public void inputMethodTextChanged(InputMethodEvent ime){ 
  System.out.println (ime); 
 } 
 public void caretPositionChanged(InputMethodEvent ime){ 
  System.out.println (ime); 
 } 
 public void windowActivated(WindowEvent e){ 
  main.repaint(); 
 } 
 public void windowClosed(WindowEvent e){ 
  System.out.println (e); 
 } 
 public void windowClosing(WindowEvent e){ 
  System.out.println (e); 
  System.exit(0); 
 } 
 public void windowDeactivated(WindowEvent e){ 
  System.out.println (e); 
 } 
 public void windowDeiconified(WindowEvent e){ 
  System.out.println (e); 
 } 
 public void windowIconified(WindowEvent e) { 
  System.out.println (e); 
 } 
 public void windowOpened(WindowEvent e){ 
  System.out.println (e); 
 } 
 public void windowGainedFocus(WindowEvent e){ 
  System.out.println (e); 
  //text 
 } 
 public void windowLostFocus(WindowEvent e){ 
  System.out.println (e); 
 } 
 public void run(){ 
  while(true){ 
   try{ 
    if(in.ready()){ 
     len=in.read(readBuffer); 
     drawChars(readBuffer,len); 
    }else{ 
     flushBuffer(); 
    } 
    Thread.currentThread().sleep(20); 
   }catch(IOException ioe){ 
    ioe.printStackTrace(System.err); 
   }catch(InterruptedException ie){ 
    ie.printStackTrace(System.err); 
   } 
  } 
 } 
 protected void flushBuffer(){ 
  if(linePointer!=0){ 
   directDraw(lineBuffer,linePointer,CharWidth,x,y,false); 
   linePointer=0; 
  } 
  x+=CharWidth; 
  CharWidth=0; 
 } 
 protected void prepareGraphics(Graphics target,Color fore,boolean isXOR){ 
   target.setFont(new Font("NSimSun",Font.PLAIN,FONT_SIZE)); 
   if(isXOR){ 
    target.setXORMode(fore); 
   }else{ 
    target.setPaintMode(); 
   } 
   target.setColor(fore); 
 } 
 protected void directDraw(char[] it,int len,int wid,int x,int y,boolean isXOR){ 
  currentForeImage=main.createImage(wid*ByteWidth,Height); 
  currentForeGraphics=currentForeImage.getGraphics(); 
  prepareGraphics(currentForeGraphics,partten[0][currentBack],false); 
  currentForeGraphics.fillRect(0,0,wid*ByteWidth,Height); 
  prepareGraphics(currentForeGraphics,partten[highlight][currentFore],isXOR); 
  currentForeGraphics.drawChars(it,0,len,0,Height-5); 
  currentForeGraphics.drawLine(0,Height,wid*ByteWidth,Height); 
  fore.drawImage(currentForeImage,x*ByteWidth,(y+1)*Height,main); 
  currentForeGraphics.dispose(); 
 } 
 protected void drawChars(char[] it,int len){ 
  for(int i=0;i<len;i++){ 
   if(!ansiflag){ 
    if(it[i]!=27){ 
     if(it[i]>31){ 
      if(x+CharWidth==80){ 
       x=0; 
       y++; 
      } 
      if(it[i]>255){ 
       CharWidth+=2; 
      }else{ 
       CharWidth++; 
      } 
      lineBuffer[linePointer++]=it[i]; 
     }else if(it[i]==10){ 
      this.flushBuffer(); 
      x=0; 
      y++; 
     } 
    }else{ 
     ansiflag=true; 
     this.flushBuffer(); 
    } 
   }else{ 
    if("[0123456789;\u001b".indexOf(it[i])!=-1){ 
     switch(it[i]){ 
      case ';': 
      //expression devide 
      syntaxPointer++; 
      break; 
      case '1':case '2':case '3':case '4':case '5': 
      case '6':case '7':case '8':case '9':case '0': 
      //expression value 
      syntaxBuffer[syntaxPointer]=syntaxBuffer[syntaxPointer]*10+Character.getNumericValue(it[i]); 
      break; 
      case '[': 
      //expression start 
      break; 
      default: 
      //second /u001b 
      break; 
     } 
    }else{ 
     //expression ends 
     ansiflag=false; 
     switch(it[i]){ 
      case 'H': 
      //locater 
      if(syntaxPointer==1){ 
       y=syntaxBuffer[0]-1; 
       x=syntaxBuffer[1]-1; 
      }else{ 
       x=0; 
       y=0; 
      } 
      break; 
      case 'm': 
      //color changer 
      if(syntaxPointer==0&&syntaxBuffer[syntaxPointer]==0){ 
       highlight=0; 
       underline=false; 
       blink=opposite=0; 
       currentFore=7; 
       currentBack=0; 
      } 
      for(int j=0;j<=syntaxPointer;j++){ 
       int value=(syntaxBuffer[j]); 
       if((0<=value)&&(value<=7)){ 
        switch(value){ 
         case 0: 
         //set default color 
         break; 
         case 1: 
         //set highlight 
         highlight=1; 
         break; 
         case 4: 
         //set underline 
         underline=true; 
         break; 
         case 5: 
         //set blink 
         blink=1; 
         break; 
         case 7: 
         //set opposite 
         opposite=1; 
         break; 
         default: 
         //others 
         break; 
        } 
       }else if((30<=value)&&(value<=37)){ 
        currentFore=value-30; 
       }else if((40<=value)&&(value<=47)){ 
        currentBack=value-40; 
       } 
      } 
      break; 
      case 's': 
      //save cursor posion 
      memX=x;memY=y; 
      break; 
      case 'u': 
      //restore cursor posion 
      x=memX;y=memY; 
      break; 
      case 'J': 
      //clear screen 
      currentForeImage=main.createImage(maxX,maxY); 
      fore.drawImage(currentForeImage,0,0,main); 
      x=y=0; 
      break; 
      case 'K': 
      //clear line 
      currentForeImage=main.createImage(maxX,Height); 
      fore.drawImage(currentForeImage,x*ByteWidth,(y+1)*Height,main); 
      break; 
      default: 
      System.out.println (it[i]); 
      break; 
     } 

     syntaxPointer=0; 
     Arrays.fill(syntaxBuffer,0); 
    } 
   } 
  } 
 } 
} 

⌨️ 快捷键说明

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