Java Code Examples for io.termd.core.util.Vector#y()

The following examples show how to use io.termd.core.util.Vector#y() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Screencaster.java    From termd with Apache License 2.0 5 votes vote down vote up
private void broadcast() {
  if (interrupted) {
    conn.close();
    return;
  }
  BufferedImage capture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  Vector size = conn.size();
  Image temp = capture.getScaledInstance(size.x(), size.y(), Image.SCALE_SMOOTH);
  BufferedImage scaled = new BufferedImage(size.x(), size.y(), BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = scaled.createGraphics();
  g2d.drawImage(temp, 0, 0, null);
  g2d.dispose();
  StringBuilder sb = new StringBuilder();
  for (int y = 0; y < size.y(); y++) {
    sb.append("\033[").append(y + 1).append(";1H");
    for (int x = 0; x < size.x(); x++) {
      Color pixel = new Color(scaled.getRGB(x, y));
      int r = pixel.getRed();
      int g = pixel.getGreen();
      int b = pixel.getBlue();
      double grey = (r + g + b) / 3.0;
      if (grey < 51) {
        sb.append('\u2588');
      } else if (grey < 102) {
        sb.append('\u2593');
      } else if (grey < 153) {
        sb.append('\u2592');
      } else if (grey < 204) {
        sb.append('\u2591');
      } else {
        sb.append(' ');
      }
    }
  }
  conn.write(sb.toString());
  conn.schedule(this::broadcast, 100, TimeUnit.MILLISECONDS);
}
 
Example 2
Source File: Screencaster.java    From termd with Apache License 2.0 4 votes vote down vote up
private void broadcast() {
  if (interrupted) {
    conn.close();
    return;
  }
  BufferedImage capture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  Vector size = conn.size();
  Image temp = capture.getScaledInstance(size.x(), size.y(), Image.SCALE_SMOOTH);
  BufferedImage scaled = new BufferedImage(size.x(), size.y(), BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = scaled.createGraphics();
  g2d.drawImage(temp, 0, 0, null);
  g2d.dispose();
  StringBuilder sb = new StringBuilder();
  for (int y = 0; y < size.y(); y++) {
    sb.append("\033[").append(y + 1).append(";1H");
    for (int x = 0; x < size.x(); x++) {
      Color pixel = new Color(scaled.getRGB(x, y));
      int r = pixel.getRed();
      int g = pixel.getGreen();
      int b = pixel.getBlue();
      double grey = (r + g + b) / 3.0;
      if (grey < 51) {
        sb.append('\u2588');
      } else if (grey < 102) {
        sb.append('\u2593');
      } else if (grey < 153) {
        sb.append('\u2592');
      } else if (grey < 204) {
        sb.append('\u2591');
      } else {
        sb.append(' ');
      }
    }
  }
  conn.write(sb.toString());
  conn.schedule(new Runnable() {
    @Override
    public void run() {
      broadcast();
    }
  }, 100, TimeUnit.SECONDS);
}
 
Example 3
Source File: SnakeGame.java    From termd with Apache License 2.0 4 votes vote down vote up
private void reset(Vector size) {
  // Fill factory area / 25
  game = new GameState(size.x(), size.y(), (size.x() * size.y()) / 10);
}
 
Example 4
Source File: Readline.java    From termd with Apache License 2.0 4 votes vote down vote up
void resize(int oldWith, int newWidth) {

      // Erase screen
      LineBuffer abc = new LineBuffer(buffer.getCapacity());
      abc.insert(currentPrompt);
      abc.insert(buffer.toArray());
      abc.setCursor(currentPrompt.length() + buffer.getCursor());

      // Recompute new cursor
      Vector pos = abc.getCursorPosition(newWidth);
      int curWidth = pos.x();
      int curHeight = pos.y();

      // Recompute new end
      Vector end = abc.getPosition(abc.getSize(), oldWith);
      int endHeight = end.y() + end.x() / newWidth;

      // Position at the bottom / right
      Consumer<int[]> out = conn.stdoutHandler();
      out.accept(new int[]{'\r'});
      while (curHeight != endHeight) {
        if (curHeight > endHeight) {
          out.accept(new int[]{'\033','[','1','A'});
          curHeight--;
        } else {
          out.accept(new int[]{'\n'});
          curHeight++;
        }
      }

      // Now erase and redraw
      while (curHeight > 0) {
        out.accept(new int[]{'\033','[','1','K'});
        out.accept(new int[]{'\033','[','1','A'});
        curHeight--;
      }
      out.accept(new int[]{'\033','[','1','K'});

      // Now redraw
      out.accept(Helper.toCodePoints(currentPrompt));
      refresh(new LineBuffer(), newWidth);
    }
 
Example 5
Source File: SnakeGame.java    From termd with Apache License 2.0 4 votes vote down vote up
private void reset(Vector size) {
  // Fill factory area / 25
  game = new GameState(size.x(), size.y(), (size.x() * size.y()) / 10);
}
 
Example 6
Source File: Readline.java    From termd with Apache License 2.0 4 votes vote down vote up
void resize(int oldWith, int newWidth) {

      // Erase screen
      LineBuffer abc = new LineBuffer();
      abc.insert(currentPrompt);
      abc.insert(buffer.toArray());
      abc.setCursor(currentPrompt.length() + buffer.getCursor());

      // Recompute new cursor
      Vector pos = abc.getCursorPosition(newWidth);
      int curWidth = pos.x();
      int curHeight = pos.y();

      // Recompute new end
      Vector end = abc.getPosition(abc.getSize(), oldWith);
      int endHeight = end.y() + end.x() / newWidth;

      // Position at the bottom / right
      Consumer<int[]> out = conn.stdoutHandler();
      out.accept(new int[]{'\r'});
      while (curHeight != endHeight) {
        if (curHeight > endHeight) {
          out.accept(new int[]{'\033','[','1','A'});
          curHeight--;
        } else {
          out.accept(new int[]{'\n'});
          curHeight++;
        }
      }

      // Now erase and redraw
      while (curHeight > 0) {
        out.accept(new int[]{'\033','[','1','K'});
        out.accept(new int[]{'\033','[','1','A'});
        curHeight--;
      }
      out.accept(new int[]{'\033','[','1','K'});

      // Now redraw
      out.accept(Helper.toCodePoints(currentPrompt));
      refresh(new LineBuffer(), newWidth);
    }