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

2012/6/13

help with shapes and mouse

wahaj wahaj

2012/6/13

#
I need help with two different things.I'm trying to make a game similar to the stupid test, the moron test etc. I'll be using shapes and then asking to click on the shapes in a certain order. what is the best way I can go about doing this? For the shapes should I make separate actors for each shape and assign the corresponding image? If I do that then I have this white box around the circle which interferes with my background image. I would use code to make the shapes but I really have no idea how to implement the methods. would this work
public Image(Color color)
{

GreenfootImage image = getImage();
image.drawRect(250,250,10,15);
image.setColor(color);
image.fill();
setImage(image);
}
what I need to be able to do is draw a shape (mostly circles, rectangles, triangles and square which I guess are technically rectangles). I will then use the coordinates of the mouse and the shape to see if what needs to be clicked has been clicked. but as for the mouse is there a better class I can use then MouseInfo or will that do? I havent been able to find a method which allows me to check whether the mouse has been clicked(not pressed). I also have another problem with the transparency of my background.what happens is when I click run the background becomes opaque. here is the code I'm using
public void background()
    {
        setBackground("background.png");
        GreenfootImage background = getBackground();
        background.setTransparency(100);
        
    }
danpost danpost

2012/6/13

#
Did you check the Greenfoot class documentation for the mouseClicked method? Yes, Squares are technically Rectangles; and Circles are technically Ovals; and Triangles, well..., they are technically a Polygon (as are squares and rectangles; but there are not any specific methods to draw them, so you would have to use the drawPolygon method for it. What you probably want to do, is have a 'Shape' class extending Actor. You can send the constructor an integer value to determine what shape to draw on its image. This integer can be saved in an instance object variable to be utilized in the event that 'this' object is clicked on. I am assuming that no more than one of any shape will be displayed at any one time. If that is not the case (if you could have, let's say, two squares showing at one time), then send a second integer variable to the constructor to store as its 'value', and use the first one just for determining the shape to produce. You could add a static integer variable to the class called shapeSelected, and initially set it to -1. Then when an object is clicked, have the value stored in shapeSelected. All the world needs to do is (in its act method) is check 'Shape.shapeSelected' to see if is something other than -1. Compare it to what the clicked should have been on, process the findings, and reset 'Shape.shapeSelected' back to -1. When you create the images with the shapes, just create a new GreenfootImage object of the appropriate size, draw the shape on it (fill the shape if you want), and setImage(imageName).
danpost danpost

2012/6/13

#
I do not believe that you want to make the background with any transparency. For one thing, why? What purpose could there possibly be to warrant a non-opaque background? There would never be anything behind the background to see! Another thing is that unwanted things happen when the background is set transparent. Maybe you are thinking that 100 is the max for transparency? If so, not so. 255 is max value (for opaque).
wahaj wahaj

2012/6/14

#
I was looking in the mouseInfo class so I didnt see that method. anyways my background is made up a bunch of colored shapes just so the background isnt plain. thats why I wanted to change its transparency but I guess I can stick with a plain white background. and yes I will be making several, sometimes up to 10 different shapes at a time. anyways here is what I cam up with. its not complete since I havent added the code to make other shapes but here it is. it works fine except for the color part. when I try to manually insert the object I can seem to get the color parameter right. its the same when I use the worlds constructor to add this object.
public Shape(int shape, int width, int height, int x, int y, Color color)
    {
        GreenfootImage image = new GreenfootImage(width,height);
        // this will draw a rectangle
        if (shape == 1)
        {
            image.drawRect(x, y, width, height);
            image.setColor(color);
            image.fill();
            setImage(image);
        }
        
    }
also aside from the given colors in the Color class, how would I be able to put different shades of colors using the RGB values. do I make another constructor the same as this except it takes RGB values instead of something like BLACK from the the color class
danpost danpost

2012/6/14

#
I am not sure why you are bringing in an 'x' and a 'y'. The 'width' and 'height' can determine the size of the shape. Also, why 'drawRect' and then immediately 'fill', when you can (as least as far as the rectangle and square) just 'fill' (just remove line 7). What exactly do you mean by 'can't seem to get the color parameter right'?
wahaj wahaj

2012/6/14

#
dont I have to create the shape before I can fill it. I'll be using this constructor to make ovals and rectangles. I accidently missed out on the comments. the x and y are where the object is to be placed though now that I think about it the addObject method takes care of it in the world.but I need those to implement drawRect as they are parameters for that method. as for the color parameter if I go something like addObject(new Shape(2,50,50, 250, 250, BLACK), 250,250); or addObject(new Shape(2,50,50, 250, 250,Color.BLACK), 250,250); it doesnt work. another thing when I use image.fillOval(x,y,width,height); setImage(image); or image.drawOval(x,y,width,height); image.fill(); setImage(image); I dont get the desired effect that is only the oval is filled and not the rectangle it is bounded by
danpost danpost

2012/6/14

#
The parameters for the fillOval, fillRect and fillPolygon methods are all based on 0,0 being the top-left corner pixel of the image (not the screen). Your method calls should be as follows: Square: image.fill(); Rectangle: same as Square. Circle: image.fillOval(0, 0, width - 1, height - 1); Triangle: int xs = {0, width / 2, width - 1}; int ys = {height - 1, 0, height - 1}; image.fillPolygon(xs, ys, 3);
wahaj wahaj

2012/6/14

#
great. my program now works like a charm. though I still havent figured out how to solve the color problem I mentioned in my above post. I could put the code for the color in the parameter but then how do I pass that info onto the method so the correct color can be selected. I want to use the RGB values instead of the set ones like BLACK
danpost danpost

2012/6/14

#
Just put in the calling statement: new Shape(1, 30, 30, new Color(redValue, greenValue, blueValue, alphaValue))
danpost danpost

2012/6/16

#
I do not believe that the background image of the world utilizes its transparency value to display the image. But, I do have a way to make it appear to. Create a new class, called 'Tint'. In this new class, create a GreenfootImage of size(1, 1), set the color to 'new Color(255, 255, 255, 100)', fill the image with its new drawing color using 'fill()', and set this image as the image for the 'Tint' actor. Add the following method to the new class:
public void addedToWorld(World world)
{
    GreenfootImage image = getImage();
    image.scale(world.getWidth(), world.getHeight());
    setImage(image);
}
In your world class, before adding any other actors, add a new Tint object to the middle of the world.
You need to login to post a reply.