Introduzione. GreenFoot è uno strumento software progettato per permettere ai principianti di fare esperienza con laprogrammazione orientata. The Greenfoot Programmers’ Manual is licensed under a. Creative Commons Attribution-Non-Commercial UK: England & Wales License. Contents 1. 2. 3. 4. Opening the Code editor for a class displays all of the programming instructions for the class. Programming instructions are organized into methods in the class.
|Published (Last):||24 December 2016|
|PDF File Size:||4.14 Mb|
|ePub File Size:||2.74 Mb|
|Price:||Free* [*Free Regsitration Required]|
Permette lo sviluppo di applicazioni grafiche in Java. Per esempio, potrebbe incuriosirti la creazione di immagini per gli attori oppure come trattare le collisioni. Greenfoot fornisce 5 classi che dovresti conoscere.
After choosing a name for your new world and clicking Ok, you should see the new world in the class display. Your new world class will have a skeleton that is valid and compiles. You can now compile your scenario, and you will notice that the new world is automatically visible in the world view.
This is one of the built-in features of Greenfoot: In theory, you could have multiple world subclasses in a single Greenfoot scenario. It is non-deterministic, though, which of those world will then be automatically created. Let us have a closer look at the code inside the new world class. The default skeleton looks something like this:. The first things we have to decide are the size of our world and the size of its cells. There are essentially two kinds of worlds: You have probably seen examples of both.
The wombats and ants scenarios are distributed as examples together with Greenfoot. If you have not looked at them, and are not sure what we are discussing here, it would be a good idea to look at them now.
All positioning of actors in the world is in terms of cells.
One result of the fact that actors can only be placed in cells is that worlds with large cells give you fairly course-grained motion. Finding actors in neighbouring cells is also easy.
If you want smoother motion, you should use a smaller cell size.
This can also be combined with large actor rpogrammer. Most of the time, we want our world to have a background image. This relatively easy to do. First, you have to make or find a suitable background image.
There are a number of background images distributed with Greenfoot. There are more background images available in the Greenfoot Image Collection on the Greenfoot web site. The background will, by default, be filled with the image by tiling the image across the world.
The Greenfoot Programmers’ Manual « Programmare con Java
To get smooth looking background, you should use an image whose right edge fits seamlessly to the left, and the bottom to the top. Alternatively, you can use a single image that is large enough to cover the whole world. If you want to paint the background programmatically you can easily do so instead of using an image file.
The world always has a background image. By default as long as we do not specify anything else it is an image that has the same size as the world and is completely transparent. A third option is to combine the two: You can load an image file, then draw onto it, manuwl use the modified file as a background for the world:.
Sometimes, when you have worlds with large grid sizes, you want to make the grid visible.
There is no special function in Greenfoot to do this. This is done simply by having the grid painted on the image that is used for the world background. The effect of this is a visible grid when the tiles are used to fill the world background. This section discusses some of the characteristics of actor classes, and what to consider when writing them.
The following dialogue lets us specify a name and an image for our new class. The name must be a valid Java class name that is: The class image will be the default image for all objects of that class. Every class has an associated image that serves as the default image for all objects of that class. Every object can later alter its own image, so that individual objects of the class may look different.
If objects do not set images explicitly, they will receive the class image.
The image selector in the dialogue shows two groups of images: If you have your own images that you like to use for your class, you have two options:. However, there are some initialisation tasks prigrammer cannot be finished here. The reason is that, at the time the object is constructed, it has not been entered into the world yet.
The order of events is:. So, if we want to do anything as part of our initialisation that needs access to the world such as create other objects in the world, or set our image depending on neighbouring objectsit cannot be done in the constructor. This method is automatically called when the actor has been added to the world. In our example above, by the time the addedToWorld method is called, the object is in the world and has a location. So we can now call our own isNextToFox method which presumably makes use of the world and our location.
The addedToWorld method is called only once. Three attributes of each actor become automatically and immediately visible on screen when you change them. If we change any of these attributes, the appearance of the actor on screen will change. The Actor class has methods to get and set any of these. The first thing progarmmer look at is location changes.
The effect of this code fragment is to move the actor one cell to the right. Once our actor gets to the end of the world it keeps trying to move outside it, but Greenfoot will not let it.
To fix this, we need to add code that checks whether we can move in that direction before actually moving. The location coordinates are the indices of the cells in the world. They should mabual exceed the world size. The 0,0 location is in the top left of the world, and coordinates increase right x and down y.
Here is an example:. The effect is that the object will slowly rotate clockwise. The valid range for the rotation is [ Thus, in the code example above, we check whether we have reached that is: Changing the image will become immediately visible on screen. This code assumes that we have a hasEaten method in our code, and then sets the image accordingly. There are two versions of the setImage method: Generating random numbers in Greenfoot is fairly easy. This class has a method called getRandomNumber.
This example will give us a number in the range [ All three method are used by the various objects in the ants scenario that progrqmmer included in the Greenfoot distribution. It is useful to study this example if you want to learn about images.
We progrqmmer refer to this scenario repeatedly in this section. We discuss all three manaul in turn. There are also tutorial videos available on the Greenfoot website showing how to make background images using these methods. Every class has an associated image. If we write no special image handling code, this is the image that will be used to display objects of this class. In the ants project, the AntHill objects use this technique.
Manaul fixed image is assigned to the class, and all Progdammer objects look the same. In the ants scenario for example, the Ant class uses this method. This way, the image of each individual ant object can dynamically change. This example illustrates a second version of the setImage method: This version saves resources and executes quicker. It is preferrable whenever the images change frequently. The file names of image files are case-sensitive in Greenfoot.
You have to use the correct case or Greenfoot will not manuxl able to find your image. Images can be generated at run-time by code included in your class.
This approach can be useful when there will be many variations of the image, and they can be drawn simply. In the ants scenario the Food class uses this approach so it can show how many pieces of food remain in the pile and the Pheromone class uses it to show its intensity decreasing. This can be a blank image, an image from a file, or the default class image. Geeenfoot image will be completely transparent. Before using any of greejfoot drawing maanual, you need to tell the image what colour you want to draw in.
The easiest way to get a Lrogrammer object is to use one of the pre-defined constants in the Color class, such as Color. Other colours available include white, gray, red, green, blue, yellow and orange.