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 & HelpProcessing Implementations › G4P Standalone JRuby/Java Application
Page Index Toggle Pages: 1
G4P Standalone JRuby/Java Application (Read 3015 times)
G4P Standalone JRuby/Java Application
Mar 30th, 2010, 5:22am
 
Hi,
Trying to get my head around using processing in a standalone app, in my case using jRuby.

module Processing
 include_package  'processing.core'
end
module Gui
 include_package 'guicomponents'
end

main_applet = Processing::PApplet.new
gwindow = Gui::GWindow.new(main_applet,'window',200,200,400,200, false, Processing::PApplet::JAVA2D)

creates a window with embedded applet OK

A few things I do not understand yet
In the GWindow constructor what is 'theApplet' parameter for as the frame has a PApplet embedded in it's constructor?
How should a component be added to the window?

All examples for processing are pde's.
How are these incorporated in a self standing application?

Sample code of a simplistic standalone java app with a frame and a button would probably answer all my questions as conversion to JRuby is fairly straight forward.

Thanks
Paul F Fraser

Re: G4P Standalone JRuby/Java Application
Reply #1 - Apr 19th, 2010, 12:01pm
 
Quote:
In the GWindow constructor what is 'theApplet' parameter for as the frame has a PApplet embedded in it's constructor


True this parameter was included to make the constructor consistent with the creation of the other GUI components where the first parameter is a refernce to the main PApplet object. Although it is not used in GWindow I kept it for possible future enhancements.

Quote:
How should a component be added to the window


The following code is taken from the showcase example. It first creates a GWindow then 2 sliders which are then added to the new window.

Moreinfo on GWinData and event handling can be found here.
http://www.lagers.org.uk/g4p/applets/g4p_windowsstarter/index.html

Code:
public void createControlWindow(){
 windControl = new GWindow(this, "Controls",600,400,width/4 + 16,height/4 + 16, false, null);
 sdrHorzPos = new GHorzSlider(this,0,height/4,width/4,16);
 sdrHorzPos.setLimits(pX, 0 ,width - pWidth);
 sdrVertPos = new GVertSlider(this,width/4,0,16,height/4);
 sdrVertPos.setLimits(pY, pnlControls.getTabHeight(), height - pHeight);

 windControl.add(sdrHorzPos);
 windControl.add(sdrVertPos);
 windControl.addData(new GWinData());

 windControl.addDrawHandler(this, "drawController");
 windControl.setExitBehaviour(GWindow.SHUTDOWN_ON_EXIT);
}

Re: G4P Standalone JRuby/Java Application
Reply #2 - Apr 21st, 2010, 6:37am
 
Thanks Quark,
In the sample code you have provided, how would a handleButtonEvents method be added.
I am working in JRuby and I have no problem adding a button to the GWindow, but getting the button hander to work has so far eluded me.
Paul
Re: G4P Standalone JRuby/Java Application
Reply #3 - Apr 21st, 2010, 8:17am
 
Since the GWindow applet is basically in a closed class it is not possible to access the normal PApplet event loop.

In G4P when a button is created it will look for a method in the main applet with the following signature
Code:
public void handleButtonEvents(GButton button) 



Assuming you have a number of buttons then you need to check to see which button was used and the event type e.g.

Code:
public void handleButtonEvents(GButton button) {
if(button == btnUp && button.eventType == GButton.CLICKED)
txfML2.scroll(GTextField.SCROLL_UP);
if(button == btnDown && button.eventType == GButton.CLICKED)
txfML2.scroll(GTextField.SCROLL_DOWN);
if(button == btnRight && button.eventType == GButton.CLICKED)
txfML2.scroll(GTextField.SCROLL_RIGHT);
if(button == btnLeft && button.eventType == GButton.CLICKED)
txfML2.scroll(GTextField.SCROLL_LEFT);
}


This will work for all buttons even if they have been added to another GWindow.

It is also possible for each GUI component (or group of components) to have their own personalised event handler.

All GUI components have a method that can be used to specify their event handler, for instance assume we have a button called btnUp and we want to add a personalised event handler. We can use the statement

Code:
btnUp.addEventHandler(this, "handleUpButtonEvents");


This assumes that you have created the following method
Code:
public void handleUpButtonEvents(GButton button){

}

Notice the parameter. All event handlers are expected to have a single parameter of the same type as the GUI component. This is the component that generates the event. In this case btnUp.

Hope this answers your question.
Re: G4P Standalone JRuby/Java Application
Reply #4 - Apr 21st, 2010, 11:01am
 
I forgot to mention...

It you are creating the Program inside the Processing IDE and the event handlers don't appear to work remove the public keyword from in front of the method header so
Code:
public void handleButtonEvents(GButton button) { 


becomes
Code:
void handleButtonEvents(GButton button) { 


I use Eclipse for most of my programming and in Eclipse methods require an access modifier whereas Processing doesn't.
Smiley
Page Index Toggle Pages: 1