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 & HelpIntegration › Multiple Windows in Eclipse
Page Index Toggle Pages: 1
Multiple Windows in Eclipse (Read 4663 times)
Multiple Windows in Eclipse
May 28th, 2009, 3:06am
 
Hey!

I found this fine piece of code in the forum. In Processing everything works fine, but Eclipse throws los of errors. Anyone knows help??

Cheers

Code:
PFrame f;
secondApplet s;

void setup() {
size(320, 240);
PFrame f = new PFrame();
}

void draw() {
 background(255,0,0);
  fill(0);
  rect(20, 20, 50, 50);
 
  s.background(0);
  s.fill(255);
  s.rect(20,20, 50, 50);
  s.redraw();
}

public class PFrame extends Frame {
   public PFrame() {
 setBounds(100,100,400,300);
 s = new secondApplet();
 add(s);
 s.init();
 show();
   }
}

public class secondApplet extends PApplet {
   public void setup() {
 size(400, 300);
 noLoop();
   }

   public void draw() {
   }
}
Re: Multiple Windows in Eclipse
Reply #1 - May 28th, 2009, 7:15am
 
i modified just a few things and it works fine:
Code:
import java.awt.Frame;

import processing.core.PApplet;

public class TestProcessingApplet extends PApplet {

// PFrame f;
secondApplet s;

public void setup() {
size(320, 240);
frameRate(15);
new PFrame(); // was PFrame f = new PFrame();
}

public void draw() {
background(255, 0, 0);
fill(0);
rect(20, 20, 50, 50 + frameCount);

}

public class PFrame extends Frame {
public PFrame() {
setBounds(300, 300, 400, 300);
s = new secondApplet();
add(s);
s.init();
setVisible(true); // was show();
}
}

public class secondApplet extends PApplet {
public void setup() {
size(400, 300);
frameRate(15);
}

public void draw() {
background(0);
fill(255);
rect(20, 20, 50, 50 - frameCount);
redraw();
}
}
}



save the code as TestProcessingApplet.java and run in eclipse. two windows should show up....

regards,
moony
Re: Multiple Windows in Eclipse
Reply #2 - Oct 15th, 2009, 4:21pm
 
Hi,
thanks for the code: works for me! Smiley

I want to try 2 windows with OPENGL in Eclipse.

When i draw only one window works ok.
But if I draw two windows i get the error:

Exception in thread "Animation Thread" javax.media.opengl.GLException: Can not destroy context while it is current
     at com.sun.opengl.impl.GLContextImpl.destroy(GLContextImpl.java:176)
     at processing.opengl.PGraphicsOpenGL.allocate(PGraphicsOpenGL.java:201)
     at processing.core.PGraphics3D.setSize(PGraphics3D.java:316)
     at processing.core.PApplet.resizeRenderer(PApplet.java:930)
     at processing.core.PApplet.size(PApplet.java:992)
     at processing.core.PApplet.size(PApplet.java:958)
     at TestProcessingApplet$secondApplet.setup(TestProcessingApplet.java:37)

Can you help me?

Thanks!

Re: Multiple Windows in Eclipse
Reply #3 - Oct 16th, 2009, 6:43am
 
tschiggi wrote on May 28th, 2009, 3:06am:
In Processing everything works fine, but Eclipse throws los of errors.

See also Processing in Eclipse.
Re: Multiple Windows in Eclipse
Reply #4 - Jan 22nd, 2010, 11:48am
 
lot wrote on Oct 15th, 2009, 4:21pm:
if I draw two windows i get the error: <snip>


same here. Have you been able to solve this
Re: Multiple Windows in Eclipse
Reply #5 - Jan 22nd, 2010, 12:35pm
 
Eclipse launches the Applet Viewer to display the applet and when creating a second applet it could be it can't launch another Applet Viewer instance. In fact the error messages seem to indicate that the Applet Viewer is trying to start the second applet in the same instance which it can't do.

One solution would be not to create a second applet but to create a Java Frame and embed a PApplet into it. This is the approach I took with the G4P library

This link shows an example of creating multiple windows.
Simple Windows with G4P when you click on the button in this example it will open 3 separate windows, true these are all JAVA2D but the code below will do the same thing but the Applet and 3 windows are all using OPENGL.

To run in Eclipse you would need to download the library and change the build path to add the guicomponents.jar file as an external library.

Code:
import java.awt.event.MouseEvent;
import processing.core.PApplet;


public class WindowStarter extends PApplet{

private GWindow[] window;

private GButton btnStart;
private GLabel lblInstr;

public void setup(){
 size(600,50, OPENGL);
 btnStart = new GButton(this, "CreateWindows", 10,10,80,30);
 lblInstr = new GLabel(this,"Use the mouse to draw a rectangle in any of the 3 windows",100,10,490);
 lblInstr.setVisible(false);
}

public void createWindows(){
 int col;
 window = new GWindow[3];
 for(int i = 0; i < 3; i++){
   col = (128 << (i * 8)) | 0xff000000;
   window[i] = new GWindow(this, "Window "+i, 130+i*210, 100+i*100,200,200,false, OPENGL);
   window[i].setBackground(col);
   window[i].addData(new MyWinData());
   window[i].addDrawHandler(this, "windowDraw");
   window[i].addMouseHandler(this, "windowMouse");
 }
}

/**
* Click the button to create the windows.
* @param button
*/
public void handleButtonEvents(GButton button){
 if(window == null && button.eventType == GButton.CLICKED){
   createWindows();
   lblInstr.setVisible(true);
 }
}

/**
* Draw for the main window
*/
public void draw(){
 background(192);
}

/**
* Handles mouse events for ALL GWindow objects
*  
* @param appc the PApplet object embeded into the frame
* @param data the data for the GWindow being used
* @param event the mouse event
*/
public void windowMouse(GWinApplet appc, GWinData data, MouseEvent event){
 MyWinData data2 = (MyWinData)data;
 switch(event.getID()){
 case MouseEvent.MOUSE_PRESSED:
   data2.sx = data2.ex = appc.mouseX;
   data2.sy = data2.ey = appc.mouseY;
   data2.done = false;
   break;
 case MouseEvent.MOUSE_RELEASED:
   data2.ex = appc.mouseX;
   data2.ey = appc.mouseY;
   data2.done = true;
   break;
 case MouseEvent.MOUSE_DRAGGED:
   data2.ex = appc.mouseX;
   data2.ey = appc.mouseY;
   break;
 }
}

/**
* Handles drawing to the windows PApplet area
*
* @param appc the PApplet object embeded into the frame
* @param data the data for the GWindow being used
*/
public void windowDraw(GWinApplet appc, GWinData data){
 MyWinData data2 = (MyWinData)data;
 if(!(data2.sx == data2.ex && data2.ey == data2.ey)){
   appc.stroke(255);
   appc.strokeWeight(2);
   appc.noFill();
   if(data2.done){
     appc.fill(128);
   }
   appc.rectMode(CORNERS);
   appc.rect(data2.sx, data2.sy, data2.ex, data2.ey);
 }
}

/**
* Simple class that extends GWinData and holds the data that is specific
* to a particular window.
*
* @author Peter Lager
*
*/
class MyWinData extends GWinData {
 public int sx,sy,ex,ey;
 public boolean done;
}
}

Re: Multiple Windows in Eclipse
Reply #6 - Jan 23rd, 2010, 4:17am
 
thanks Peter, your method works. Although i'm not sure if i understand how it works structurally (new to Java and Eclipse). Could you explain this handler/GWinData business a little?
Re: Multiple Windows in Eclipse
Reply #7 - Jan 23rd, 2010, 7:09am
 
To handle multiple windows the G4P has 3 main classes the first 2 are

GWindow this class inherits from (extends) the java.awt.Frame class.
GWinApplet this class inherits from PApplet so provides all the features that we would expect from a Processing sketch.

When a GWindow is created a GWinApplet object is also created and embedded into the GWindow object. (Note the user would not normally access the GWinApplet object directly but use methods available in GWindow).

Okay so now we have a sketch that has created a separate window which is basically a second Processing sketch except how can we use the second window because all the draw and event handling is in the GWinApplet object and we can't simply add our own draw method etc.

The solution is to create methods in the main sketch that will be called from the GWinApplet object and this is show in the following code (taken from example)
Code:
window[i].addDrawHandler(this, "windowDraw");
window[i].addMouseHandler(this, "windowMouse");

This code tells the GWindow object the name of the method to be used. So in this case it will look for a method called windowDraw that has 2 parameters of type GWinApplet and GWindata e.g.
Code:
public void windowDraw(GWinApplet appc, GWinData data){
  ...
}

The appc parameter is a reference to the embedded GWinApplet object so can be used to access all the methods available in PApplet e.g. stroke, fill, color etc.

So now we come to the GWinData class but just before that I want to mention a couple of things.
(1) Since our 'windowDraw' method is in the main sketch it can access all the attributes (variables) in the main sketch.
(2) in the example code the same draw method has been used for all 3 windows, you might want a different method for each window this can be done e.g.
Code:
window[0].addDrawHandler(this, "windowDraw0");
window[1].addDrawHandler(this, "windowDraw1");
window[2].addDrawHandler(this, "windowDraw2");

So back to GWinData, in this example we have three windows using the same method to do essentially the same thing i.e. draw a rectangle BUT it is drawing a different rectangle for each window. I could create 3 sets of variables in the main applet - not too bad for three windows but what if you had 10! In fact you might look at the Mandelbrot example where we can have dozens of windows this is not a good place to store the windows data.

GWinData provides a mechanism for creating objects that can hold data required by a particular GWindow object. To use it you must create your own class that inherits from it i.e.
Code:
class MyWinData extends GWinData { 


In this class you can put any attributes and methods for processing them that you like. Then you create objects of this class and add them to the window e.g.
Code:
window[i].addData(new MyWinData()); 


The only restriction is that in the handler methods since it is provides a reference to a GWinData object we need to cast it to our class type so we can access the attributes and methods e.g.
Code:
public void windowDraw(GWinApplet appc, GWinData data){
   MyWinData data2 = (MyWinData)data;


Hope this helps.
Smiley
Re: Multiple Windows in Eclipse
Reply #8 - Jan 26th, 2010, 5:07pm
 
it does indeed help, thanks. Though this whole handler business is a bit above my head in general still…  Cheesy
Page Index Toggle Pages: 1