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

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

初探PApplet窗口打开方式(Processing程序)的更多相关文章

  1. Xcode 的正确打开方式——Debugging(转载)

    Xcode 的正确打开方式——Debugging   程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...

  2. 添加/删除/修改Windows 7右键的“打开方式”

    右键菜单添加/删除"打开方式" 此"打开方式"非系统的"打开方式",二者可以并存. 右键菜单添加"打开方式" 在HKEY ...

  3. 解决:win7右键打开方式添加应用程序无法设置和删除多余的打开方式

    win7右键打开方式添加应用程序无法设置 点击“开始”—“运行”,输入“regedit”打开注册表,在“HKEY_CLASSES_ROOT\Applications\”中找到无法添加的程序 ( 比如“ ...

  4. windows右键打开方式里面添加新的应用程序

    1.打开注册表编辑器.打开运行窗口,快捷键,开始+R.输入“regedit”,回车确定. 2.进入注册表编辑器的HKEY_CLASSES_ROOT文件夹下的*子文件夹下的shell文件夹. 3.右键s ...

  5. 关于无法把程序(Adobe Fireworks CS5)添加到打开方式的解决办法

    关于无法把程序(Adobe Fireworks CS5)添加到打开方式的解决办法 最近换了新版的Adobe Fireworks CS5,发现打开图片文件时在右键“打开方式”里仍然是以前的Firewor ...

  6. 删除 Mac OS X 中“打开方式”里重复或无用的程序列表

    如果右键菜单的「打开方式」里出现了已不存在的应用程序或者重复的项目,打开终端,执行以下命令: /System/Library/Frameworks/CoreServices.framework/Ver ...

  7. python实现应用程序在右键菜单中添加打开方式

    最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写 1.需求拆解: 如何实现手动添加右键菜单的打开方式: Step1:打开注册表编辑器,Win+R-> ...

  8. ubuntu 添加右键打开方式,无法添加程序打开方式

    最近把工作环境迁移到ubuntu,装了WPS for Linux ,说实话确实是十分良心啊!运行效率奇高,绿色无广告,并且和windows版本无异,感觉就可以抛弃自带的libreoffice了. 但是 ...

  9. 【转载】win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序)

    win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序) 以下文章 部分选自 https://blog.csdn.net/shan165310175/article/details/8 ...

  10. ppt标签打开文件 word标签打开文件 窗口打开文件 粘贴默认方式

    ppt标签打开文件 word标签打开文件 word窗口打开文件 ppt粘贴默认方式 word粘贴默认方式 ppt粘贴默认方式 只保留文本 == 通过 视图 切换窗口. == 层叠  样式 如下. == ...

随机推荐

  1. openEuler欧拉修改SSH端口

    修改SSH端口的主要原因是提高服务器的安全性.默认情况下,SSH服务运行在端口22上,因此攻击者和自动化脚本通常会针对此端口发起暴力破解攻击.密码猜测和其他恶意活动. vim /etc/ssh/ssh ...

  2. 【NAS】绿联NAS+极狐Gitlab+1Panel

    1. 准备域名 例如我的 ???.mllt.cc 2. 内网穿透 我使用的Natfrp(https://www.natfrp.com/tunnel/) 创建HTTP隧道(对应端口10080)创建HTT ...

  3. mysql5.7以后group by 报错 sql_mode=only_full_group_by的解决方法

    一.发现问题 1.查询语句 SELECT * from class group by class_name; 2.报错结果 ..... this is incompatible with sql_mo ...

  4. 搭建SpringBoot中验证数据机制问题 Add a provider like Hibernate Validator (RI) to your classpath

    搭建SpringBoot中的验证数据机制时出现的错误 报错代码 java.lang.IllegalStateException: Failed to load ApplicationContext a ...

  5. Qt/C++编写的mqtt调试助手使用说明

    一.使用说明 第一步,选择协议前缀,可选mqtt://.mqtts://.ws://.wss://四种,带s结尾的是走ssl通信,ws表示走websocket通信.一般选默认的mqtt://就好. 第 ...

  6. [转]CMake菜谱(CMake Cookbook中文版)

    CMake菜谱(CMake Cookbook中文版) 翻译 搜索 复制

  7. C#中串口类SerialPort类的详细用法

    原文链:serialport控件的详细用法

  8. Matlab R2009b 版 license 到期问题

    打开2009b Matlab,反复提示需要激活,是lisense到期了,到期时间2017年11月11日.解决方法: 将以下内容替换 Matlab 安装路径下 license/*.lic 文件中的所有内 ...

  9. 即时通讯框架MobileIMSDK的H5端开发快速入门

    ► 相关链接: ① MobileIMSDK-H5端的详细介绍 ② MobileIMSDK-H5端的开发手册new(* 精编PDF版) 一.技术准备 您是否已对Web端即时通讯技术有所了解? 1)新手入 ...

  10. Python 进阶:深入理解 import 机制与 importlib 的妙用

    大家好,今天我们来深入探讨 Python 中的导入机制和 importlib 模块.相信不少朋友和我一样,平时写代码时可能只用过最基础的 import 语句,或者偶尔用 importlib.impor ...