Getting Started
Here you will find both basic and advanced tutorials to help you get started using Piccolo2D. All tutorials provide examples in both Java and C#. This section assumes you have read Piccolo2D Patterns and have a basic understanding of the concepts presented there.Piccolo2D.Java - Out of the Box
This tutorial will help you figure out where to begin. It will guide you from downloading Piccolo2D to writing and compiling your first zoomable user interface.
1. Get the Pre-Requisites
In order to write your first Piccolo2D.Java application, you will need to make sure that you have the following:
- Java 2 Platform, Standard Edition (J2SE)
- To run any Piccolo2D.Java code, you must have the Java 2 Platform installed on your machine. If you do not have the Java 2 Platform, you should first download the J2SE installer and install the Java 2 Platform.
- Eclipse IDE
- This tutorial assumes that you are using Eclipse as your development environment. Eclipse is not required to write Piccolo2D.Java applications, but we do recommend using it. Once you have installed the Java 2 Platform, you can download and install eclipse.
- Piccolo2D classes
- Of course, you will need Piccolo2D.Java. You can download the Jar files or the full source code from the .
2. Setting up the Environment
These steps will help you setup the Eclipse environment to begin Piccolo2D.Java development.
Add Piccolo2D to Eclipse
- Open the Eclipse IDE.
- Choose File > New Project, and create a new Java project named "piccolo2d".
- Locate the file
piccolo2d-xxx.zip
, which you downloaded in step 1. Extract the zipped package using WinZip or another compression utility. - Renamed the extracted
Piccolo2D-xxx
folder to the name (my choice waspiccolo2d
) of the eclipse project that you created in step a. - Locate the Eclipse workspace folder. This folder is located inside the folder that holds your Eclipse installation.
- Drag the renamed
piccolo2d
folder into the workspace folder, replacing the existingpiccolo2d
folder in the workspace. - Last of all go into Eclipse and right-click on the
piccolo2d
project and choose "Refresh" from the popup menu. - Piccolo2D's classes should now be visible in eclipse.
3. Building Your First ZUI
These steps will guide you through writing your first zoomable user interface with Piccolo2D.Java.
3.1. Create a new Java project in Eclipse.
Here you will create a new Eclipse java project.
- In Eclipse choose File > New Project and create another "Java Project". This time name the project "Hello World".
3.2. Add References to Piccolo2D
These steps will add references to the Piccolo2D project so that you can use Piccolo2D's classes in your own project.
- Right click on your new "Hello World" project and choose "Properties".
- In the properties panel click on the "Java Build Path" list item on the left.
- Now click on the "Projects" tab and choose the "Add..." button. Check to add the "piccolo2d" project that you created earlier.
- Now your "Hello World" project is setup to use classes from the "piccolo2d" project.
3.3. Subclass JFrame
Next we will subclass the standard Swing class JFrame.
- Select your "Hello World" project and then choose File > New > Class.
- Name the new class "HelloWorldExample".
- Choose the Superclass javax.swing.JFrame.
- Check the box labeled "public static void main(String[] args)" so that a main method will be created for your class.
- Click the Finish button to create you new class.
3.4. Write the Code
Now we will write our first Piccolo2D code.
- At the top of the screen add the following import statements so that we can use the PCanvas and PText classes.
import javax.swing.JFrame; import edu.umd.cs.piccolo.PCanvas; import edu.umd.cs.piccolo.nodes.PText;
- The constructor is used to initialize and wire up piccolo2d, so create one like this:
private HelloWorldExample() { final PCanvas canvas = new PCanvas(); final PText text = new PText("Hello World"); canvas.getLayer().addChild(text); add(canvas); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); setVisible(true); }
Last of all override main method so that a new instance of your "HelloWorldExample" class gets created by main. The final code should look like this:import javax.swing.JFrame; import edu.umd.cs.piccolo.PCanvas; import edu.umd.cs.piccolo.nodes.PText; public class HelloWorldExample extends JFrame { private HelloWorldExample() { final PCanvas canvas = new PCanvas(); final PText text = new PText("Hello World"); canvas.getLayer().addChild(text); add(canvas); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); setVisible(true); } public static void main(String[] args) { new HelloWorldExample(); } }
That's it! You've just written your first zooming application. The first line creates a new PText node and initializes it to the string "Hello World". And, the second line adds the new node to the main layer of the canvas. Zooming and panning are there for free.
3.5. Run the Code
Now let's see Piccolo2D in Action
- Right-click on your "HelloWorldExample" class and choose Run As > Java Application
- You should see the text "Hello World"
- Click anywhere in the window with the left mouse button and drag to pan the camera around.
- Click anywhere in the window with the right mouse button and drag to the right to zoom in on the point. Drag to the left to zoom out.
The reason this works is because the zoom and pan event handlers are installed by default. See the Defining User Interaction tutorial for more details.