Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

306
LINES

< > BotCompany Repo | #1014279 // pMain2 - start Processing applet, retain Frame and instance [dev.]

JavaX fragment (include)

set flag AllPublic.

lib 1013739

import processing.core.*;

static <A extends PApplet2> Pair<A, Frame> pMain2(final Class<A> c) {
  ret swing(func -> Pair<A, Frame> {
    A a = nuInstance(c);
    Frame f = pMain2_runSketch(null, a);
    clearThreadLocal(PApplet2_instancesMade);
    ret pair(a, f);
  });
}

static PApplet2 pMain2() {
  ret pMain2(mc());
}

static Frame pMain2_runSketch(final String[] args,
                               final PApplet constructedSketch) {
  // Supposed to help with flicker, but no effect on OS X.
  // TODO IIRC this helped on Windows, but need to double check.
  System.setProperty("sun.awt.noerasebackground", "true");

  // Remove 60fps limit on the JavaFX "pulse" timer
  System.setProperty("javafx.animation.fullspeed", "true");

  // Doesn't seem to do anything helpful here (that can't be done via Runner)
  //System.setProperty("com.apple.mrj.application.apple.menu.about.name", "potato");

  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
      e.printStackTrace();
      uncaughtThrowable = e;
    }
  });

  // This doesn't work, need to mess with Info.plist instead
  /*
  // In an exported application, add the Contents/Java folder to the
  // java.library.path, so that native libraries work properly.
  // Without this, the library path is only set to Contents/MacOS
  // where the launcher binary lives.
  if (platform == MACOSX) {
    URL coreJarURL =
      PApplet.class.getProtectionDomain().getCodeSource().getLocation();
    // The jarPath from above will/may be URL encoded (%20 for spaces)
    String coreJarPath = urlDecode(coreJarURL.getPath());
    if (coreJarPath.endsWith("/Contents/Java/core.jar")) {
      // remove the /core.jar part from the end
      String javaPath = coreJarPath.substring(0, coreJarPath.length() - 9);
      String libraryPath = System.getProperty("java.library.path");
      libraryPath += File.pathSeparator + javaPath;
      System.setProperty("java.library.path", libraryPath);
    }
  }
  */

  // Catch any HeadlessException to provide more useful feedback
  try {
    // Call validate() while resize events are in progress
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
  } catch (HeadlessException e) {
    System.err.println("Cannot run sketch without a display. Read this for possible solutions:");
    System.err.println("https://github.com/processing/processing/wiki/Running-without-a-Display");
    System.exit(1);
  }

  // So that the system proxy setting are used by default
  System.setProperty("java.net.useSystemProxies", "true");

  if (args.length < 1) {
    System.err.println("Usage: PApplet [options] <class name> [sketch args]");
    System.err.println("See the Javadoc for PApplet for an explanation.");
    System.exit(1);
  }

  boolean external = false;
  int[] location = null;
  int[] editorLocation = null;

  String name = null;
  int windowColor = 0;
  int stopColor = 0xff808080;
  boolean hideStop = false;

  int displayNum = -1;  // use default
//    boolean fullScreen = false;
  boolean present = false;
//    boolean spanDisplays = false;
  int density = -1;

  String param = null, value = null;
  String folder = calcSketchPath();

  int argIndex = 0;
  while (argIndex < args.length) {
    int equals = args[argIndex].indexOf('=');
    if (equals != -1) {
      param = args[argIndex].substring(0, equals);
      value = args[argIndex].substring(equals + 1);

      if (param.equals(ARGS_EDITOR_LOCATION)) {
        external = true;
        editorLocation = parseInt(split(value, ','));

      } else if (param.equals(ARGS_DISPLAY)) {
        displayNum = parseInt(value, -1);
        if (displayNum == -1) {
          System.err.println("Could not parse " + value + " for " + ARGS_DISPLAY);
        }

      } else if (param.equals(ARGS_WINDOW_COLOR)) {
        if (value.charAt(0) == '#' && value.length() == 7) {
          value = value.substring(1);
          windowColor = 0xff000000 | Integer.parseInt(value, 16);
        } else {
          System.err.println(ARGS_WINDOW_COLOR + " should be a # followed by six digits");
        }

      } else if (param.equals(ARGS_STOP_COLOR)) {
        if (value.charAt(0) == '#' && value.length() == 7) {
          value = value.substring(1);
          stopColor = 0xff000000 | Integer.parseInt(value, 16);
        } else {
          System.err.println(ARGS_STOP_COLOR + " should be a # followed by six digits");
        }

      } else if (param.equals(ARGS_SKETCH_FOLDER)) {
        folder = value;

      } else if (param.equals(ARGS_LOCATION)) {
        location = parseInt(split(value, ','));

      } else if (param.equals(ARGS_DENSITY)) {
        density = parseInt(value, -1);
        if (density == -1) {
          System.err.println("Could not parse " + value + " for " + ARGS_DENSITY);
        } else if (density != 1 && density != 2) {
          density = -1;
          System.err.println(ARGS_DENSITY + " should be 1 or 2");
        }
      }

    } else {
      if (args[argIndex].equals(ARGS_PRESENT)) {
        present = true;

//        } else if (args[argIndex].equals(ARGS_SPAN_DISPLAYS)) {
//          spanDisplays = true;

      } else if (args[argIndex].equals(ARGS_HIDE_STOP)) {
        hideStop = true;

      } else if (args[argIndex].equals(ARGS_EXTERNAL)) {
        external = true;

      } else {
        name = args[argIndex];
        break;  // because of break, argIndex won't increment again
      }
    }
    argIndex++;
  }

//    // Now that sketch path is passed in args after the sketch name
//    // it's not set in the above loop(the above loop breaks after
//    // finding sketch name). So setting sketch path here.
//    // https://github.com/processing/processing/commit/0a14835e6f5f4766b022e73a8fe562318636727c
//    // TODO this is a hack added for PDE X and needs to be removed [fry 141104]
//    for (int i = 0; i < args.length; i++) {
//      if (args[i].startsWith(ARGS_SKETCH_FOLDER)){
//        folder = args[i].substring(args[i].indexOf('=') + 1);
//      }
//    }

  final PApplet sketch;
  if (constructedSketch != null) {
    sketch = constructedSketch;
  } else {
    try {
      Class<?> c =
        Thread.currentThread().getContextClassLoader().loadClass(name);
      sketch = (PApplet) c.newInstance();
    } catch (RuntimeException re) {
      // Don't re-package runtime exceptions
      throw re;
    } catch (Exception e) {
      // Package non-runtime exceptions so we can throw them freely
      throw new RuntimeException(e);
    }
  }

  if (platform == MACOSX) {
    try {
      final String td = "processing.core.ThinkDifferent";
      Class<?> thinkDifferent =
        Thread.currentThread().getContextClassLoader().loadClass(td);
      Method method =
        thinkDifferent.getMethod("init", new Class[] { PApplet.class });
      method.invoke(null, new Object[] { sketch });
    } catch (Exception e) {
      e.printStackTrace();  // That's unfortunate
    }
  }

  // Set the suggested display that's coming from the command line
  // (and most likely, from the PDE's preference setting).
  sketch.display = displayNum;

  // Set the suggested density that is coming from command line
  // (most likely set from the PDE based on a system DPI scaling)
  sketch.suggestedDensity = density;

  sketch.present = present;

  // For 3.0.1, moved this above handleSettings() so that loadImage() can be
  // used inside settings(). Sets a terrible precedent, but the alternative
  // of not being able to size a sketch to an image is driving people loopy.
  // A handful of things that need to be set before init/start.
//    if (folder == null) {
//      folder = calcSketchPath();
//    }
  sketch.sketchPath = folder;

  // Don't set 'args' to a zero-length array if it should be null [3.0a8]
  if (args.length != argIndex + 1) {
    // pass everything after the class name in as args to the sketch itself
    // (fixed for 2.0a5, this was just subsetting by 1, which didn't skip opts)
    sketch.args = PApplet.subset(args, argIndex + 1);
  }

  // Call the settings() method which will give us our size() call
//    try {
  sketch.handleSettings();
//    } catch (Throwable t) {
//      System.err.println("I think I'm gonna hurl");
//    }

////    sketch.spanDisplays = spanDisplays;
//    // If spanning screens, that means we're also full screen.
////    fullScreen |= spanDisplays;
//    if (spanDisplays) {
//      displayIndex = SPAN;
////      fullScreen = true;
//    }

//    // If the applet doesn't call for full screen, but the command line does,
//    // enable it. Conversely, if the command line does not, don't disable it.
//    // Query the applet to see if it wants to be full screen all the time.
//    //fullScreen |= sketch.sketchFullScreen();
//    sketch.fullScreen |= fullScreen;

  sketch.external = external;

  if (windowColor != 0) {
    sketch.windowColor = windowColor;
  }

  final PSurface surface = sketch.initSurface();
//      sketch.initSurface(windowColor, displayIndex, fullScreen, spanDisplays);

  /*
  // Wait until the applet has figured out its width. In a static mode app,
  // everything happens inside setup(), so this will be after setup() has
  // completed, and the empty draw() has set "finished" to true.
  while (sketch.defaultSize && !sketch.finished) {
    //System.out.println("default size");
    try {
      Thread.sleep(5);

    } catch (InterruptedException e) {
      //System.out.println("interrupt");
    }
  }
  */

  if (present) {
    if (hideStop) {
      stopColor = 0;  // they'll get the hint
    }
    surface.placePresent(stopColor);
  } else {
    surface.placeWindow(location, editorLocation);
  }

  // not always running externally when in present mode
  // moved above setVisible() in 3.0 alpha 11
  if (sketch.external) {
    surface.setupExternalMessages();
  }

  sketch.showSurface();
  sketch.startSurface();
  /*
  if (sketch.getGraphics().displayable()) {
    surface.setVisible(true);
  }

  //sketch.init();
  surface.startThread();
  */
  
  ret frame;
}

Author comment

Began life as a copy of #1013740

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014279
Snippet name: pMain2 - start Processing applet, retain Frame and instance [dev.]
Eternal ID of this version: #1014279/7
Text MD5: 415600df243359413b150c519bcd9a86
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-19 18:05:53
Source code size: 10402 bytes / 306 lines
Pitched / IR pitched: No / No
Views / Downloads: 441 / 474
Version history: 6 change(s)
Referenced in: [show references]