Processing IDE and java vs Eclipse or Netbeans

PerPer
edited September 2016 in Using Processing

Hi!

I have been using processing for many years and are now starting learn more java. In my course we are using NetBeans. I wounder are there any advantages of starting to code processing sketches in NetBeans or Eclipse instead of Processing. For example if I want to integrate a processing sketch in a pure java sketch.

What advantages could there be of switching the IDE from Processing to NetBeans or Eclipse?


I have integrated processing into netbeans by using these following steps. Posting it here if it could help anyone.

1) Create new java project
2) Copy the files in ”contents/Resources/Java/core/library” to a new folder called for example ”_ProcessingLibraries”
3) Right click ”Libraries” in your project in NetBeans and choose ”Add folder/library” and select and add all the jarfiles
4) Create a new java class and call it for example ”MySketch”
5) Write in MySketch class:

package pkg6_processing;

import processing.core.*;  // import the processing core

public class MySketch extends PApplet {  // extend the PApplet for inherit all

    @ Override
    public void settings() {
        size(300, 300);
    }

    @ Override
    public void setup() {
        fill(120, 50, 240);
    }

    @ Override
    public void draw() {
        ellipse(mouseX, mouseY, 20, 20);
    }
}

6) Run the PApplet in the main class

package pkg6_processing;

public class Main {

    public static void main(String[] args) {
        MySketch.main("pkg6_processing.MySketch"); // run the applet in main
    }
}

Comments

  • edited September 2016

    What advantages could there be of switching the IDE from Processing to NetBeans or Eclipse?

    There are many more features available in Eclipse and Netbeans which you will discover as you use these IDEs more. One of my favourite features is rafactoring identifiers for instance renaming a class, method or attribute can be done in a single step.

    Personally I find it much faster to develop software using Eclipse than Processing.

  • PerPer
    edited September 2016

    Thanks for reply!

    When you are using eclipse or netbeans are you integrating processing the same way i am doing above or do you have another, in your opinion better way of doing it. I got some problems using classes by using this code:

    Main.java

    package pkg6_processing;
    
    public class Main {
    
        public static void main(String[] args) {
            MySketch.main("pkg6_processing.MySketch");
        }
    }
    

    MySketch.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class MySketch extends PApplet {  // extend the PApplet for inherit all
    
        Dot dots;
    
        @ Override
        public void settings() {
            size(300, 300);
        }
    
        @ Override
        public void setup() {
            dots = new Dot(width/2, height/2);
        }
    
        @ Override
        public void draw() {
            background(255);
            dots.display();
            dots.move();
        }
    }
    

    Dot.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class Dot extends PApplet {
    
      int x;
      int y;
    
      Dot(int x_, int y_) {
        x = x_;
        y = y_;
      }
    
      public void display() { 
        noFill();
        stroke(255, 0, 0);
        ellipse(x, y, 10, 10);
      }
    
      public void move() {
        x = mouseX;
        y = mouseY;
      }
    }
    

    I followed and tried to implement the instructions for eclipse (but I am using Netbeans): https://processing.org/tutorials/eclipse/

    And the thread you are also in here: https://forum.processing.org/two/discussion/13468/how-to-understand-processing-main-method-in-other-ide-given-multi-classes-defined-inside

    But I cannot get the above code to work

  • I don't think Dot should extend papplet, only the MySketch class should. You will need to pass the main papplet around in order to get at the methods.

  • ^ read the "Processing in Eclipse with Multiple Classes" section from the eclipse tutorial link you posted

  • Generally only one class in a sketch will extend PApplet and that class will be the sketch window.

    The Dot class can be written in two ways

    Version 1

    public class Dot{
      PApplet.app; 
      int x;
      int y;
    
      Dot(PApplet papp, int x_, int y_) {
        app = papp;
        x = x_;
        y = y_;
      }
    
      public void display() { 
        app.noFill();
        app.stroke(255, 0, 0);
        app.ellipse(x, y, 10, 10);
      }
    
      public void move() {
        x = app.mouseX;
        y = app.mouseY;
      }
    }
    

    and in the MySketch class change line 16 to
    dots = new Dot(this, width/2, height/2);

    Version 2

    public class Dot {
      int x;
      int y;
    
      Dot(int x_, int y_) {
        x = x_;
        y = y_;
      }
    
      public void display(PApplet app) { 
        app.noFill();
        app.stroke(255, 0, 0);
        app.ellipse(x, y, 10, 10);
      }
    
      public void move(PApplet app) {
        x = app.mouseX;
        y = app.mouseY;
      }
    }
    

    and in the MySketch class change lines 22 & 23 to
    dots.display(this);
    dots.move(this);

  • PerPer
    edited September 2016

    Thanks both of you once again but it didnt work. Now I passed the PApplet in the two ways you suggested @quark and wrote:

    Version 1:
    Main.java

    package pkg6_processing;
    
    public class Main {
    
        public static void main(String[] args) {
            MySketch.main("pkg6_processing.MySketch");
        }
    }
    

    MySketch.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class MySketch extends PApplet {  // extend the PApplet for inherit all
    
        Dot dots;
    
        @ Override
        public void settings() {
            size(300, 300);
        }
    
        @ Override
        public void setup() {
            dots = new Dot(this, width / 2, height / 2);
        }
    
        @ Override
        public void draw() {
            background(255);
            dots.display();
            dots.move();
        }
    }
    

    Dot.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class Dot {
        PApplet.app;
        int x;
        int y;
    
        Dot(PApplet papp, int x_, int y_) {
            app = papp;
            x = x_;
            y = y_;
        }
    
        public void display() {
            app.noFill();
            app.stroke(255, 0, 0);
            app.ellipse(x, y, 10, 10);
        }
    
        public void move() {
            x = app.mouseX;
            y = app.mouseY;
        }
    }
    

    Got this colored in red: Namnlöst

    Version 2:
    Main.java

    package pkg6_processing;
    
    public class Main {
    
        public static void main(String[] args) {
            MySketch.main("pkg6_processing.MySketch");
        }
    }
    

    MySketch.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class MySketch extends PApplet {  // extend the PApplet for inherit all
    
        Dot dots;
    
        @ Override
        public void settings() {
            size(300, 300);
        }
    
        @ Override
        public void setup() {
            dots = new Dot(width/2, height/2);
        }
    
        @ Override
        public void draw() {
            background(255);
            dots.display(this);
            dots.move(this);
        }
    }
    

    Dot.java

    package pkg6_processing;
    
    import processing.core.*;  // import the processing core
    
    public class Dot {
    
        int x;
        int y;
    
        Dot(int x_, int y_) {
            x = x_;
            y = y_;
        }
    
        public void display(PApplet app) {
            app.noFill();
            app.stroke(255, 0, 0);
            app.ellipse(x, y, 10, 10);
        }
    
        public void move(PApplet app) {
            x = app.mouseX;
            y = app.mouseY;
        }
    }
    

    with this errormessage:

    Exception in thread "main" java.lang.ClassFormatError: Invalid superclass index 0 in class file pkg6_processing/Main at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:455) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:367) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495) /Users/Per/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

  • In the Dot class line 11 should be

    PApplet app;

    Was typo in my last post

  • PerPer
    edited September 2016

    Of course. I should have noticed! Now it works! Thanks a lot!!

    Finally I wounder, I saw an example using this instead PApplet.main(new String[]{MySketch.class.getName()});

    instead of this: MySketch.main("pkg6_processing.MySketch"); putting this in the main.java

    What does that do? I guess it creates string for the class to run?

    But to use this I need to import processing.core also in the main so I stick to the otherone. Just for learning

    Seem to need a little bit more code in NetBeans compared to Processing but maybe thats worth the effort :) if its a better IDE

  • If you have a choice the first option is best here is an example of what I have use in Eclipse

    public class ElilipseIntersection extends PApplet {
    
         public static void main(String[] args) {
            PApplet.main(new String[] { ElilipseIntersection.class.getName() } );
        }
    
        public void settings(){
            size(400, 400);
        }
        ...
    

    This is far superior to the second option because it allows you to rename the class using refactoring. I

    In this statement

    MySketch.main("pkg6_processing.MySketch");

    you could still use refactoring and it would only change the first MySketchbut it would not change the second one because it is in a String. You have to remember to do that yourself.

    There are other issues with that statement. The first part of the string pkg6_processing is the name of the package which has the class MySketchI which suggests static void main is in a different package to MySketch. Most unusual since it would normally be inside the MySketch class. I am surprised there is also no warning message because it is calling a static method. Iwould have expected to see

    PApplet.main("MySketch");

  • edited September 2016

    For many years, even from the previous old forum, my favorite template has always been kinda this:

    public static final void main(final String[] args) {
      String sketch = Thread.currentThread().getStackTrace()[1].getClassName();
      String[] sketchArgs = { sketch };
      PApplet.main( concat(sketchArgs, args) );
    }
    

    That's specially useful for the PDE b/c we don't clearly spot the name of the wrapper PApplet class (which is the name of the 1st ".pde" tab file btW; also the folder's name!).

    Although not so "crucial", that's a very lazy way to ignite PApplet classes for all IDEs, b/c we don't need to care about the class' name ever! It's totally generic! \m/

  • PerPer
    edited September 2016

    THANKS A LOT!

    FInal question. If Im creating a GUI for my sketch in a JFrame using swing and for example are passing a global variable for background color by using for example a slider in a JFrame form with the main method like this. I experience huge lag when I am moving the slider (the frameRate in the processing sketch is great)

    public class NewJFrame extends javax.swing.JFrame {
    
        public NewJFrame() {
            initComponents();
        }
    
        @ SuppressWarnings("unchecked")
      // HERE IS GENERATED CODE WITH SWING 
     // (I cut it out for the moment because it was huge)
    
     // GLOBAL VARIABLE FOR COLOR 
        public static int col;
    
        private void jSlider1MouseMoved(java.awt.event.MouseEvent evt) {                                    
            col = jSlider1.getValue();
        }                                   
    
        public static void main(String args[]) {
     // HERE IS GENERATED CODE WITH SWING 
     //  (I cut it out for the moment because it was huge)                    
    
            PApplet.main(new String[]{MySketch.class.getName()});
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JPanel jPanel1;
        private javax.swing.JSlider jSlider1;
        // End of variables declaration                   
    }
    

    And are getting the value in the MySketch class:

        @ Override
        public void draw() {
            background(NewJFrame.col);
            dots.display();
            dots.move();
        }
    

    Is this not a good method of controlling my processing sketch with an GUI made in swing? I experience no lag at all when i am using controlP5 inside processing.

Sign In or Register to comment.