您的位置:首页 > 移动开发

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

2021-04-13 10:39 921 查看

使用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)
}

有哪里出现遗漏的或是错误的讲解,请指正,感谢您的阅读!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: