This site requires JavaScript, please enable it in your browser!
Greenfoot back
PGKCoder
PGKCoder wrote ...

2014/7/11

Images for Food class do not show up in Ant Exercise

PGKCoder PGKCoder

2014/7/11

#
Good evening, I have created my code below which appears to go through. The problem is that the image is never displayed on the canvas. I have even dropped the food manually on the canvas and watched the debug window walk the code and still no food. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
import java.awt.Color;
 
/**
 * A pile of food for which ants will search.
 * A new object consists of 100 crumbs.
 */
public class Food extends Actor
{
    private int nCrumbs;
    private static final int MAX_CRUMBS = 100; // number of crumbs in one food pile
    private static final int IMG_SIZE = 30; // the height and width of a food object
    private static final int CRUMB_SIZE = 3; // the radius of a single crumb
    private GreenfootImage image = null;
     
    /**
     * Create a new food source.
     * It will have a certain number of crumbs
     */
    public Food()
    {
        nCrumbs = MAX_CRUMBS;
        updateImage();
    }
     
    /**
     * Update the image of this food
     */
    public void updateImage()
    {
        // Initialize image variable (already declared) as a new Image object
        GreenfootImage image = new GreenfootImage(IMG_SIZE, IMG_SIZE);
        // declare local variables x and y
        int locX, locY;
         
        // For-loop that counts up to the total number of crumbs in the Food object
        for (int i = 0; i < nCrumbs; i++)
        {
            // Pick a random x for the crumb's center--random, not Gaussian
            locX = Greenfoot.getRandomNumber(IMG_SIZE - 4) + 2;
            // Pick a random y for the crumb's center--random, not Gaussian
            locY = Greenfoot.getRandomNumber(IMG_SIZE - 4) + 2;
            // Draw a solid circle centered at x and y with diameter 3
            Color myColor = new Color(160, 200, 60);
            //image.fillOval(locX, locY, CRUMB_SIZE, CRUMB_SIZE);
            image.setColorAt(locX, locY, myColor);
            setImage(image);
 
        }
    }   
 
    /**
     * Get the image of this food
     */
    public GreenfootImage getImage()
    {
        if (image == null)
        {
            updateImage();
        }
        return image;
    }
     
    /**
     * Set the image
     */
    public void setImage()
    {
        updateImage();
    }
     
    /**
     * Decrease the number of crumbs available
     * One ant takes one crumb from the Food pile
     */
    // This is the method that is invoked when Ants pick up crumbs.
        // decrease the number of crumbs in the pile by one
        // If there are no crumbs left in this object ...
              // removeObject(this); // Why doesn’t this remove the Food object??
        // otherwise....
                // update image
     
    //Added code from original and remarked out the code within the method.
    public void takeSome()
    {
        if (nCrumbs <= 0)
        {
            getWorld().removeObject(this);
        }
        else
        {
        updateImage();
        }
    }
 
 
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // No action code required. Food is inert.
    }   
}
danpost danpost

2014/7/11

#
If I am not mistaken, the program hangs up when you add a Food object into the world. The constructor calls 'updateImage', that calls 'setImage' (lines 65 to 71), that call 'updateImage', that calls 'setImage', etc. The problem is the 'setImage' method (lines 65 to 71). If you remove the 'setImage' method (lines 65 to 71) and let line 48 of the 'updateImage' method call the 'setImage' method that is already in the Actor class and is inherited by the Food class, you will then see it in your world. You can test this by first changing line 48 to the following:
1
super.setImage(image);
This will explicitly call the 'setImage' method of the Actor class.
PGKCoder PGKCoder

2014/7/11

#
HI Danpost, I tried removing the call by commenting out lines 65 to 71. Still no food. I was able to see the food if I comment out the getImage on line 56. However, then the remove food does not appear to work. Any other suggestions?
danpost danpost

2014/7/11

#
Yes. Remove the 'getImage()' method also (lines 53 to 63). And you can remove line 15 as well. There is no need to save the image that is set (it is already being saved in your Actor class, automatically).
danpost danpost

2014/7/11

#
What I meant when I said 'it is already being saved in your Actor class' is this: through the Actor class, by way of inheritance, your actor already has an 'image' field with 'get' and 'set' methods for it.
PGKCoder PGKCoder

2014/7/11

#
Hi Danpost, Thank you for the help. I had to use a very similar solution in the pheromone code as well.
You need to login to post a reply.