Connecting an ordinary Java class and Processing class

So I code something in Java Mode using Eclipse. I have a Processing class
public class Pikachu extends PApplet
and an ordinary Java class
public class Jojo
They're in separated file, because it's too complicated and messy to put them in one file.
And, the public static void main is with Jojo.

I wanna call Pikachu using Jojo, and also add some params, but I'm confused on how to do that.

Let's say Pikachu is just ordinary Java class like Jojo.
Then, when I wanna run/call Pikachu from Jojo, I just need to make the object, like,
Pikachu pika = new Pikachu(param1, param2);
How to do this with Processing class? I tried some things, but just lead me to error (like calling it like usual Java class, or even make constructor...)

If I have a mistake, or there's any lack information, or you have a link that may help me, I will be really glad if you'd like to tell me.

Some details:
Machine: Win8 x64
Processing: 2.1.2
Eclipse: Eclipse IDE for Java Developers

Tagged:

Answers

  • "I tried some things, but just lead me to error"
    Might be interesting to see the attempts, and the error messages...

  • edited May 2014 Answer ✓

    Processing in short is a framework API which runs under a Thread called "Animation Thread".
    A PApplet's instance has a JFrame window + a PGraphics canvas too!

    I believe it'd be much easier if you have a separate Overseer class, which implements PApplet, for your other classes!
    Here's an initial template for it:

    /**
     * PApplet Instantiation (v1.1)
     * by GoToLoop (2014/May)
     *
     * forum.processing.org/two/discussion/5145/
     * connecting-an-ordinary-java-class-and-processing-class
     */
    
    import processing.core.*;
    import processing.data.*;
    import processing.event.*;
    import processing.opengl.*;
    
    import java.util.HashMap;
    import java.util.ArrayList;
    
    import java.io.File;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.IOException;
    
    public class Overseer extends PApplet {
    
      public static final void main(String[] args) {
        final String sketch = Thread.currentThread()
          .getStackTrace()[1].getClassName();
    
        PApplet.main(append(args, sketch));
      }
    
      final Jojo jojo = new Jojo(this);
      final Pikachu pikachu = new Pikachu(this, "param1", "param2");
    
      {
        jojo.setPikachu(pikachu);
        pikachu.setJojo(jojo);
      }
    
      void setup() {
        size(800, 600, JAVA2D);
        smooth(4);
        frameRate(60);
      }
    
      void draw() {
        background(0);
      }
    
      void keyPressed() {
      }
    
      void keyReleased() {
      }
    
      void mousePressed() {
      }
    }
    

    You just need to pass the Overseer instance reference to any class which wants to interact w/ Processing's canvas!

    P.S.: I don't use Eclipse! @_@

  • @PhiLho
    Thank you very much for your reply! :)
    Well I've said it...

    (like calling it like usual Java class, or even make constructor...)

    Buuut dafuq me ( ._.) when I tried this, it worked...

    //Constructor for Pikachu
    public Pikachu() {
        this.init();
    }
    

    and when I called it in Jojo, it works

    Pikachu pika = new Pikachu();
    

    Instantly, Pikachu is executed just like usual Processing.

    @GoToLoop
    Thank you very much for your reply! :)
    Sorry, so sorry I'm so newbie ( T_T) I'm afraid I don't understand this part:

    You just need to pass the Overseer instance reference to any class which wants to interact w/ Processing's canvas!

    I'm very interested with that implementation, because it seems tidier and can control both class... What if the params I wanna pass is something produced by Jojo? How to do that?

    Ah, don't worry about Eclipse. I use IDE so that I don't have to blahblahblah the libs when compiling and running, and since I use many components I just have known, it would immediately tell me when I type something irrelevant.

  • "when I tried this, it worked"
    That's why I asked, I didn't see problems with the process you described, hence my question... :-)

  • Wait wait now I understand GoToLoop answers... Thank you very much!

    @PhilHo Hoo I see I see! Thank you very much! :D

  • edited May 2014

    Just as an addendum, some class templates for Jojo & Pikachu!
    That's in case they need to access Processing's API & each other of course! *-:)



    import processing.core.PApplet;
    
    public class Jojo {
      final PApplet p;
    
      Pikachu pika;
    
      public Jojo(PApplet sketch) {
        p = sketch;
      }
    
      public void setPikachu(Pikachu chu) {
        pika = chu;
      }
    }
    

    import processing.core.PApplet;
    
    public class Pikachu {
      final PApplet p;
      final String pam1, pam2;
    
      Jojo jo;
    
      public Pikachu(PApplet sketch, String arg1, String arg2) {
        p = sketch;
    
        pam1 = arg1;
        pam2 = arg2;
      }
    
      public void setJojo(Jojo j) {
        jo = j;
      }
    }
    

Sign In or Register to comment.