Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.net.*;
- Client myClient;
- final int w = 200;
- final int h = 200;
- final static int INT_SIZE = 4;
- final int frmRate = 30;
- final int port = 5204;
- byte[] byteBuffer = new byte[w * h * INT_SIZE];
- PGraphics gr;
- void setup() {
- size (w, h);
- gr = createGraphics(w, h, JAVA2D);
- gr.beginDraw();
- gr.background(255);
- gr.smooth();
- gr.endDraw();
- frameRate(frmRate);
- myClient = new Client(this, "127.0.0.1", port);
- }
- void draw() {
- gr.beginDraw();
- gr.loadPixels();
- if (myClient.available() > byteBuffer.length) {
- println(frameCount);
- int byteCount = myClient.readBytes(byteBuffer);
- for (int k = 0; k < byteBuffer.length / INT_SIZE; k++) {
- byte[] b = new byte[INT_SIZE];
- for (int j = 0; j < INT_SIZE; j ++) {
- b[j] = byteBuffer[k * INT_SIZE + j];
- }
- int col = convertByteArrayToInt(b);
- gr.pixels[k] = col;
- }
- }
- gr.updatePixels();
- gr.endDraw();
- image(gr, 0, 0);
- }
- private static int convertByteArrayToInt(byte[] buffer) {
- if (buffer.length != INT_SIZE) {
- throw new IllegalArgumentException("buffer length must be 4 bytes!");
- }
- int value = 0;
- for (int i = 0; i < INT_SIZE; i++) {
- value |= (0xFF & buffer[i]) << (8 * (3 - i));
- }
- return value;
- }
- void stop() {
- myClient.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement