mousePressed from another frame

edited October 2014 in How To...

I create a new class called newFrame. newFrame extends PApplet. When I want to add mousePressed() method to it, I have the warning "Duplicate method mousePressed()". What should I do next?

Answers

  • edited August 2014

    Classes which extend PApplet should be instantiated via undocumented methods:

    • PApplet.main() or
    • PApplet.runSketch()!


    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    static final void main(final String[] args) {
      final String nested, sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      try {
        nested = Class.forName(sketch)
          .getClasses()[0].getName();
      }
    
      catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    
      PApplet.main(nested, args);
      //PApplet.runSketch(append(args, ""), new MyApp());
    }
    
    public static final class MyApp extends PApplet {
      void setup() {
        println("OK!");
        exit();
      }
    
      void draw() {
        background(-1);
      }
    }
    
  • edited August 2014

    A more elaborate example:

    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    // forum.processing.org/two/discussion/6822/
    // mousepressed-from-another-frame
    
    static final void main(final String[] args) {
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      final Class[] nesteds;
      try {
        nesteds = Class.forName(sketch).getClasses();
      }
      catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    
      println(nesteds);
      for ( int i = 0, ii = max(0, nesteds.length-2); i != ii;
        PApplet.main(nesteds[i++].getName(), args) );
    }
    
    public static final class MyApp extends PApplet {
      void setup() {
        size(300, 200, JAVA2D);
        noLoop();
        println("OK!");
      }
    
      void draw() {
        background(-1);
      }
    }
    
    public static final class MyFrame extends PApplet {
      void setup() {
        noLoop();
        println("Frame!");
        //exit();
      }
    
      void draw() {
        background(0);
      }
    }
    
Sign In or Register to comment.