What processing library to use to create simultaneously running renders

淺唱寂寞╮ 提交于 2019-12-13 04:37:49

问题


I was recently working on processing (http://processing.org/) and was hoping to find out whether there is a library which would help me create two different windows which are rendering two views of the same scene. Also would there be any way to directly switch from one to another.


回答1:


I don't know if such library exists for Processing, but you can easily create two windows using the following code based on a recent Processing forum thread

import java.awt.Frame;
import java.awt.MouseInfo;
import java.awt.Point;
PFrame f;
secondApplet s;

void setup () {
  size(500, 500);
  PFrame f = new PFrame();
  frame.setTitle("first window");
}


void draw () {
  background(255);
  s.background(0); // Second window background method

// .... //

}

public class PFrame extends Frame {
  public PFrame() {
    setBounds(0, 0, 500, 500);
    s = new secondApplet();
    setResizable(false);
    add(s); 
    s.init(); 
    show();
    setTitle("second window");
  }
}

public class secondApplet extends PApplet {
  public void setup() {
    size(500, 500);
  }
  public void draw() {
  }
}

Then you could create a drawing in each window based on a common source of data and methods. I'm no expert, but I hope this helps you get started with your project.

Note that GUI libraries such as G4P support the creation of multiple windows too.

EDIT: Comment related code:

//...//
int x=50;
void draw () {
rect(width/2,0,x,x);
s.rect(s.width/2,0,x+10,x+10);
}
//...//



回答2:


Here's a quick and hacky example:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.*;

ImagePanel view2;
PImage viewport2 = new PImage(400,400,RGB);

void setup(){
  size(800,400,P3D);
  noStroke();

  frame.setSize(400,400);
  frame.setTitle("view 1");

  view2 = new ImagePanel((BufferedImage)viewport2.getNative());
  JFrame v2 = new JFrame("view 2");
  v2.setSize(400,400);
  v2.add(view2);
  v2.show();
}
void draw(){
  background(0);
  lights();
  //view1
  pushMatrix();
    translate(200,200,0);
    rotateX(45);
    rotateY(map(mouseX,0,height,-PI,PI));
    renderScene();
  popMatrix();
  //view2
  pushMatrix();
  translate(600,200,0);
  rotateY(45);
  rotateX(map(mouseX,0,height,-PI,PI));
  renderScene();
  popMatrix();
  viewport2 = get(400,0,400,400);//fetch a screenshot from the invisible side (right side);
  view2.setImage((BufferedImage)viewport2.getNative());
}
void renderScene(){
  pushMatrix();
  box(100);//and other elements here
  popMatrix();
}

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel(BufferedImage img) {
       setImage(img);
    }
    public void setImage(BufferedImage img){
      image = img;
      this.repaint();
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);        
    }

}

There are 3 things to look at:

  1. generating separate views - notice the sketch size is actually double and I use push/pop calls to isolate transforms and render the same data. Feel free to play with the camera() and perspective() functions as well.
  2. generating frames. Notice Processing uses a java.awt.Frame which I resize using frame.setSize() and I extend a JPanel so I can paint into the Graphics object.
  3. updating images: I'm using get(x,y,width,height) to get a snapsshot PImage of the rendered scene at the end of the draw/render loop. PImage provides a getNative() method which returns a BufferedImage representation. This can be painted into a java.awt.Graphics instance. I tell the custom JPanel component to repaint to update the pixels.

This approach is a bit hacky, but you're using the same sketch with the same variables rather than two PApplet instances.



来源:https://stackoverflow.com/questions/17590527/what-processing-library-to-use-to-create-simultaneously-running-renders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!