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 › P5 in Eclipse, using the PApplet as an GUI object
Page Index Toggle Pages: 1
P5 in Eclipse, using the PApplet as an GUI object (Read 1604 times)
P5 in Eclipse, using the PApplet as an GUI object
Apr 4th, 2010, 3:49pm
 
(I'm pretty advanced in Java and eclipse. I could compile my own p5 core.jar if needed, but I think overriding should have to do.)

So I want to use a PApplet merely as a object for a GUI. Not as an applet, but really in a window (presentation mode).
This is because I think running all your code from in the draw() method is a bit dirty, and because I think it's more intuitive otherwise.

If there is a completely other way of doing what I want to do, don't hesitate to tell me.

This is an example that would make clear what I want. but it doesn't work for reasons I don't know. I don't get any errors though.
I used the Stripe example (with classes) given in the Processing in Eclipse page to illustrate my problem.
processing.org/learning/tutorials/eclipse/
The whole Stripe class is unchanged, it can be seen there, i will not rewrite it.

Main.java
Code:

//the main method
public class Main {
public static void main(String args[]) {
new Run();
}
}


Run.java
Code:

import processing.core.PApplet;

public class Run {
Stripe[] stripes = new Stripe[50]; //array of 50 strypes.
GUI gui = new GUI(); //GUI is a class that extends PApplet.

public Run(){
//setup
PApplet.main(new String[] { "GUI" }); //this inits the PApplet, it also runs the setup() method i presume.
gui.init(); //it could be that the setup would be run from here. but anyways, i've put noLoop() in setup() as you can see further. Because i think this is needed.

//construct all the stripes, the Stripes method is unchanged.
for (int i = 0; i < stripes.length; i++) {
stripes[i] = new Stripe(gui); //I give the reference of the GUI to Stripe, so it knows where it should draw.
}

//this would be a first drawing loop, but leaveable and more in other cases. unlike the draw loop
while(true){
//actually, it allready fails here. the function does get called though. (if i'd be printing the gui.random(20) to the console, it gives a stream of random numbers.)
gui.background(gui.random(20));

//this is more stuff that happens in the demo. it fails, but it's the same problem as above.
//as mentioned, the Stripe class is unchanged.
for (int i = 0; i < stripes.length; i++) {
stripes[i].move();
stripes[i].display();
}

//i guess something other would be needed here, but i tried a lot here.
//my biggest insight was that i would be able to make a PGraphics buffer to draw to and draw it to the screen in the draw() method using image(buffer, 0, 0), but it seems i can't even draw stuff in a PGraphics thingy outside the draw() method.
gui.redraw();
}
}
}


GUI.java
Code:

import processing.core.*;

public class GUI extends PApplet {
public void setup() {
size(300,200, P2D);
noLoop(); //i think this is needed.
}

//override the draw() method with an empty one because not doing so would terminate the program as you can see in the source of PApplet.java.
public void draw() {
}
}
Re: P5 in Eclipse, using the PApplet as an GUI object
Reply #1 - Apr 5th, 2010, 1:41am
 
I don't see how you use your Run class in your GUI (Swing, I presume).
But if you can't embed the PApplet directly, perhaps you can hide the sketch's window, draw on a PGraphics and use the Graphics2D to draw on your own frame, in a paint method.
A bit convoluted, I reckon...
Re: P5 in Eclipse, using the PApplet as an GUI object
Reply #2 - Apr 5th, 2010, 6:32am
 
PhiLho  wrote on Apr 5th, 2010, 1:41am:
I don't see how you use your Run class in your GUI (Swing, I presume).
But if you can't embed the PApplet directly, perhaps you can hide the sketch's window, draw on a PGraphics and use the Graphics2D to draw on your own frame, in a paint method.
A bit convoluted, I reckon...

i don't think that that would work. because then drawing stuff to a PGraphics in Run, and drawing it to the PApplet with GUI.draw(){image(buffer, 0, 0);} should have to work also. i don't think anything can get drawn on a PGraphics outside GUI.draw()...

i think the solution would be to turn of some safety, overriding a method...

besides, your method wouldn't allow me to use GUI.mouseX and stuff. :/

but thx for the reply, anything is welcome ;)
Page Index Toggle Pages: 1