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)

1  
set flag AllPublic.
2  
3  
lib 1013739
4  
5  
import processing.core.*;
6  
7  
static <A extends PApplet2> Pair<A, Frame> pMain2(final Class<A> c) {
8  
  ret swing(func -> Pair<A, Frame> {
9  
    A a = nuInstance(c);
10  
    Frame f = pMain2_runSketch(null, a);
11  
    clearThreadLocal(PApplet2_instancesMade);
12  
    ret pair(a, f);
13  
  });
14  
}
15  
16  
static PApplet2 pMain2() {
17  
  ret pMain2(mc());
18  
}
19  
20  
static Frame pMain2_runSketch(final String[] args,
21  
                               final PApplet constructedSketch) {
22  
  // Supposed to help with flicker, but no effect on OS X.
23  
  // TODO IIRC this helped on Windows, but need to double check.
24  
  System.setProperty("sun.awt.noerasebackground", "true");
25  
26  
  // Remove 60fps limit on the JavaFX "pulse" timer
27  
  System.setProperty("javafx.animation.fullspeed", "true");
28  
29  
  // Doesn't seem to do anything helpful here (that can't be done via Runner)
30  
  //System.setProperty("com.apple.mrj.application.apple.menu.about.name", "potato");
31  
32  
  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
33  
    public void uncaughtException(Thread t, Throwable e) {
34  
      e.printStackTrace();
35  
      uncaughtThrowable = e;
36  
    }
37  
  });
38  
39  
  // This doesn't work, need to mess with Info.plist instead
40  
  /*
41  
  // In an exported application, add the Contents/Java folder to the
42  
  // java.library.path, so that native libraries work properly.
43  
  // Without this, the library path is only set to Contents/MacOS
44  
  // where the launcher binary lives.
45  
  if (platform == MACOSX) {
46  
    URL coreJarURL =
47  
      PApplet.class.getProtectionDomain().getCodeSource().getLocation();
48  
    // The jarPath from above will/may be URL encoded (%20 for spaces)
49  
    String coreJarPath = urlDecode(coreJarURL.getPath());
50  
    if (coreJarPath.endsWith("/Contents/Java/core.jar")) {
51  
      // remove the /core.jar part from the end
52  
      String javaPath = coreJarPath.substring(0, coreJarPath.length() - 9);
53  
      String libraryPath = System.getProperty("java.library.path");
54  
      libraryPath += File.pathSeparator + javaPath;
55  
      System.setProperty("java.library.path", libraryPath);
56  
    }
57  
  }
58  
  */
59  
60  
  // Catch any HeadlessException to provide more useful feedback
61  
  try {
62  
    // Call validate() while resize events are in progress
63  
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
64  
  } catch (HeadlessException e) {
65  
    System.err.println("Cannot run sketch without a display. Read this for possible solutions:");
66  
    System.err.println("https://github.com/processing/processing/wiki/Running-without-a-Display");
67  
    System.exit(1);
68  
  }
69  
70  
  // So that the system proxy setting are used by default
71  
  System.setProperty("java.net.useSystemProxies", "true");
72  
73  
  if (args.length < 1) {
74  
    System.err.println("Usage: PApplet [options] <class name> [sketch args]");
75  
    System.err.println("See the Javadoc for PApplet for an explanation.");
76  
    System.exit(1);
77  
  }
78  
79  
  boolean external = false;
80  
  int[] location = null;
81  
  int[] editorLocation = null;
82  
83  
  String name = null;
84  
  int windowColor = 0;
85  
  int stopColor = 0xff808080;
86  
  boolean hideStop = false;
87  
88  
  int displayNum = -1;  // use default
89  
//    boolean fullScreen = false;
90  
  boolean present = false;
91  
//    boolean spanDisplays = false;
92  
  int density = -1;
93  
94  
  String param = null, value = null;
95  
  String folder = calcSketchPath();
96  
97  
  int argIndex = 0;
98  
  while (argIndex < args.length) {
99  
    int equals = args[argIndex].indexOf('=');
100  
    if (equals != -1) {
101  
      param = args[argIndex].substring(0, equals);
102  
      value = args[argIndex].substring(equals + 1);
103  
104  
      if (param.equals(ARGS_EDITOR_LOCATION)) {
105  
        external = true;
106  
        editorLocation = parseInt(split(value, ','));
107  
108  
      } else if (param.equals(ARGS_DISPLAY)) {
109  
        displayNum = parseInt(value, -1);
110  
        if (displayNum == -1) {
111  
          System.err.println("Could not parse " + value + " for " + ARGS_DISPLAY);
112  
        }
113  
114  
      } else if (param.equals(ARGS_WINDOW_COLOR)) {
115  
        if (value.charAt(0) == '#' && value.length() == 7) {
116  
          value = value.substring(1);
117  
          windowColor = 0xff000000 | Integer.parseInt(value, 16);
118  
        } else {
119  
          System.err.println(ARGS_WINDOW_COLOR + " should be a # followed by six digits");
120  
        }
121  
122  
      } else if (param.equals(ARGS_STOP_COLOR)) {
123  
        if (value.charAt(0) == '#' && value.length() == 7) {
124  
          value = value.substring(1);
125  
          stopColor = 0xff000000 | Integer.parseInt(value, 16);
126  
        } else {
127  
          System.err.println(ARGS_STOP_COLOR + " should be a # followed by six digits");
128  
        }
129  
130  
      } else if (param.equals(ARGS_SKETCH_FOLDER)) {
131  
        folder = value;
132  
133  
      } else if (param.equals(ARGS_LOCATION)) {
134  
        location = parseInt(split(value, ','));
135  
136  
      } else if (param.equals(ARGS_DENSITY)) {
137  
        density = parseInt(value, -1);
138  
        if (density == -1) {
139  
          System.err.println("Could not parse " + value + " for " + ARGS_DENSITY);
140  
        } else if (density != 1 && density != 2) {
141  
          density = -1;
142  
          System.err.println(ARGS_DENSITY + " should be 1 or 2");
143  
        }
144  
      }
145  
146  
    } else {
147  
      if (args[argIndex].equals(ARGS_PRESENT)) {
148  
        present = true;
149  
150  
//        } else if (args[argIndex].equals(ARGS_SPAN_DISPLAYS)) {
151  
//          spanDisplays = true;
152  
153  
      } else if (args[argIndex].equals(ARGS_HIDE_STOP)) {
154  
        hideStop = true;
155  
156  
      } else if (args[argIndex].equals(ARGS_EXTERNAL)) {
157  
        external = true;
158  
159  
      } else {
160  
        name = args[argIndex];
161  
        break;  // because of break, argIndex won't increment again
162  
      }
163  
    }
164  
    argIndex++;
165  
  }
166  
167  
//    // Now that sketch path is passed in args after the sketch name
168  
//    // it's not set in the above loop(the above loop breaks after
169  
//    // finding sketch name). So setting sketch path here.
170  
//    // https://github.com/processing/processing/commit/0a14835e6f5f4766b022e73a8fe562318636727c
171  
//    // TODO this is a hack added for PDE X and needs to be removed [fry 141104]
172  
//    for (int i = 0; i < args.length; i++) {
173  
//      if (args[i].startsWith(ARGS_SKETCH_FOLDER)){
174  
//        folder = args[i].substring(args[i].indexOf('=') + 1);
175  
//      }
176  
//    }
177  
178  
  final PApplet sketch;
179  
  if (constructedSketch != null) {
180  
    sketch = constructedSketch;
181  
  } else {
182  
    try {
183  
      Class<?> c =
184  
        Thread.currentThread().getContextClassLoader().loadClass(name);
185  
      sketch = (PApplet) c.newInstance();
186  
    } catch (RuntimeException re) {
187  
      // Don't re-package runtime exceptions
188  
      throw re;
189  
    } catch (Exception e) {
190  
      // Package non-runtime exceptions so we can throw them freely
191  
      throw new RuntimeException(e);
192  
    }
193  
  }
194  
195  
  if (platform == MACOSX) {
196  
    try {
197  
      final String td = "processing.core.ThinkDifferent";
198  
      Class<?> thinkDifferent =
199  
        Thread.currentThread().getContextClassLoader().loadClass(td);
200  
      Method method =
201  
        thinkDifferent.getMethod("init", new Class[] { PApplet.class });
202  
      method.invoke(null, new Object[] { sketch });
203  
    } catch (Exception e) {
204  
      e.printStackTrace();  // That's unfortunate
205  
    }
206  
  }
207  
208  
  // Set the suggested display that's coming from the command line
209  
  // (and most likely, from the PDE's preference setting).
210  
  sketch.display = displayNum;
211  
212  
  // Set the suggested density that is coming from command line
213  
  // (most likely set from the PDE based on a system DPI scaling)
214  
  sketch.suggestedDensity = density;
215  
216  
  sketch.present = present;
217  
218  
  // For 3.0.1, moved this above handleSettings() so that loadImage() can be
219  
  // used inside settings(). Sets a terrible precedent, but the alternative
220  
  // of not being able to size a sketch to an image is driving people loopy.
221  
  // A handful of things that need to be set before init/start.
222  
//    if (folder == null) {
223  
//      folder = calcSketchPath();
224  
//    }
225  
  sketch.sketchPath = folder;
226  
227  
  // Don't set 'args' to a zero-length array if it should be null [3.0a8]
228  
  if (args.length != argIndex + 1) {
229  
    // pass everything after the class name in as args to the sketch itself
230  
    // (fixed for 2.0a5, this was just subsetting by 1, which didn't skip opts)
231  
    sketch.args = PApplet.subset(args, argIndex + 1);
232  
  }
233  
234  
  // Call the settings() method which will give us our size() call
235  
//    try {
236  
  sketch.handleSettings();
237  
//    } catch (Throwable t) {
238  
//      System.err.println("I think I'm gonna hurl");
239  
//    }
240  
241  
////    sketch.spanDisplays = spanDisplays;
242  
//    // If spanning screens, that means we're also full screen.
243  
////    fullScreen |= spanDisplays;
244  
//    if (spanDisplays) {
245  
//      displayIndex = SPAN;
246  
////      fullScreen = true;
247  
//    }
248  
249  
//    // If the applet doesn't call for full screen, but the command line does,
250  
//    // enable it. Conversely, if the command line does not, don't disable it.
251  
//    // Query the applet to see if it wants to be full screen all the time.
252  
//    //fullScreen |= sketch.sketchFullScreen();
253  
//    sketch.fullScreen |= fullScreen;
254  
255  
  sketch.external = external;
256  
257  
  if (windowColor != 0) {
258  
    sketch.windowColor = windowColor;
259  
  }
260  
261  
  final PSurface surface = sketch.initSurface();
262  
//      sketch.initSurface(windowColor, displayIndex, fullScreen, spanDisplays);
263  
264  
  /*
265  
  // Wait until the applet has figured out its width. In a static mode app,
266  
  // everything happens inside setup(), so this will be after setup() has
267  
  // completed, and the empty draw() has set "finished" to true.
268  
  while (sketch.defaultSize && !sketch.finished) {
269  
    //System.out.println("default size");
270  
    try {
271  
      Thread.sleep(5);
272  
273  
    } catch (InterruptedException e) {
274  
      //System.out.println("interrupt");
275  
    }
276  
  }
277  
  */
278  
279  
  if (present) {
280  
    if (hideStop) {
281  
      stopColor = 0;  // they'll get the hint
282  
    }
283  
    surface.placePresent(stopColor);
284  
  } else {
285  
    surface.placeWindow(location, editorLocation);
286  
  }
287  
288  
  // not always running externally when in present mode
289  
  // moved above setVisible() in 3.0 alpha 11
290  
  if (sketch.external) {
291  
    surface.setupExternalMessages();
292  
  }
293  
294  
  sketch.showSurface();
295  
  sketch.startSurface();
296  
  /*
297  
  if (sketch.getGraphics().displayable()) {
298  
    surface.setVisible(true);
299  
  }
300  
301  
  //sketch.init();
302  
  surface.startThread();
303  
  */
304  
  
305  
  ret frame;
306  
}

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: 339 / 368
Version history: 6 change(s)
Referenced in: [show references]