初探PApplet窗口打开方式(Processing程序)
使用Processing快6年了,是时候回过头来看看它的"main"方法了,也就是它从哪出生的?~~~
源码学习
//////////////////////////////////////////////////////////////
// MAIN
/**
* main() method for running this class from the command line.
* <p>
* Usage: PApplet [options] <class name> [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程序)的更多相关文章
- Xcode 的正确打开方式——Debugging(转载)
Xcode 的正确打开方式——Debugging 程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...
- 添加/删除/修改Windows 7右键的“打开方式”
右键菜单添加/删除"打开方式" 此"打开方式"非系统的"打开方式",二者可以并存. 右键菜单添加"打开方式" 在HKEY ...
- 解决:win7右键打开方式添加应用程序无法设置和删除多余的打开方式
win7右键打开方式添加应用程序无法设置 点击“开始”—“运行”,输入“regedit”打开注册表,在“HKEY_CLASSES_ROOT\Applications\”中找到无法添加的程序 ( 比如“ ...
- windows右键打开方式里面添加新的应用程序
1.打开注册表编辑器.打开运行窗口,快捷键,开始+R.输入“regedit”,回车确定. 2.进入注册表编辑器的HKEY_CLASSES_ROOT文件夹下的*子文件夹下的shell文件夹. 3.右键s ...
- 关于无法把程序(Adobe Fireworks CS5)添加到打开方式的解决办法
关于无法把程序(Adobe Fireworks CS5)添加到打开方式的解决办法 最近换了新版的Adobe Fireworks CS5,发现打开图片文件时在右键“打开方式”里仍然是以前的Firewor ...
- 删除 Mac OS X 中“打开方式”里重复或无用的程序列表
如果右键菜单的「打开方式」里出现了已不存在的应用程序或者重复的项目,打开终端,执行以下命令: /System/Library/Frameworks/CoreServices.framework/Ver ...
- python实现应用程序在右键菜单中添加打开方式
最近项目组开发的一个小工具想要在右键菜单中添加打开方式,以有道云笔记为例进行了需求拆解和代码编写 1.需求拆解: 如何实现手动添加右键菜单的打开方式: Step1:打开注册表编辑器,Win+R-> ...
- ubuntu 添加右键打开方式,无法添加程序打开方式
最近把工作环境迁移到ubuntu,装了WPS for Linux ,说实话确实是十分良心啊!运行效率奇高,绿色无广告,并且和windows版本无异,感觉就可以抛弃自带的libreoffice了. 但是 ...
- 【转载】win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序)
win10解决设置默认打开方式不生效问题(双击每次都要选择默认打开程序) 以下文章 部分选自 https://blog.csdn.net/shan165310175/article/details/8 ...
- ppt标签打开文件 word标签打开文件 窗口打开文件 粘贴默认方式
ppt标签打开文件 word标签打开文件 word窗口打开文件 ppt粘贴默认方式 word粘贴默认方式 ppt粘贴默认方式 只保留文本 == 通过 视图 切换窗口. == 层叠 样式 如下. == ...
随机推荐
- 金TECH频道|最近备受关注的“应用重构”到底是什么?
"金TECH频道"旨在为您分享中电金信助力行业数字化转型的最新产品业务动态.技术观点洞察与应用实践案例.让我们在这里,与行业发展同频共振,共筑数字新基石.
- #渗透测试 kioptix level 2靶机通关教程及提权
声明! 文章所提到的网站以及内容,只做学习交流,其他均与本人以及泷羽sec团队无关,切勿触碰法律底线,否则后果自负!!!! 工具链接:https://pan.quark.cn/s/530656ba55 ...
- 龙哥量化:通达信常用指标写法macd数值太小怎么办macd的数值是0.01怎么放大
1.先放公式 MACD 放大坐标系 1000倍 参数 12.26.9DIF:EMA(CLOSE*1000,12)-EMA(CLOSE*1000,26);DEA:EMA(DIF,9);MACD:(DIF ...
- 这可能是国内Qt/C++界最受欢迎开源项目之一/5.8Kstar/持续迭代更新
一.前言 本项目大概在2020年开始的,大概在2022年重写了一遍,主要是分门别类存放.本项目主要是QWidget编写的一些开源的demo,支持Qt4.Qt5.Qt6,支持任意系统,预计会有100多个 ...
- Quartz分布式定时任务
前言: 项目需要执行定时任务,该类定时任务只需要实现类似Spring原生的@Scheudle注解的定时方法即可,无需考虑分片.刷新及重启,且因项目是多实例,所以需要考虑实现分布式,考察了目前开源的几款 ...
- Unity TheHeretic Gawain Demo 异教徒Demo技术学习
<异教徒 Heretic>是Unity在2019年GDC大会上展示的一款技术Demo,部分资源于2020年中旬公开下载. 这款Demo主要用于展示Unity在数字人技术领域的最新进展,尤其 ...
- Solution -「ROI 2018」「LOJ #2850」无进位加法
\(\mathscr{Description}\) Link. 给定 \(\{a_n\}\),每次令某个 \(a_i\leftarrow a_i+1\),最终得到 \(\{b_n\}\),求 ...
- Java工具类HttpClientUtil
1. 依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> ...
- .NET Core GC标记阶段(mark_phase)底层原理浅谈
简介 C# 采用基于代的回收机制,并使用了更复杂的 链式跟踪算法 来识别对象是否为垃圾. GC触发的原因 截至到.NET 8,GC触发的原因有18种 enum gc_reason { reason_a ...
- 题解:AT_abc389_d [ABC389D] Squares in Circle
假定原点为圆心. 我们只考虑点在第一象限的情况,剩下的情况同理. 因为圆心是原点,所以在圆内的点的横坐标一定在 \(r\) 之内. 枚举点的横坐标 \(x + \frac{1}{2}\),二分最大的 ...