初探PApplet窗口打開方式(Processing程序)


使用Processing快6年了,是時候回過頭來看看它的"main"方法了,也就是它從哪出生的😁?~~~


源碼學習

  //////////////////////////////////////////////////////////////

  // MAIN


  /**
   * main() method for running this class from the command line.
   * <p>
   * Usage: PApplet [options] &lt;class name&gt; [sketch args]
   * <ul>
   * <li>The [options] are one or several of the parameters seen below.
   * <li>The class name is required. If you're running outside the PDE and
   * your class is in a package, this should include the full name. That means
   * that if the class is called Sketchy and the package is com.sketchycompany
   * then com.sketchycompany.Sketchy should be used as the class name.
   * <li>The [sketch args] are any command line parameters you want to send to
   * the sketch itself. These will be passed into the args[] array in PApplet.
   * <p>
   * The simplest way to turn and sketch into an application is to
   * add the following code to your program:
   * <PRE>static public void main(String args[]) {
   *   PApplet.main("YourSketchName");
   * }</PRE>
   * That will properly launch your code from a double-clickable .jar
   * or from the command line.
   * <PRE>
   * Parameters useful for launching or also used by the PDE:
   *
   * --location=x,y         Upper-lefthand corner of where the applet
   *                        should appear on screen. If not used,
   *                        the default is to center on the main screen.
   *
   * --present              Presentation mode: blanks the entire screen and
   *                        shows the sketch by itself. If the sketch is
   *                        smaller than the screen, the background around it
   *                        will use the --window-color setting.
   *
   * --hide-stop            Use to hide the stop button in situations where
   *                        you don't want to allow users to exit. also
   *                        see the FAQ on information for capturing the ESC
   *                        key when running in presentation mode.
   *
   * --stop-color=#xxxxxx   Color of the 'stop' text used to quit an
   *                        sketch when it's in present mode.
   *
   * --window-color=#xxxxxx Background color of the window. The color used
   *                        around the sketch when it's smaller than the
   *                        minimum window size for the OS, and the matte
   *                        color when using 'present' mode.
   *
   * --sketch-path          Location of where to save files from functions
   *                        like saveStrings() or saveFrame(). defaults to
   *                        the folder that the java application was
   *                        launched from, which means if this isn't set by
   *                        the pde, everything goes into the same folder
   *                        as processing.exe.
   *
   * --display=n            Set what display should be used by this sketch.
   *                        Displays are numbered starting from 1. This will
   *                        be overridden by fullScreen() calls that specify
   *                        a display. Omitting this option will cause the
   *                        default display to be used.
   *
   * Parameters used by Processing when running via the PDE
   *
   * --external             set when the applet is being used by the PDE
   *
   * --editor-location=x,y  position of the upper-lefthand corner of the
   *                        editor window, for placement of applet window
   *
   * All parameters *after* the sketch class name are passed to the sketch
   * itself and available from its 'args' array while the sketch is running.
   *
   * @see PApplet#args
   * </PRE>
   */
  static public void main(final String[] args) {
    runSketch(args, null);
  }


  /**
   * Convenience method so that PApplet.main(YourSketch.class)
   * launches a sketch, rather than having to call getName() on it.
   */
  static public void main(final Class<?> mainClass, String... args) {
    main(mainClass.getName(), args);
  }


  /**
   * Convenience method so that PApplet.main("YourSketch") launches a sketch,
   * rather than having to wrap it into a single element String array.
   * @param mainClass name of the class to load (with package if any)
   */
  static public void main(final String mainClass) {
    main(mainClass, null);
  }


  /**
   * Convenience method so that PApplet.main("YourSketch", args) launches a
   * sketch, rather than having to wrap it into a String array, and appending
   * the 'args' array when not null.
   * @param mainClass name of the class to load (with package if any)
   * @param sketchArgs command line arguments to pass to the sketch's 'args'
   *             array. Note that this is <i>not</i> the same as the args passed
   *             to (and understood by) PApplet such as --display.
   */
  static public void main(final String mainClass, final String[] sketchArgs) {
    String[] args = new String[] { mainClass };
    if (sketchArgs != null) {
      args = concat(args, sketchArgs);
    }
    runSketch(args, null);
  }

還有一個超長,也是最重要的runSketch()我這就不貼了。。。
可以看到,主要有兩種方法運行PApplet對象,即JFrame窗口。如下:

PApplet.main()

下面是默認的pde輸出程序自動生成的.java文件中的main方法:

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "test4run" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }

一般只需調用PApplet.main()即可,參數為一個字符串數組,如果只填一個也可,填類名,必須一致,不然會報錯!如果標准填法,如下:

    String[] appletArgs = new String[] { "--present", "--window-color=#FFFFFF", "--stop-color=#cccccc", "Appname" };

這些字符串都是作為參數傳給runSketch(),把相應的開關打開配置參數,簡單看一下:

/*
   * --location=x,y         窗口的懸浮位置,相對於桌面窗口坐標系,默認是居中
   *
   * --present              展示模式,全屏,有個底色,即window-color,只要size尺寸小於屏幕大小未填充區域則顯示底色
   *
   * --hide-stop            展示模式中是否隱藏stop按鈕,當然即使隱藏ESC仍舊有效
   *
   * --stop-color=#xxxxxx   stop按鈕顏色,主要是防止和底色相近難以辨別
   *
   * --window-color=#xxxxxx 底色
   *
   * --sketch-path          項目目錄,針對保存幀等寫操作的路徑參數
   *
   * --display=n            顯示的窗口索引,這和實際的顯示設備和系統標定的顯示標號相掛鈎
   *
   * --external             擴展的一些方法判斷依據(一般作為一個布爾值使用)[待研究]
   *
   * --editor-location=x,y  編輯器窗口位置,方便定義應用窗口位置[待研究]
*/

PApplet.runSketch()

這一種方法比較靈活,入口函數啟動如下(kotlin):

fun main(args: Array<String>) {
    var app = ShowApp()
    PApplet.runSketch(arrayOf("show"),app)
}

注意需要new一個PApplet對象,然后作為第二參數傳入,第一參數類型為String[]。當然也可以在此拓展,我們可以任意創建窗口,實現多窗口開發或展示(kotlin):

    var bsapp = BoardShowApp()
    var bsapp2 = BoardShowApp()
    PApplet.runSketch(arrayOf("BoardShow1"),bsapp)
    PApplet.runSketch(arrayOf("BoardShow2"),bsapp2)

如果讀者細心學習Processing官方示例,有個多窗口應用的范例(MultipleWindows):

你會發現它就創建了一個PApplet類ChildApplet,作為子窗口,然后在構造器中使用了上述開啟窗口的方法----PApplet.runSketch()
筆者發現不再構造器中運行runSketch是無效的,因此如果在運行時想要打開第二個或多個窗口,必須在子類構造時執行這個方法。至於子窗口的種種參數那么跟surface對象有關了,我們往后再聊。

附件

下面是完整代碼的參考:

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 Test4run extends PApplet {


public void setup() {
   
}

public void draw() {
   
}

public void settings() { 
     size(400, 400);
}

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "Test4run" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

import processing.core.PApplet

class ChildApp : PApplet() {
    override fun settings() {
        size(400, 400)
        smooth()
    }

    override fun setup() {
        surface.setTitle("Child sketch")
    }

    override fun draw() {
        background(0)

    }

    override fun mousePressed() {
    }

    override fun mouseDragged() {
    }

    //JFrame frame;
    init {
        runSketch(arrayOf(this.javaClass.name), this)
    }
}

class ShowApp : PApplet(){

    val childapp = ChildApp()
    override fun settings() {
        size(800,400)
    }

    override fun setup() {
    }

    override fun draw() {
        background(20)
    }

}

fun main(args: Array<String>) {
    var sapp = ShowApp()
    var sapp2 = ShowApp()

    PApplet.runSketch(arrayOf("Show1"),sapp)
    PApplet.runSketch(arrayOf("Show2"),sapp2)
}

有哪里出現遺漏的或是錯誤的講解,請指正,感謝您的閱讀!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM