0

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.

Dave
  • 127
  • 1
  • 7

2 Answers2

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);
}
//...//
user2468700
  • 964
  • 7
  • 10
  • Oh thanks. But I had one doubt about having common variables.I want some kind of variable which can be accessed in both the draw functions. But when I try doing that what happens is both the renders happen in the same window and with visual results. – Dave Jul 11 '13 at 11:32
  • 1
    You manage both windows in one draw method (see the edited answer). It's not the most efficient way but, as of yet, I don't know better. An actual Java programmer could give you a more detailed solution. – user2468700 Jul 11 '13 at 12:33
1

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.

George Profenza
  • 50,687
  • 19
  • 144
  • 218