By Geertjan-Oracle on Feb 03, 2015
When I met with Michael Kölling, from BlueJ and Greenfoot, when we discussed the relationship between those tools and NetBeans, Michael pointed out that BlueJ users are closer to NetBeans IDE than Greenfoot users. I took copious notes of our discussion, especially on the differences between BlueJ and Greenfoot, since that difference has never been completely clear to me. As Michael explained it to me: "BlueJ visualizes program structures and their execution, while Greenfoot uses visualization as the input to the program."
"Today we're building a game," is the angle teachers with Greenfoot in the classroom use, rather than "today we're going to learn about object orientation". Clearly, the two tools, though built on the same framework, have different approaches, with "motivation first" being the approach applicable to Greenfoot. It doesn't try to be as complete and formal as BlueJ and it's not a general IDE, while BlueJ is for teaching full programming. Anything in Java you can do in BlueJ, including Java 8. On the other hand, Greenfoot is just for two dimensional graphics, i.e., a very specific domain. Many programs you would never make in Greenfoot; it sacrificises generality for one thing only, to raise interest in programming. Then, after that, you potentially, assuming Greenfoot has raised your interest, go somewhere else to become a real programmer.
The underlying reasoning for Greenfoot came about upon the discovery that, increasingly, the first contact people have with programming is before university or technical college. Long before that, you've normally already decided whether programming is "for you". To "catch them early", the insight was that you need to enthuse people in the early stages of their development, i.e., you need to go to schools.
Anyway, all that having been said, let's see what it takes to take a Greenfoot project and work with it in NetBeans. It's less intuitive than BlueJ (as described here) but Michael and I worked through a process that works. My plan is to make a NetBeans plugin for converting a newly opened Greenfoot project in NetBeans to being a project that does step 2, 3, and 4 below automatically:
- Import Java Source Files. Use the 'Java Project with Existing Sources' wizard in NetBeans to import the Java source files created in Greenfoot, same as for BlueJ.
- Configure Classpath. Add the two JARs, greenfoot.jar and bluejcore.jar, from the Greenfoot distribution, to the classpath of the NetBeans project.
- Set NetBeans Project Properties. Set the main method to 'greenfoot.export.GreenfootScenarioMain' and set the working directory to point to the location of the sources. Click to enlarge the image below.
- Set Greenfoot Project Properties. Create a 'standalone.properties' file in the 'default package', with properties such as 'main.class' and 'project.name':
controls.runonce.shortDescription=Call 'act' once for every actor.
controls.run.shortDescription=Run the simulation.
controls.reset.shortDescription=Reset the simulation.
- Figure Out Images and Resources. Images and resources aren't found automatically. Either put all images and resources into the default package or add 'setImage' method to the classes where images are used and do the same for the resources. Possibly something needs to change within the Greenfoot code to find the images and resources automatically when the Greenfoot JAR is used in standalone mode.
The whole project, here the 'Boids' project, looks something like this:
When you run it from NetBeans, you should see this, which is exactly what you see when you run it from Greenfoot. Take note of some of the properties shown above in step 4, which are applied to the viewer, e.g., the buttons you see in the lower part below:
As you can see from the above, a bit of tweaking must be done, which is why I think a small NetBeans plugin that does the above automatically would be helpful, i.e., what's needed is a "Greenfoot" tab in the Project Properties dialog of Java SE projects, which will let the user specify the above items to be added to the project and configure the project automatically to smoothen the transition from Greenfoot to NetBeans.
Something like this: