Processing 处理调用void setup()两次,我需要解决这个问题
我正在开发一个程序,从源文件中读取一些图像(.jpg)和文本,并将它们组合成一个PDF。我知道处理可能不是最好的语言,但它是我唯一知道如何处理的语言。无论如何,我有一个问题,即处理呼叫设置两次。我已经看到,当size()是安装程序中的第一行时,这个问题得到了解决,但是我不能让它发生,因为我必须读入并存储所有数据,找到最宽图像的宽度,然后确保其高度足以容纳包含多个图像的页面,并在决定窗口的宽度和高度之前添加文本。我正在寻找关于如何构造代码的建议,以便在不必调用安装程序两次的情况下获取所有信息,因为这会导致我的PDF包含所有数据的两个副本。我已经包括安装,如果它帮助任何人。谢谢Processing 处理调用void setup()两次,我需要解决这个问题,processing,Processing,我正在开发一个程序,从源文件中读取一些图像(.jpg)和文本,并将它们组合成一个PDF。我知道处理可能不是最好的语言,但它是我唯一知道如何处理的语言。无论如何,我有一个问题,即处理呼叫设置两次。我已经看到,当size()是安装程序中的第一行时,这个问题得到了解决,但是我不能让它发生,因为我必须读入并存储所有数据,找到最宽图像的宽度,然后确保其高度足以容纳包含多个图像的页面,并在决定窗口的宽度和高度之前添加文本。我正在寻找关于如何构造代码的建议,以便在不必调用安装程序两次的情况下获取所有信息,因为
void setup(){
font = loadFont("TimesNewRomanPSMT-20.vlw");
File clientsFolder = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients");
clients = clientsFolder.listFiles();
for(File x : clients){
println(x.getName());
}
//test files to see if they end in .txt, and have a matching .pdf extension that is newer
String nextClient = needPdf();
File nextClientData = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients/" + nextClient);
//println(nextClientData.getName());
//open the file for reading
//setup can't throw the exception, and it needs it, so this should take care of it
try{
Scanner scan = new Scanner(nextClientData);
while(scan.hasNextLine() ){
exercises.add(scan.nextLine());
}
//println(exercises.toString());
printedData = new Exercise[exercises.size()];
println(exercises.size());
for(int i = 0; i < exercises.size(); i++){
printedData[i] = new Exercise((String)exercises.get(i));
}
//count the width and height
int w = 0, h = 0;
for(Exercise e: printedData){
if(e.getWidest() > w){
w = e.getWidest();
}
if(e.getTallest() > h){
h = e.getHeight();
}
}
//and finally we can create the freaking window
// this cuts the .txt off
size(w, h, PDF, "C:/Users/[my name]/Desktop/" + nextClient.substring(0, nextClient.length() - 4) + ".pdf");
}catch (FileNotFoundException e){
println("Unknown error in PApplet.setup(). Exiting.");
println(e.getMessage() );
exit();
}
}
void setup(){
font=loadFont(“timesnewromapsmt-20.vlw”);
File clientsFolder=新文件(“C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients”);
clients=clientsFolder.listFiles();
用于(文件x:客户端){
println(x.getName());
}
//测试文件,查看它们是否以.txt结尾,并且是否具有更新的匹配.pdf扩展名
字符串nextClient=needPdf();
File nextClientData=新文件(“C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients/”+nextClient);
//println(nextClientData.getName());
//打开文件进行读取
//安装程序无法抛出该异常,并且它需要它,因此应该处理它
试一试{
扫描仪扫描=新扫描仪(nextClientData);
while(scan.hasNextLine()){
添加(scan.nextLine());
}
//println(exercises.toString());
printedData=新练习[exercises.size()];
println(exercises.size());
对于(int i=0;iw){
w=e.getWidest();
}
if(e.gettalest()>h){
h=e.getHeight();
}
}
//最后,我们可以创建一个奇怪的窗口
//这会切断.txt文件
大小(w,h,PDF,“C:/Users/[my name]/Desktop/”+nextClient.substring(0,nextClient.length()-4)+“.PDF”);
}catch(filenotfounde异常){
println(“PApplet.setup()中的未知错误。正在退出”);
println(如getMessage());
退出();
}
}
也许您可以在完成计算后调整窗口大小?一旦我做了这个草图,看看如何调整大小将工作,它是期待一个图像文件,看看它是否可以帮助你
//no error handling for non image files!
PImage img;
int newCanvasWidth = MIN_WINDOW_WIDTH; // made global to use in draw
int newCanvasHeight = MIN_WINDOW_HEIGHT;
java.awt.Insets insets; //"An Insets object is a representation of the borders of a container"
//from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
void setup()
{
size(200, 200); // always first line
frame.pack(); insets = frame.getInsets();
frame.setResizable(true);
/// for debuging, system depende`nt, at least screen is...
print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
print(" MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
print(" screenWidth = " + displayWidth);
println(" screenHeight = " + displayHeight);
}
void draw()
{
if (img != null)
{
image(img, 0, 0, newCanvasWidth, newCanvasHeight);
}
}
void getImageAndResize(File selected)
{
String path = selected.getAbsolutePath();
if (path == null)
{
println ("nono :-|");
}
else
{
img = loadImage(path);
// a temp variable for readability
int widthInsets =insets.left + insets.right;
int heightInsets =insets.top + insets.bottom;
// constrain values between screen size and minimum window size
int newFrameWidth = constrain(img.width + widthInsets, MIN_WINDOW_WIDTH, displayWidth);
int newFrameHeight = constrain(img.height + heightInsets, MIN_WINDOW_HEIGHT, displayHeight -20);
// Canvas should consider insets for constraining? I think so...
newCanvasWidth = constrain(img.width, MIN_WINDOW_WIDTH - widthInsets, displayWidth - widthInsets);
newCanvasHeight = constrain(img.height, MIN_WINDOW_HEIGHT - heightInsets, displayHeight -20 - heightInsets);
// set canvas size to img size WITHOUT INSETS
setSize(newCanvasWidth, newCanvasHeight);
// set frame size to image + Insets size
frame.setSize(newFrameWidth, newFrameHeight);
//// for debuging
println(path);
println(" ");
print("imgW = " + img.width);
println(" imgH = " + img.height);
print("width+ins = " + widthInsets);
println(" height+ins = " + heightInsets);
print("nFrameW = " + newFrameWidth);
println(" nFrameH = " + newFrameHeight);
print("nCanvasw = " + newCanvasWidth);
println(" nCanvsH = " + newCanvasHeight);
println(" ------ ");
}
}
void mouseClicked()
{
img = null;
selectInput("select an image", "getImageAndResize" );
}
也许您可以在完成计算后调整窗口的大小?一旦我做了这个草图,看看如何调整大小将工作,它是期待一个图像文件,看看它是否可以帮助你
//no error handling for non image files!
PImage img;
int newCanvasWidth = MIN_WINDOW_WIDTH; // made global to use in draw
int newCanvasHeight = MIN_WINDOW_HEIGHT;
java.awt.Insets insets; //"An Insets object is a representation of the borders of a container"
//from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
void setup()
{
size(200, 200); // always first line
frame.pack(); insets = frame.getInsets();
frame.setResizable(true);
/// for debuging, system depende`nt, at least screen is...
print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
print(" MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
print(" screenWidth = " + displayWidth);
println(" screenHeight = " + displayHeight);
}
void draw()
{
if (img != null)
{
image(img, 0, 0, newCanvasWidth, newCanvasHeight);
}
}
void getImageAndResize(File selected)
{
String path = selected.getAbsolutePath();
if (path == null)
{
println ("nono :-|");
}
else
{
img = loadImage(path);
// a temp variable for readability
int widthInsets =insets.left + insets.right;
int heightInsets =insets.top + insets.bottom;
// constrain values between screen size and minimum window size
int newFrameWidth = constrain(img.width + widthInsets, MIN_WINDOW_WIDTH, displayWidth);
int newFrameHeight = constrain(img.height + heightInsets, MIN_WINDOW_HEIGHT, displayHeight -20);
// Canvas should consider insets for constraining? I think so...
newCanvasWidth = constrain(img.width, MIN_WINDOW_WIDTH - widthInsets, displayWidth - widthInsets);
newCanvasHeight = constrain(img.height, MIN_WINDOW_HEIGHT - heightInsets, displayHeight -20 - heightInsets);
// set canvas size to img size WITHOUT INSETS
setSize(newCanvasWidth, newCanvasHeight);
// set frame size to image + Insets size
frame.setSize(newFrameWidth, newFrameHeight);
//// for debuging
println(path);
println(" ");
print("imgW = " + img.width);
println(" imgH = " + img.height);
print("width+ins = " + widthInsets);
println(" height+ins = " + heightInsets);
print("nFrameW = " + newFrameWidth);
println(" nFrameH = " + newFrameHeight);
print("nCanvasw = " + newCanvasWidth);
println(" nCanvsH = " + newCanvasHeight);
println(" ------ ");
}
}
void mouseClicked()
{
img = null;
selectInput("select an image", "getImageAndResize" );
}
在setup()之前移动所有这些函数如何?虽然processing通常会抱怨您“混合了静态和活动模式”,但此黑客似乎在processing 2.0.1中起作用:
int i = beforeSetup();
int szX,szY;
int beforeSetup() {
println("look! I am happening before setup()!!");
szX = 800;
szY = 600;
return 0;
}
void setup() {
size(szX,szY);
println("awww");
}
实际上,您是在调用一个函数来填充int i,就像黑客在运行您想要的所有函数一样,因此必须在设置窗口大小之前计算您想要的任何函数。在setup()之前移动所有这些函数如何?虽然processing通常会抱怨您“混合了静态和活动模式”,但此黑客似乎在processing 2.0.1中起作用:
int i = beforeSetup();
int szX,szY;
int beforeSetup() {
println("look! I am happening before setup()!!");
szX = 800;
szY = 600;
return 0;
}
void setup() {
size(szX,szY);
println("awww");
}
你实际上是在调用一个函数来填充int i,就像黑客在运行你想要的所有函数一样,因此在设置窗口大小之前,你必须计算你想要的任何东西。请用你正在使用的语言标记你的问题。我确实标记了它,它的处理请用你正在使用的语言标记你的问题。我确实标记了它,加工