本文介绍了将处理应用程序组合成1个大型可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了这3个应用程序。他们每个人都从用户输入收集数据并创建一个csv文件。其中2个依赖于LEAP运动控制器。我试图想出一种方法将它们组合成一个程序。关于如何做到这一点的任何提示?我可以将它们封装成类,然后让主程序分别调用它们吗?或者我是否需要进入并重新组合一起工作?每个应用程序使用draw()与所有类型的标志非常不同,noLoop()和Loop()调用在必要时暂停以收集数据,因此将所有重写为1都很困难。此外,代码目前的形式非常草率。我基本上已经砍掉并拉出了randoms位以让每个人做我需要做的事情。每一个都是混乱,所以将所有组合成一个程序似乎是一个真正的噩梦。

I've built these 3 applications. Each of them collects data from user input and creates a csv file. 2 of them rely on the LEAP motion controller. I am trying to figure out a way to combine them all into one program. Any tips on how I could go about doing this? Can I just enclose them into like a class then have the main program call each one individually? Or would I need to go in and rewrtie everything to work together? Each application makes very different use of the draw() with all types of flags and noLoop() and Loop() calls to pause as necessary to collect data so rewrite all as 1 would be difficult. Furthermore the code is extremely sloppy in its current form. I basically hacked away and pulled together randoms bits to get each one to do what I needed to do. Each one is a mess so combining all into one program seems like it would be a real nightmare.

无论如何包装它们以便一端结束另一个自动打开?有点像在网页中你可以如何链接或在关闭后自动打开另一个页面?每个应用程序只需要运行一次,所以我不必担心来回。

Is there anyway to package them so as one ends the other automatically opens? Kinda like how in a webpage you can just link or automatically open another page after one closes? Each application just needs to run once so I don't have to worry about going back and forth.

最终我将构建一个可实时更新的可视化与这些应用程序的数据。我想我可以为该部分构建一个Web应用程序,因为我希望能够从多个位置/平台查看可视化。

Eventually I'm going to build a visualization that updates in real time with the data from these applications. I think I may build a web application for that part because I want the visualization to be able to be viewed from multiple locations/platforms.

我已经包含了一个dropbox链接所有的代码。有人建议使用Swing接口将应用程序直接嵌入到Java中。现在我正在阅读我认为正在解释的怎么去做这个。我之前从未使用过Java Swing,所以我现在迷失了....

I've included a dropbox link to all the code. Someone suggested embedding the application directly into Java using a Swing Interface. Now I'm reading this which I think is explaining how to go about doing this. I've never used Java Swing before so I am currently lost....

推荐答案

关于实施真的有很多问题...你想要一个大应用程序,还是你想要它们作为不同的应用程序?您想运行三个已完成的应用程序,还是要组合源代码?您也可以将它们变成库...关于您在其他问题中的评论(),是的,您当然可以创建另一个PApplet来存储您的应用程序。我稍微修改了一下这个例子来展示。

There are quite a few questions on the implementation really... Do you want just one big app or do you also want them as different ones? Do you want to run the three finished apps, or do you want to combine the source code? You can also make them into libraries... Regarding your comment in the other question (Create more than one window of a single sketch in Processing), yes of course you can create another PApplet to store your application. I modified the example a bit to showcase.

在这里,我创建了两个草图RedBG和BlueBG:

Here, I created two sketches RedBG and BlueBG like this:

int counter = 0;
public void setup() {
  size(400, 400);
  counter = 0;
}
public void draw() {
  background(255, counter, counter);
  fill(255-counter);
  text(String.valueOf(counter), width*0.5, height*0.5, width, height);
  counter++;
}

这是红色的,蓝色的有 background(counter,counter,255); ,它们都可以作为正确的草图。然后我拿了两个代码并将它们放在控制器草图中的不同选项卡中,并将它们包装成这样的类:

This is the red one, the blue one has background(counter, counter, 255);, and they both work as proper sketches. Then I took the two codes and placed them in different tabs in a controller sketch, and wrapped them into classes like this:

public class RedBG extends PApplet {
  int counter = 0;
  public void setup() {
    size(400, 400);
    counter = 0;
  }
  public void draw() {
    background(255, counter, counter);
    fill(255-counter);
    text(String.valueOf(counter), width*0.5, height*0.5, width, height);
    counter++;
  }
}

然后,控制器类只是对我的修改在回答。这是它的代码:

Then, the controller class is just a modification of my answer in here. Here's its code:

import javax.swing.*;
PApplet r, b;
PFrame rf, bf;
String nextWindow = "red";
int controllerCounter = 200;
String control = "preparing...";
void setup() {
  size(400, 400);
  r = new RedBG();
  b = new BlueBG();
  frame.setTitle("controller");
  fill(0);
}
void draw() {
  background(255);
  if (controllerCounter < 1) {
    switchState();
    controllerCounter = 200;
  }
  control = null;
  control = "Launching " + nextWindow + " in: " + controllerCounter;
  text(control, width*0.5, height*0.5, width, height);
  controllerCounter--;
}
void switchState() {
  if (nextWindow == null) {
    stopApplet(b);
    nextWindow = "red";
  }
  else if (nextWindow.equals("red")) {
    startApplet(r);
    nextWindow = "blue";
  }
  else if (nextWindow.equals("blue")) {
    stopApplet(r);
    startApplet(b);
    nextWindow = null;
  }
}
void startApplet(final PApplet p) {
  if (p == null) return;
  final PFrame f = new PFrame(p);
  p.frame = f;
  f.setTitle(p.getClass() + " window");
  //this thread is only necessary if you are restarting the PApplets
  Thread t = new Thread(new Runnable() {
    public void run() {
      p.setup();
    }
  });
  t.run();
}
void stopApplet(PApplet p) {
  if (p == null || p.frame == null) return;
  p.dispose();
  p.frame.dispose();
}
public class PFrame extends JFrame {
  public PFrame(PApplet p) {
    setSize(400, 400);
    add(p);
    p.init();
    show();
  }
}

来自草图的两个类是:

public class BlueBG extends PApplet {
  int counter = 0;
  public void setup() {
    size(400, 400);
    counter = 0;
  }
  public void draw() {
    background(counter, counter, 255);
    fill(255-counter);
    text(String.valueOf(counter), width*0.5, height*0.5, width, height);
    counter++;
  }
}

和:

public class RedBG extends PApplet {
  int counter = 0;
  public void setup() {
    size(400, 400);
    counter = 0;
  }
  public void draw() {
    background(255, counter, counter);
    fill(255-counter);
    text(String.valueOf(counter), width*0.5, height*0.5, width, height);
    counter++;
  }
}

简而言之,从三幅草图中获取所有代码(所有选项卡),将它们放在控制器草图的新选项卡中,并使用扩展PApplet的类进行换行。

In short, take all your code from the three sketches (all tabs), throw them in a new tab in the controller sketch, and wrap with a class extending PApplet.

不幸的是,除非修改代码,否则不能在控制器草图中包含三个草图的选项卡。在随后的修改后的样本中,只有lala1()和lala3()位于蓝色窗口中。 lala2()位于控制器窗口中...

Unfortunately you can't have the tabs of your three sketches in the controller sketch, unless you modify your code. In the modified sample that follows, only lala1() and lala3() are in the blue window. lala2() is in the controller window...

public class BlueBG extends PApplet {
  int counter = 0;
  public void setup() {
    size(400, 400);
    counter = 0;
  }
  public void draw() {
    background(counter, counter, 255);
    fill(255-counter);
    text(String.valueOf(counter), width*0.5, height*0.5, width, height);
    counter++;
    lala1();
    lala2();
    lala3(this);
  }
  public void lala1() {
    fill(255, 255, 0);
    ellipse(100, 100, 100, 100);
  }
}
public void lala2() {
  fill(255, 0, 255);
  ellipse(150, 150, 100, 100);
}
public void lala3(PApplet p) {
  p.fill(0, 255, 255);
  p.ellipse(200, 200, 100, 100);
}

最后但并非最不重要的是,有时代码会抛出NullPointerException和类似奇怪的错误消息在后台方法调用中缺少pushMatrix()以与popMatrix()一起使用。这是由 startApplet()方法中的 setup()调用引起的,这是一个并发问题,因此需要更深入的思考和知识......作为一个临时措施,我从一个线程调用 setup() ...如果你不打算重复这个过程,那么整个线程的东西是没有必要的,你不需要每次都 setup()

Last but not least, sometimes the code will throw NullPointerException and weird error messages like "missing a pushMatrix() to go with that popMatrix()" on a background method call. This is caused by the setup() call in the startApplet() method and it is an issue of concurrency thus needs deeper thinking and knowledge... As a temporary measure I made it call setup() from a thread... If you are not going to repeat the process, then the whole thread thing is not necessary, and you don't need to setup() every time.!

PS这是一个hacky-slashy解决方案...我的建议是将您的草图合并为一个并正确执行...

P.S. This is a hacky-slashy solution... My suggestion is to merge your sketches into one and do it properly...

这篇关于将处理应用程序组合成1个大型可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:12