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

📄 jtermboard.java

📁 用JAVA程序来上BBS?这就是一个这样的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      y = (y - yPos + fheight / 2) / fheight;
      if (isInBoard(x, y)) {
        endPos = startPos = y * BoardWidth + x;
      }
    }

  }

// paste the text in the clipboard
  public void pasteselect() {
    try {
      java.awt.datatransfer.Clipboard clip = Toolkit.getDefaultToolkit().
          getSystemClipboard();
      java.awt.datatransfer.Transferable obj = clip.getContents(null);
      String data;
      data = (String) obj.getTransferData(java.awt.datatransfer.DataFlavor.
                                          stringFlavor);
      sendCommand(data);
    }
    catch (Exception err) {
      err.printStackTrace();
    }

  }

// Set the Terminal Cursor position
  public void setCursorPos(int x, int y) {
    this.cursorX = x;
    this.cursorY = y;
    repaint();
  }

//Mouse Move when Pressed
  public void setSelectEnd(int x, int y) {
    if (active) {
      x = (x - xPos + fwidth / 2) / fwidth;
      y = (y - yPos + fheight / 2) / fheight;
      if (isInBoard(x, y)) {
        endPos = y * BoardWidth + x;
        repaint();
      }
    }
  }

  boolean isSelected(int x, int y) {
    if (startPos == -1 || endPos == -1)
      return false;
    int pos = y * BoardWidth + x;
    if (pos >= startPos && pos <= endPos || pos >= endPos && pos <= startPos) {
      return true;
    }
    return false;
  }

// copy select text to clipboard
  public void copySelect() {

    int sp, ep;
    int x, y;
    if (startPos <= endPos) {
      sp = startPos;
      ep = endPos;
    }
    else {
      ep = startPos;
      sp = endPos;
    }
    String selstr = "";
    for (int i = sp; i <= ep; i++) {
      x = i % BoardWidth;
      y = i / BoardWidth;
      if (buffer[x][y].getData() == '\u0000') {
        selstr += '\u0020';
      }
      else {
        selstr += buffer[x][y].getData();
      }
      if (x == BoardWidth - 1)
        selstr += "\r\n";
    }

    java.awt.datatransfer.Clipboard clip = Toolkit.getDefaultToolkit().
        getSystemClipboard();
    java.awt.datatransfer.StringSelection obj = new java.awt.datatransfer.
        StringSelection(selstr); ;
    clip.setContents(obj, null);

  }

// select all text
  public void SelectAll() {
    if (active) {
      startPos = 0;
      endPos = BoardWidth * BoardHeight - 1;
      repaint();
    }
  }

// drag the select
  void this_mouseDragged(MouseEvent e) {
    setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    if (startPos == -1) {
      this.setSelectStart(e.getX(), e.getY());
    }
    else
      this.setSelectEnd(e.getX(), e.getY());

  }

  private void AutoReturnMsg() {
    String method = System.getProperties().getProperty("ReturnMethod");
    DefaultListModel list = new DefaultListModel();

    try {
      String filename = System.getProperties().getProperty("user.dir") +
          java.io.File.separator + "message.dat";
      FileInputStream file = new FileInputStream(filename);
      InputStreamReader in = new InputStreamReader(file);
      String data = "";
      char[] ch = new char[1];

      while (true) {
        if (in.read(ch, 0, 1) == 1) {
          if (ch[0] == '\r') {
            list.addElement(data);
            data = "";
          }
          if (ch[0] == '\n') {
            data = "";
            continue;
          }
          else {
            data += ch[0];
          }
          continue;
        }
        break;
      }
      in.close();
    }
    catch (Exception e) {

    }
    int count = list.size();

    if (method.compareTo("Sort") == 0) {
      if (count > 0) {
        index %= count;
      }
    }
    else {
      java.util.Random rand = new java.util.Random();
      index = rand.nextInt();
      if (index < 0)
        index = -index;
      index %= count;
      System.out.println(index);
    }
    this.sendCommand("\u001A" + (String) list.elementAt(index) + "\r\n");
    index++;
  }

// receive message
  public void OnRecvMsg() {
    String auto = System.getProperties().getProperty("AutoReturnMsg");
    if (auto.compareTo("true") == 0) {
      AutoReturnMsg();
    }
    else {
      String ring = System.getProperties().getProperty("SoundOn");
      if (ring.compareTo("true") == 0) {
        Ring();
      }
    }
  }

  void Ring() {

    try {

      File input = new File(res.getString("RingWavFile"));
      javax.sound.sampled.AudioInputStream audio = AudioSystem.
          getAudioInputStream(input);
      SourceDataLine line;
      // get an AudioInputStream of the desired format for playback
      AudioFormat format = AudioSystem.getAudioFileFormat(input).getFormat();
      AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(
          format, audio);

      if (playbackInputStream == null) {
        return;
      }

      DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
      if (!AudioSystem.isLineSupported(info)) {
        return;
      }
      try {
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format, 2048);

      }
      catch (LineUnavailableException ex) {
        line = null;
        ex.printStackTrace();
      }

      int frameSizeInBytes = format.getFrameSize();
      int bufferLengthInFrames = line.getBufferSize() / 8;
      int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
      byte[] data = new byte[bufferLengthInBytes];
      int numBytesRead = 0;

      // start the source data line
      line.start();
      while (true) {
        try {
          if ( (numBytesRead = playbackInputStream.read(data)) == -1) {
            break;
          }
          int numBytesRemaining = numBytesRead;
          while (numBytesRemaining > 0) {
            numBytesRemaining -= line.write(data, 0, numBytesRemaining);
          }
        }
        catch (Exception e) {
          e.printStackTrace();
          break;
        }
      }
      line.drain();
      line.stop();
      line.close();
      line = null;
      input = null;
      System.gc();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  boolean isLetter(char ch) {
    if (ch <= 'Z' &&
        ch >= 'A') {
      return true;
    }
    if (ch <= 'z' &&
        ch >= 'a') {
      return true;
    }
    return false;
  }

  boolean isDigit(char ch) {
    if (ch <= '9' && ch >= '0') {
      return true;
    }
    return false;
  }

  boolean isLetterOrDigit(char ch) {
    return isLetter(ch) || isDigit(ch);
  }

// is a empty char or only a space char
  boolean isSeperator(char ch) {
    if (ch == res.getString("EmptyChar").charAt(0) || ch == '\u0020') {
      return true;
    }
    return false;
  }

  void this_keyTyped(KeyEvent e) {
    if (active == false) {
      connect(hostname, hostport);
      return;
    }
    try {
      sendCommand(String.valueOf(e.getKeyChar()));
      if (e.getKeyChar() == '\u001A') {
        String cmd = JOptionPane.showInputDialog(this, "请输入回复信息");
        if (cmd != null) {
          sendCommand(cmd + "\r\n");
        }
      }

    }
    catch (Exception err) {
    }

  }

  void this_keyPressed(KeyEvent e) {
    if (active == false)
      return;
    try {
      switch (e.getKeyCode()) {
        case KeyEvent.VK_RIGHT:
          sendCommand(res.getString("KEY_RIGHT"));
          break;
        case KeyEvent.VK_UP:
          sendCommand(res.getString("KEY_UP"));
          break;
        case KeyEvent.VK_DOWN:
          sendCommand(res.getString("KEY_DOWN"));
          break;
        case KeyEvent.VK_LEFT:
          sendCommand(res.getString("KEY_LEFT"));
          break;
        case KeyEvent.VK_PAGE_DOWN:
          sendCommand(res.getString("KEY_PGDN"));
          break;
        case KeyEvent.VK_PAGE_UP:
          sendCommand(res.getString("KEY_PGUP"));
          break;
        case KeyEvent.VK_END:
          sendCommand(res.getString("KEY_END"));
          break;
        case KeyEvent.VK_HOME:
          sendCommand(res.getString("KEY_HOME"));

          break;
        case KeyEvent.VK_INSERT:
          sendCommand(res.getString("KEY_INSERT"));

          break;
        case KeyEvent.VK_DELETE:
          sendCommand(res.getString("KEY_DELETE"));
          break;
        case KeyEvent.VK_COPY:
          this.copySelect();
          break;
        case KeyEvent.VK_PASTE:
          this.pasteselect();
          break;
        default:
          break;
      }

    }
    catch (Exception err) {

    }

  }

  void this_mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON1) {
      startPos = endPos = -1;
      repaint();
    }
  }

  void this_mouseWheelMoved(MouseWheelEvent e) {
    int c = e.getUnitsToScroll(); //e.getScrollAmount();
    //System.out.println("amount:"+c+",direct:"+d);
    String cmd = "";
    if (c > 0) {
      for (int i = 0; i < c; i++) {
        cmd += res.getString("KEY_DOWN");
      }
    }
    else {
      c = -c;
      for (int i = 0; i < c; i++) {
        cmd += res.getString("KEY_UP");
      }

    }
    sendCommand(cmd);
  }

  void this_ancestorResized(HierarchyEvent e) {
    //System.out.println("fwidth="+fwidth);
    int width, height;
    height = (getHeight() - 2 * yPos) / BoardHeight;
    if (height != fheight) {
      Font oldfont = this.getFont();
      fheight = height;
      Font font = new Font(oldfont.getFontName(), oldfont.getStyle(), fheight);
      fwidth = (int)this.getFontMetrics(font).getMaxCharBounds(this.getGraphics()).
          getWidth() / 2;
      this.setFont(font);
      System.getProperties().setProperty("FontSize",String.valueOf(fheight));
      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);
        }

      }
    }
  }
}

class JTermBoard_this_mouseAdapter
    extends java.awt.event.MouseAdapter {
  JTermBoard adaptee;

  JTermBoard_this_mouseAdapter(JTermBoard adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.this_mouseClicked(e);
  }
}

class JTermBoard_this_mouseMotionAdapter
    extends java.awt.event.MouseMotionAdapter {
  JTermBoard adaptee;

  JTermBoard_this_mouseMotionAdapter(JTermBoard adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseMoved(MouseEvent e) {
    adaptee.this_mouseMoved(e);
  }

  public void mouseDragged(MouseEvent e) {
    adaptee.this_mouseDragged(e);
  }
}

class routine
    extends java.util.TimerTask {
  protected JTermBoard parent;
  int slot=500;
  int count = 0;
  public routine(JTermBoard owner) {
    parent = owner;
  }

  public void run() {
    int i, j;
    for (i = 0; i < parent.BoardHeight; i++) {
      for (j = 0; j < parent.BoardWidth; j++) {
          parent.buffer[j][i].blink();
      }
    }
    count++;
    int dumyslottime=Integer.parseInt(System.getProperties().getProperty("DumySlotTime"))*1000;
    if (count*slot==dumyslottime){
         String t=System.getProperties().getProperty("DumyChar");
         t=String.valueOf((char)Integer.parseInt(t));
         parent.sendCommand(t);
      	count=0;
    }
    parent.repaint();
  }
}

class JTermBoard_this_keyAdapter
    extends java.awt.event.KeyAdapter {
  JTermBoard adaptee;

  JTermBoard_this_keyAdapter(JTermBoard adaptee) {
    this.adaptee = adaptee;
  }

  public void keyTyped(KeyEvent e) {
    adaptee.this_keyTyped(e);
  }

  public void keyPressed(KeyEvent e) {
    adaptee.this_keyPressed(e);
  }
}

class JTermBoard_this_mouseWheelAdapter implements java.awt.event.MouseWheelListener {
  JTermBoard adaptee;

  JTermBoard_this_mouseWheelAdapter(JTermBoard adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseWheelMoved(MouseWheelEvent e) {
    adaptee.this_mouseWheelMoved(e);
  }
}

class JTermBoard_this_hierarchyBoundsAdapter extends java.awt.event.HierarchyBoundsAdapter {
  JTermBoard adaptee;

  JTermBoard_this_hierarchyBoundsAdapter(JTermBoard adaptee) {
    this.adaptee = adaptee;
  }
  public void ancestorResized(HierarchyEvent e) {
    adaptee.this_ancestorResized(e);
  }
}


⌨️ 快捷键说明

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