We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Combining multiple sketches
Page Index Toggle Pages: 1
Combining multiple sketches (Read 796 times)
Combining multiple sketches
May 30th, 2007, 12:42pm
 
Hi!

Is it possible, from within processing, to dynamically load and combine multiple sketches?

Say for example that we have two processing sketches, coolSketchOne.jar, and coolSketchTwo.jar: can these, during runtime, be loaded from within a third processing sketch, and their output be blended?

Ideally I would like the background color in each to be disregarded, so that sketches may be layered on top of each other.

Thanks!

Ilias B.
Re: Combining multiple sketches
Reply #1 - May 31st, 2007, 4:41am
 
Hey Ilias,

I imagine you've used flash before and nested movie clips within each other. There isn't an identical structure in Processing that I'm aware of, but combining different sketches is as easy as separating their functionality out into different classes. You can then add instances of each class to your processing sketch and combine many, many different "sketches." It's not the same as loading a movie clip, but once you get a hang of the concept, you'll likely find it just as easy to do. Also, classes behave in a much more predictable fashion than nested movie clips. Best of luck.

The following example uses two classes within a single sketch, one is a springy square, the other an easing circle:

//make variables for the "sketches" we want to use
//these "sketches" are actually just classes defined later in the code
//I'd recommend using tabs to organize the different classes

coolSketchOne sketch;
coolSketchTwo sketch2;

void setup(){
 //setup works as usual
 size(400,400);
 background(255);
 //instatiate our classes
 sketch = new coolSketchOne(width, height);
 sketch2 = new coolSketchTwo(width, height);
}

void draw(){
 background(255);
 //call the happen function (also called a method) within each sketch
 sketch.happen();
 sketch2.happen();
}

void mousePressed(){
 //call the reset function within each sketch
 sketch.reset();
 sketch2.reset();
}

//here is where the different drawing behaviors are defined
//each class contains a set of behaviors for itself

public class coolSketchOne{
private int w, h;
private float x, y;
private float vx, vy;


coolSketchOne(int w, int h){
  this.w = w;
  this.h = h;
}

public void move(){
  //easing motion
  vx = (mouseX - x)/6;
  vy = (mouseY - y)/6;
  x += vx;
  y += vy;
}

public void render(){
  noFill();
  ellipse(x, y, 4, 4);
}

public void happen(){
  move();
  render();
}

public void reset(){
  //move to bottom of screen
  x = mouseX;
  y = height;
}
 
}

public class coolSketchTwo{
private int w, h;
private float x, y;
private float vx, vy;
private float ax, ay;


coolSketchTwo(int w, int h){
  this.w = w;
  this.h = h;
}

public void move(){
  //springing motion
  ax = (mouseX - x)/32;
  ay = (mouseY - y)/32;
  vx += ax;
  vy += ay;
  x += vx;
  y += vy;
  vx *= .9;
  vy *= .9;
}

public void render(){
  noFill();
  rect(x-4, y-4, 8, 8);
}

public void happen(){
  move();
  render();
}

public void reset(){
  //move to a random position
  x = random(w);
  y = random(h);
}
 
}
Re: Combining multiple sketches
Reply #2 - May 31st, 2007, 1:53pm
 
Thank you for the example!

Although a software engineer with quite a few years of experience, I'm new to Java, and I'm only learning it now because of it having been used to develop processing.

I see Processing as a fantastic initiative, but the only way it could be truly usable in a performance setting would be if you could combine the various sketches, and even postpone the decision about which to load to as late as possible, even so that ideally the code for each sketch would be loaded dynamically during performance, much in the same way that a VJ can load clips from a library.

I would love to develop an application that allows this, but as I will have to learn quite a bit about Java before I can implement anything, I thought it'd be wise to ask around a bit about whether it would be worth my while, or if I would reach an obstacle further in the future that may well tell me this idea is impossible to actually implement.

In short though, you've helped me a great deal along the way, thank you Smiley
Re: Combining multiple sketches
Reply #3 - Jun 1st, 2007, 5:39pm
 
Glad I could be of help. Someone out there probably knows quite a bit more about dynamically loading data into processing. I know you can load xml data, so it shouldn't be too hard to combine that ability with user input to tell a browser to launch another processing applet (just a simple link). Getting the two applets to play together, however, is something I'm not sure about.
Page Index Toggle Pages: 1