简体   繁体   English

如何在处理中添加外部库

[英]How to add external libraries in processing

I have been using processing for a while and ran into an issue when I am trying to add an external java library. 我已经使用了一段时间,在尝试添加外部Java库时遇到了问题。 I followed all the steps provided online where I have to access the lib folder(processing-3.3) and add my library inside there. 我遵循了在线提供的所有步骤,必须访问lib文件夹(processing-3.3)并将库添加到其中。 Unfortunately, my method did not work. 不幸的是,我的方法无效。 Another problem I am facing is I don't know how to import this library. 我面临的另一个问题是我不知道如何导入该库。 This is the link to the library and this is the .jar file link . 这是库的链接 ,这是.jar文件链接 Can somebody help? 有人可以帮忙吗?

Regards 问候

That library isn't wrapped as a Processing library and depends on the Apache Commons Math library . 该库没有被包装为处理库,并依赖于Apache Commons Math库 In theory, you could use that in Processing, but it's long winded, as you'd have to: 从理论上讲,您可以在“处理”中使用它,但由于需要:

  1. Download commons-math3-3.6.1-bin.zip 下载commons-math3-3.6.1-bin.zip
  2. Unzip it, and rename commons-math3-3.6.1.jar to commons_math.jar (Processing is restrictive on certain characters in library names) 解压缩它,并将commons-math3-3.6.1.jar重命名为commons_math.jar (处理对库名称中的某些字符有限制)
  3. Make a folder in Documents/Processing/libraries named commons_math then inside of it another folder named library (so you'll have Documents/Processing/libraries/commons_math/library ) Documents / Processing / libraries中创建一个名为commons_math的文件夹,然后在其中另一个名为library的文件夹(这样您将拥有Documents / Processing / libraries / commons_math / library
  4. Copy commons_math.jar in Documents/Processing/libraries/commons_math/library 文件/加工/库/ commons_math /库复制commons_math.jar
  5. Restart Processing 重新开始处理

At this point, you should be able to do this in Processing: Sketch > Import Library... > (Contributed) commons_math and you'll see ALL the packages imported. 此时,您应该可以在处理中进行此操作: “草图”>“导入库...”>((已提供))commons_math ,您将看到所有导入的包。 You should be able to run the sketch, demonstrating you can use the library. 您应该能够运行草图,表明可以使用该库。

Next up is the trilateration library, which isn't build, so you'll need install/setup gradle, build it, then repeat the procedure above for the trilateration library. 接下来是未构建的trilateration库,因此您需要安装/设置gradle并对其进行构建,然后对trilateration库重复上述过程。 An alternative is to create 3 tabs in the sketch(to try and keep it tidy), one for each of trilateration library classes , copy the source code, remote the public modifier prefixing each class (as Processing sketches in the Processing IDE allow a single public class) and remove the package declaration. 另一种方法是在草图中创建3个选项卡(以使其保持整洁),为每个三边测量库类创建一个选项卡,复制源代码,并在每个类的前面加上public修饰符(因为Processing IDE中的Processing草图允许单个公共类)并删除包声明。

Finally you should be able to use this library in Processing...phew! 最后,您应该能够在Processing ... phew中使用该库!

It's tedious, a pain to maintain, etc., etc. ,etc. 繁琐,维护困难等,等等。

I recommend using eclipse instead. 我建议改用eclipse If you want to save time creating a Processing Project, instead of manually copying the Processing core libraries, creating a main class extending PApplet, etc. you can install Proclipsing . 如果要节省创建处理项目的时间,而不是手动复制处理核心库,创建扩展PApplet的主类等,则可以安装Proclipsing It's an eclipse plugin that makes it easy to create Processing Java projects. 这是一个eclipse插件,可以轻松创建Processing Java项目。

Once you're in eclipse and can run a basic sketch, all you need to do is: 一旦您蚀了并且可以运行基本草图,您需要做的是:

  1. Add the Apache Commons Math jar into the projects lib/user folder 将Apache Commons Math jar添加到项目lib / user文件夹中 向项目添加共享
  2. Add the library to the build path 将库添加到构建路径 添加共同点来构建路径
  3. Download/unzip the trilateration library 下载/解压缩三边测量库
  4. From the library folder's src/main/java drag the com folder on top of the eclipse project's src 从库文件夹的src / main / java中,将com文件夹拖到eclipse项目的src顶部 将三边测量源添加到项目
  5. Copy the example code from the library's readme: 从库的自述文件中复制示例代码: 库样本代码
  6. Notice some code is in red, because the classes aren't imported. 请注意,由于未导入类,因此某些代码为红色。 Press CMD+Shift+O (on OSX)/ Ctrl+Shift+O (on Linux/Windows). CMD+Shift+O (在OSX上)/ Ctrl+Shift+O (在Linux / Windows上)。 This will organize imports (and do the job for you). 这将组织导入(并为您完成工作)。 The first suggestion works like a charm: 第一个建议就像一个魅力: 组织进口
  7. Right click the sketch and choose Run As > Java Application: 右键单击草图,然后选择运行方式> Java应用程序: 作为Java应用程序运行

Woo hoo! 呜呜! You can run the sample code now. 您现在可以运行示例代码。 It doesn't look impressive though: nothing is displayed, not even in console, but hey, it compiles :D 它看起来并不令人印象深刻:什么也没有显示,即使在控制台中也没有显示,但是,它可以编译:D

You can easily add a few print statements to see the results: 您可以轻松添加一些打印语句以查看结果:

println(centroid);
println("standardDeviation",standardDeviation);
println("covarianceMatrix",covarianceMatrix);

At this point you can start modifying the code do what you want it to do. 此时,您可以开始修改代码以执行您想要的操作。 Bare in mind most drawing functions in Processing take float arguments and this library works mostly with double so be sure to cast from double to float before drawing. 切记,Processing中的大多数绘图函数都采用float参数,并且该库主要使用double进行工作,因此请确保在绘制之前将doublefloat

If you've been using Processing for a while, the eclipse route sounds doable and there's so many nice features in the IDE to speed up development. 如果您已经使用Processing一段时间了,那么蚀路线听起来是可行的,并且IDE中有很多不错的功能可以加快开发速度。

Is there a faster way ? 有没有更快的方法? Could be: have a look at the M2E which makes it easy to integrate with Maven in eclipse. 可能是:看看M2E ,它可以轻松地在Eclipse中与Maven集成。 The trilateration library has Maven support . 三边测量库具有Maven支持 In theory, you can have Maven do the heavy tedious work of adding the Apache Commons Math, compile and add the trilateration library to your project. 从理论上讲,您可以让Maven做繁琐的工作,添加Apache Commons Math,编译并将trilateration库添加到您的项目中。 This is going further from Processing land and moving more into Java territory, but if you have the time: it's work exploring. 这从“处理土地”进一步发展,并更多地转移到Java领域,但是如果您有时间的话,那就是探索工作。 The more Java you know, the easier it will be to get Processing to do your bidding :) 您知道的Java越多,让Processing进行投标就越容易:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM