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

2011/10/12

How to use fillOval?

rgadue rgadue

2011/10/12

#
For the question below.. * 3a. The updateImage() method creates a Greenfoot image object that * contains a certain number of crumbs (as specified by nCrumbs) at random * locations. Choose x and y values so that all of the crumbs appear in the * image. None of them should be cropped by the border of the image; each * crumb is a circle with a diameter of three. (Hint: Use the fillOval() method to create a circle.) I came up with...
public void updateImage() 
    {
        GreenfootImage getImage;  // Initialize image variable (already declared) as a new Image object
        int locX;
        int locY;  // declare local variables x and y
        
        for (int i = 0; i < nCrumbs; i++) {  // For-loop that counts up to the total number of crumbs in the Food object
            locX = Greenfoot.getRandomNumber(100);  // Pick a random x for the crumb's center--random, not Gaussian
            locY = Greenfoot.getRandomNumber(100);  // Pick a random y for the crumb's center--random, not Gaussian
            // Draw a solid circle centered at x and y with diameter 3
        }
        setImage(image);
    } 
Not sure if this is on the right track and not sure how to draw the circle with filloval.
bourne bourne

2011/10/12

#
First, you declared a GreenfootImage called "getImage" but do not initialize it Furthermore, at the end where you used the setImage method, you use "image" instead of the "getImage" GreenfootImage that you declared previously. Also I would suggest setting the locX and locY values to a random value according to the size of the image you are going to draw to. Recall a GreenfootImage's getWidth() and getHeight() methods. Considering "None of them should be cropped by the border of the image", you would want to exclude possible centered locations for the oval around the border: 1 + Greenfoot.getRandomNumber(getImage.getWidth() - 2) ** A positive 1 (radius of circle) away from one side and - 2 : 2 X radius of circle in account for both sides Now for your question about fillOval, why did you not check out the GreenfootImage api: void fillOval(int x, int y, int width, int height) The x and y parameters are your locX and locY values. And because you want it to be centered over these locations, subtract from the locX and locY, the radius of the desired oval (1). And finally the width and height is your 3.
keiko1dog keiko1dog

2011/11/24

#
Got a question to this same thing except mine is .... The Instructor took out all the code and replaced 90% of it. We have to replace the rest. I know I don't get all of this but I do get how to initialize a variable but no matter how I do it I keep throwing an error: cannot find symbol- variable ..... meaning whatever I put I get the same error!! I'm screaming over here!! This is what is written: import greenfoot.*; import java.util.Random; public class Food extends Actor { private int nCrumbs; private static final int MAX_CRUMBS = 100; private static final int IMG_SIZE =30; private static final int CRUMB_SIZE =3; private GreenfootImage image = null; public void updateImage() { // Initialize image variable(already declared) as a new Image object // declare local variable x and y // for loop that counts up to the total number of the crumbs in the Food object // pick a random x for the crumb's center -- not Gaussian // pick a random y for the crumb's center -- not Gaussian // Draw a solid circle centered at x and y with diameter 3 Get the image of this food public GreenfootImage getImage() { if (image = null) { updateImage(); } return image; This is what I have: public void updateImage() { GreenfootImage image = new getImage(); int locX; int locY; for (i = 0; i < nCrumbs; i++) { locX =1 + Greenfoot.getRandomNumber(getImage(getWidth()); locY =1 + Greenfoot.getRandomNumber(getImage(getHeight()); setImage(getImage); } The GreenfootImage image variable won't initilize I think I have been sitting at the computer to long and its something i'm just clearly overlooking and i'm gonna be real upset when I finally figure it out! but anyone who can help I would sure appreciate it
AwesomeNameGuy AwesomeNameGuy

2011/11/24

#
why does getImage() call updateimage() to get the image, and then updateimage call getImage() to get the image? If the image is null, when does it ever become not null?
keiko1dog keiko1dog

2011/11/24

#
That was my question! I think I am suppose to take that null out of there when I initialize that variable. The code for public GreenfootImage getImage() is his code. It also has a null in it that I think needs to be removed. Any thoughts ??
AwesomeNameGuy AwesomeNameGuy

2011/11/24

#
For it to be not null, you have to assign it to an object. So make a new GreenfootImage object with the keyword new. getImage() is in his code? well he should have wrote if (image == null) instead of using the single =. Now for your method when you call getImage() you are returning a null reference as of right now, so instead why not just make a new GreenfootImage object by saying new GreenfootImage(IMG_SIZE,IMG_SIZE) instead of calling new getImg() (I didn't even know you can use the new keyword when you call a method). Also, notice you are redefining the variable image in your method, by saying GreenfootImage image, don't do that, that is also going to cause errors the way your code is written. Just say image = new GreenfootImage(IMG_SIZE,IMG_SIZE). That way you aren't redefining it. Then use fillOval to draw the oval in the for loop,(look it up in the api), but I would also consider taking setImage() out of the for loop, I don't think it needs to be there.
keiko1dog keiko1dog

2011/11/24

#
I finally know I'm not crazy!!! That GreenfootImage image is his also. So I should just scratch all that and new GreenfootImage(IMG_SIZE,IMG_SIZE) and just use getImage at the end of the loop
keiko1dog keiko1dog

2011/11/24

#
Ok I made a mistake in the public GreenfootImage getImage() it does have if (image == null). But I tried to put in under public void updateImage() image = new GreenfootImage(IMG_SIZE,IMG_SIZE) and that didn't work either so I'm starting over. If you have any more suggestions that would be great! Thanks for the help!
danpost danpost

2011/11/24

#
The first step ( Initialize image variable(already declared) as a new Image object) involves setting a variable of type GreenfootImage to be a new GreenfootImage of height IMG_SIZE and width IMG_SIZE. The name of the variable was already declared as 'image' and was already declared as a type of GreenfootImage. Pay special attention to what I have bolded, and try to understand why those particular words were bolded.
keiko1dog keiko1dog

2011/11/24

#
Thank you so much for the explanations!!!!! they helped me understand and figure it out!!! Thanks again AwesomeNameGuy and DanPost!! I really appreciate your help!
You need to login to post a reply.