如何在处理中添加外部库 [英] How to add external libraries in processing

查看:45
本文介绍了如何在处理中添加外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一段时间处理,但在尝试添加外部 Java 库时遇到了问题.我按照在线提供的所有步骤操作,我必须访问 lib 文件夹(processing-3.3)并在其中添加我的库.不幸的是,我的方法没有奏效.我面临的另一个问题是我不知道如何导入这个库.

  • 将库添加到构建路径
  • 下载/解压缩三边测量库
  • 从库文件夹的 src/main/java 将 com 文件夹拖到 eclipse 项目的 src
  • 从库的自述文件中复制示例代码:
  • 注意一些代码是红色的,因为这些类没有被导入.按 CMD+Shift+O(在 OSX 上)/Ctrl+Shift+O(在 Linux/Windows 上).这将组织导入(并为您完成工作).第一个建议就像一个魅力:
  • 右键单击草图并选择运行方式 > Java 应用程序:
  • 呜呜!您现在可以运行示例代码.虽然它看起来并不令人印象深刻:什么都没有显示,甚至在控制台中也不显示,但是嘿,它编译了 :D

    您可以轻松添加一些打印语句来查看结果:

    println(centroid);println("标准偏差",标准偏差);println("covarianceMatrix",covarianceMatrix);

    此时您可以开始修改代码做您想做的事情.请记住,Processing 中的大多数绘图函数都采用 float 参数,并且这个库主要与 double 一起使用,所以一定要从 double 转换为 绘制前浮动.

    如果您已经使用 Processing 一段时间了,那么 eclipse 路线听起来是可行的,而且 IDE 中有很多不错的功能可以加快开发速度.

    有没有更快的方法?可能是:查看 M2E,它可以轻松与 Maven 在 Eclipse 中.三边测量库具有 Maven 支持.理论上,您可以让 Maven 完成繁重繁琐的工作,例如添加 Apache Commons Math、编译三边测量库并将其添加到您的项目中.这是从处理领域走得更远,更多地进入 Java 领域,但如果你有时间:这是工作探索.您了解的 Java 越多,就越容易让 Processing 为您出价 :)

    I have been using processing for a while and ran into an issue when I am trying to add an external java library. I followed all the steps provided online where I have to access the lib folder(processing-3.3) and add my library inside there. 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. Can somebody help?

    Regards

    解决方案

    That library isn't wrapped as a Processing library and depends on the Apache Commons Math library. 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
    2. Unzip it, and rename commons-math3-3.6.1.jar to commons_math.jar (Processing is restrictive on certain characters in library names)
    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)
    4. Copy commons_math.jar in Documents/Processing/libraries/commons_math/library
    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. 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. 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.

    Finally you should be able to use this library in Processing...phew!

    It's tedious, a pain to maintain, etc., etc. ,etc.

    I recommend using eclipse instead. 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. It's an eclipse plugin that makes it easy to create Processing Java projects.

    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
    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
    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). 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:

    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

    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.

    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.

    Is there a faster way ? Could be: have a look at the M2E which makes it easy to integrate with Maven in eclipse. The trilateration library has Maven support. 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. This is going further from Processing land and moving more into Java territory, but if you have the time: it's work exploring. The more Java you know, the easier it will be to get Processing to do your bidding :)

    这篇关于如何在处理中添加外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆