Advertisement
Guest User

remote_client

a guest
Nov 21st, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import processing.net.*;
  2.  
  3. Client myClient;
  4.  
  5. final int w = 200;
  6. final int h = 200;
  7. final static int INT_SIZE = 4;
  8. final int frmRate = 30;
  9. final int port = 5204;
  10.  
  11. byte[] byteBuffer = new byte[w * h * INT_SIZE];
  12. PGraphics gr;
  13.  
  14.  
  15. void setup() {
  16.   size (w, h);
  17.   gr = createGraphics(w, h, JAVA2D);
  18.   gr.beginDraw();
  19.   gr.background(255);
  20.   gr.smooth();
  21.   gr.endDraw();
  22.   frameRate(frmRate);
  23.   myClient = new Client(this, "127.0.0.1", port);
  24. }
  25. void draw() {
  26.  
  27.   gr.beginDraw();
  28.   gr.loadPixels();
  29.   if (myClient.available() > byteBuffer.length) {
  30.     println(frameCount);
  31.     int byteCount = myClient.readBytes(byteBuffer);
  32.     for (int k = 0; k < byteBuffer.length / INT_SIZE; k++) {
  33.       byte[] b = new byte[INT_SIZE];
  34.       for (int j = 0; j < INT_SIZE; j ++) {
  35.         b[j] = byteBuffer[k * INT_SIZE + j];
  36.       }
  37.       int col = convertByteArrayToInt(b);
  38.       gr.pixels[k] = col;
  39.     }
  40.   }
  41.   gr.updatePixels();
  42.   gr.endDraw();
  43.  
  44.   image(gr, 0, 0);
  45. }
  46.  
  47. private static int convertByteArrayToInt(byte[] buffer) {
  48.   if (buffer.length != INT_SIZE) {
  49.     throw new IllegalArgumentException("buffer length must be 4 bytes!");
  50.   }
  51.  
  52.   int value = 0;
  53.   for (int i = 0; i < INT_SIZE; i++) {
  54.     value |= (0xFF & buffer[i]) << (8 * (3 - i));
  55.   }
  56.   return value;
  57. }
  58.  
  59. void stop() {
  60.   myClient.stop();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement