GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing happycoding.io — How to Use Processing Libraries Libraries
Previous:
Next:
Libraries
Now you know the fundamentals of coding in Processing. You know how to write code, and you know how to use objects and create your own classes. For example, you know how to use the Now that you know all of that, you can use software libraries to expand what Processing can do. What’s a library?A library is a bunch of code that other people have already written, which you can add to your own sketch to expand Processing’s functionality. Libraries give you access to new classes that contain functions that let you run the library’s code without really worrying about exactly how it all works. To use a library, you have to add it to your sketch. There are a few ways to do that: Library Contributions ManagerThe easiest way to find a library and add it to your sketch is using the Contribution Manager that comes with the Processing editor. From the Processing editor, go to the
This brings up the Contribution Manager:
This shows you a list of libraries that are specifically designed to be used in Processing. Find the library you want, then click the You aren’t done yet! That step downloads the library onto your computer, but you still have to add it to your sketch. Go back to the Usually you’ll see some Adding a Library JarThe Contribution Manager lists a few of the libraries that can be used in Processing, but there are many others out there. In fact, since Processing is built using Java, you can use any Java library in Processing! Usually these libraries are packaged as a When you have the For example, I’m going to download the Handy library, which draws shapes in a hand-drawn style. Downloading the Handy library gives me a file named Now that I have the
Import StatementsThe Contributions Manager handles adding import statements for you, but if you add a library jar manually, you have to add the import statements manually. If you aren’t familiar with import statements, here’s a brief introduction: Libraries organize their classes into packages, which is like a directory that holds classes. To use a library class, you have to do two things: first you have to add the library to your sketch (which you do via the Contributions Manager or by dragging a There are two ways to import a class: you can use a wildcard import, which imports all of the classes in a package, or you can use an explicit import, which imports one specific class. A wildcard import looks like this, and it’s what the Contributions Manager does: An explicit import looks like this: I personally prefer using explicit import statements because it makes it more obvious exactly what classes you’re using, but unless you’re using two libraries that have classes with the same name, either approach is fine. To figure out what classes are located in which packages, you’ll have to consult the documentation for your specific library. For example, here is the documentation for the Handy library. This is a lot like the Processing reference, except it lists the classes and functions that the library gives us. Looking through the Handy documentation, you can find the Now you can use the Using a LibraryNow that you have a library added to your sketch and its classes imported, you can use that library. Every library is different, so you have to consult your library’s documentation to know exactly what classes you can use, and what functions those classes contain. For example, this tutorial contains examples on how to use the Handy library, including this one: This code draws a rectangle, but notice that it calls the
Here’s a more complex example that uses he Handy library:
This code calls various functions in the Handy library to draw four rectangles with different styles. To understand this code, you don’t need to worry about how Handy works internally. That’s the whole point of using libraries: they let you do cool stuff without writing all of the code yourself! Learning More About LibrariesThis might seem a little overwhelming: how do you know which library to use? How do you know how to use it? But don’t worry: you already know how to figure that out. By now you’re probably pretty accustomed to looking stuff up in the Processing reference, reading example code, and trying stuff out. That’s exactly what you do with libraries: consult their documentation, read example code, and then try something out. There are a ton of libraries out there: libraries for computer vision, image processing, artificial intelligence, physics simulations, robotics, you name it. Using libraries really opens up the possibilities of what you can do with code, so don’t be afraid to get creative! Homework
Previous:
Next:
Libraries Examples |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |