We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I want to learn to write processing code in other IDEs like Eclipse or Netbeans (thereafter IDE). To learn it I try to work backward by running and study processing application exported java file (let's call it RainGame.java
) inside other IDEs.
I try to run the java file directly inside IDE, it works ( imported core.jar of course); then I cut the following code from RainGame.java and copied it into a new class named Main
, and then run the main method of Main class, it still works:
import processing.core.*;
public class Main extends PApplet{
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "exercise_10_04_improved_rain_game" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
However, when I tried to cut three classes inside RainGame.java
out and copied them into three class files named Catcher
, Drop
, Timer
, and then call main method in Main class again, but they didn't work. The three classes are wrapped as following:
import processing.core.*;
public class Catcher extends PApplet {
....
}
import processing.core.*;
public class Drop extends PApplet {
...
}
import processing.core.*;
public class Timer extends PApplet {
...
}
My Question: why they don't work anymore? I wonder it is due to the mechanism of main method and the use of passedArgs
and appletArgs
, but I have no idea how they work and I could not find anything useful online to figure it out.
Thanks for your help
Answers
What exactly do you mean when you say they don't work? Do you get an error? Unexpected behavior? Something else?
https://forum.Processing.org/two/discussions/tagged?Tag=papplet.main()
https://forum.Processing.org/two/discussions/tagged?Tag=papplet.runsketch()
@KevinWorkman Thanks and here are the errors:
@GoToLoop thanks. Though I found one post which seemed closed related, it looked a little too advanced compared with my problem at hand. So I still couldn't figure out by reading those posts. Would you explain in a simpler way at your convenience? Thanks
I found the API for main method
Now, I guess what troubles me the most is what exactly are the
args
, what they are for, and how to use them in the main method?Thanks
http://processing.GoogleCode.com/svn/trunk/processing/build/javadoc/core/processing/core/PApplet.html#main(java.lang.String, java.lang.String[])
You need to modify the
public void static main
method for each class e.g.The
public void static main
method defines the starting point for program execution. That means you usually have just onemain
method per application.Personally I would avoid calling a class Main as it can cause confusion. ;)
Just an observation: Saying something like
RainGame.class.getName()
is completely verbose & redundant.A simple & direct "RainGame" is the way to go! ;)
And rather than: L-)
This overloaded main() "igniter" is more direct & concise: $-)
You might want to rethink that observation since it is wrong ;)
The OP is asking about writing sketches in Eclipse and Netbeans which both have the facility to refactor-rename classes etc. In my example changing the class name will also change the statement inside the main method. In your example the user would have to remember to change the "RainGame" string.
Verbosity is not always undesirable :)
Here's a nice couple of quotes for @GoToLoop I saw in Haverbeke's Eloquent Javascript; both of which made me think of his coding style :P
This one's good as well:
@blindfish, those quotes you've posted above and that
RainGame.class.getName()
"pattern" got nothing to do w/ ea. other at all!As @quark had already explained, that is an advanced refactoring facility provided by advanced IDEs.
It's got nothing to do w/ clarity nor it is something provided by the Java language itself! 8-|
Typing in "RainGame" is both clearer and faster for the point-of-view of Java language alone! ~O)
Actually for beginners,
RainGame.class.getName()
is a completely obscure statement! 8-}@GoToLoop: Sorry - I did indeed stray well off topic :\"> - but those quotes followed nicely from @quark's "Verbosity is not always undesirable"... O:-)
@quark and @GoToLoop thanks a lot!
I tried the following options: 1. giving each class a
main
method; 2. only give main method toRainGame
class and run from there; however, neither works. Maybe it is due to my misunderstanding of @quark's answer.I have pasted the full code below. The application I call it CatchRainGame which consists of five classes: 1.
Main
class only contains the main method for the application; 2.RainGame
class contains global variables,settings()
,setup()
,draw()
and etc.; 3.Drop
class is responsible for creating rain drop object; 4.Timer
for counting; 5.Catcher
class is for displaying a catcher object to catch rain drop.My intention is to run the application from the
main
method ofMain
class. So, I would imagine there is only one main method as @quark suggested and I want the onlymain method
in a seperated classMain
, away fromRainGame
,Drop
,Timer
,Catcher
classes.What exactly should I do to the codes below? Thanks
The original code was from a book example from Learning Processing by Daniel Shiffman.
Main class code --
RainGame class code --
Drop class code --
Timer class code --
Catcher class code --
@GoToLoop thanks, but when I removed
extends PApplet
from other three classes, they won't recognizewidth
,height
,ellipse()
. what shall I do about this?Also, what do you mean by
PApplet callbacks
? What exactly they are insideRainGame
class?@GoToLoop thanks, I went back to Processing in Eclipse with Multiple Classes and it says how and why need to remove
extends PApplet
.Now, I have corrected my code accordingly, this time there is no error, but I still could not get my canvas and the game. I am not sure where goes wrong.
In the output it says:
The corrected codes are below:
Main class --
RainGame --
Drop --
Timer --
Catcher --
P.S.: Strange that https://Processing.org/tutorials/eclipse/ isn't listed in https://Processing.org/tutorials/ :-$
Me neither! B/c after copy & paste your code in Processing 3, each class in its own ".java" tab, it simply worked. Only small adjustment I had to do was paste MainToRun in the main ".pde" this way instead:
Everything else I didn't have to modify a single dot in order to play the game. <:-P
anyway, I work around this by working into two IDEs at the same time. Thanks everyone!
@quark and @GoToLoop, however, I still wonder as we repeatedly seen
args
:What could
args
orpassedArgs
be?As they are
args[]
orpassedArgs[]
, does it mean there can be multiple strings applied in args or passedArgs? Could you give some simple code examples?Thanks!
From processing API -- we have seen
From the example code above -- we have seen
This is what I've said far way up:
Java's own main() parameter comes from either command line or shortcut arguments.
PApplet.main() in turn has extra features which I've already mentioned as well:
Correct order to fully merge Java's main() + PApplet.main()'s arguments is something like this:
But as I've said, args is almost always empty.
No1 calls Processing sketches from commandline passing args to it! 3:-O
BtW, in case args happen to be passed, and by using the model above, we can see those stored in variable args[] inside sketch: :-bd
But those are just advanced examples. As Processing PApplet.main()'s description says: :D
@GoToLoop
Saving a few key presses is not enough reason to go against good programming practice which recommends that identifiers should have meaningful names. I think it is important that we promote good programming practice on this forum, especially for newbies.
String className = Thread.currentThread().getStackTrace()[1].getClassName();
is so much clearer ;))
@blindfish
Glad you liked the quote
here is two more for you
I liked reading the quotes you posted. Although they were not on target here, but it doesn't take away their truth :)
As Processing programmers we're not used to call its API prefixed w/ anything.
Having to type in something simple like:
textFont(createFont("Arial", 12, true));
asparent.textFont(parent.createFont("Arial", 12, true));
is tedious & tiresome enough! I-)
Establishing some convention to represent the PApplet's reference as 1-character name won't diminish its clarity any more than using i & j as iterators, x & y & z as coordinates, w & h as dimensions: 8-X
p.textFont(p.createFont("Arial", 12, true));
That's just common sense folks! As even p5.js has adopted such convention as well: ;;)
https://GitHub.com/processing/p5.js/wiki/Instantiation-Cases
Of course it isn't! But you were already using a much more verbose, slower and cryptic:
String className = RainGame.class.getName();
for the sake of your IDE's rename/refactor features.I've just gone a little farther and replaced it w/ a snippet not relying on any special IDE for it to work!
That is, no matter which class name that statement is in, it's gonna find that out on its own! \m/
r y c i t a t ;)
Thanks a million! @GoToLoop and @quark