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

2021/11/9

BINARY

1
2
ronald ronald

2021/11/9

#
Hello I would like to know how to change an image to a map in world by clicking on it thanks in advance
Aaron-aid Aaron-aid

2021/11/9

#
if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new world()); }
Aaron-aid Aaron-aid

2021/11/9

#
or try if (Greenfoot.mouseClicked(this)) { setImage("image.png"); }
ronald ronald

2021/11/9

#
I forgot to tell you that I use an array of images
Aaron-aid Aaron-aid

2021/11/9

#
oh, im currently struggling with arrays too :c
ronald ronald

2021/11/10

#
    GreenfootImage [] images = {new GreenfootImage("button-green.png"), new GreenfootImage("button-blue.png")};
                                                                  
    int map[][] = ((MyWorld) getWorld()).map;
    
    public void act()
    {
        for (int i = 0; i < images.length; i++)
        {
            for (int j = 0; j < images.length; j++)
            {
                if (Greenfoot.mouseClicked(this) && map[j][i] == 0)
                {
                    MyWorld world = (MyWorld) getWorld();                   
                    Greenfoot.setWorld(world);
                    world.level();
                    setImage(images[0]);   
                }
                else if (Greenfoot.mouseClicked(this) && map[j][i] == 1)
                {    
                    MyWorld world = (MyWorld) getWorld();                   
                    Greenfoot.setWorld(world);
                    world.level();
                    setImage(images[1]);
                }
            }
        }
    }
hi I don't know if you understand my method in actor and I don't know if this is the right method to access a variable and method in world to avoid repeating I'm trying to transform images 0 and 1 into other images all in an array in actor Thank you for your help
ronald ronald

2021/11/10

#
maybe I'm wrong as the images are in actor, it may be necessary to access the actor classes, oops I'm a little confused
danpost danpost

2021/11/10

#
@ronald, * line 4 will fail as getWorld will return null during the assignment of the field. * these lines (used twice)
MyWorld world = (MyWorld) getWorld();                   
Greenfoot.setWorld(world);
do absolutely nothing. You are getting the currently active world and telling greenfoot to make it the active world (which it already is). * you should probably check for mouse click first:
public void act()
{
    if (!Greenfoot.mouseClicked(this)) return; //(no click -- do nothing)
    ...
* not sure why you included loops at all. Only one image will eventually be assigned to the actor.
danpost danpost

2021/11/10

#
GreenfootImage [] images =
{
    new GreenfootImage("button-green.png"),
     new GreenfootImage("button-blue.png")
};

public void act()
{
    if (Greenfoot.mouseClicked(this)) setImage(images[getImage() == images[1] ? 0 : 1]);
}
ronald ronald

2021/11/10

#
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    int map[][] = {
                   {0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0},
                   {1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,1,1,0,0,1,0},
                   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,1,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0},
                   {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0},
                   {1,1,0,1,0,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,1,0},
                   {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                   {1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {1,1,0,0,0,0,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0},
                   {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
                   {1,1,0,1,0,0,0,0,1,1,0,0,0,1,1,1,0,0,1,1,0,1,0},
                   {1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0},
                   {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1},
                   {1,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1},
                   {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0},
                   {0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0},
                   {0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0},
                   {0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0},
                   {0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0},
                   {0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0},
                   {0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0},
                   {0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0},
                   {0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0},
                   {0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0},
                   {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},
                   {0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
                   {0,0,1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0},
                   {0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,0,0,0,0,1,0,1,1,1,0,1,0,0,1,0,1,1,0,1,1},
                   {0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0,1,1,1,1,1,1},
                   {1,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,1,1,0,1,1,1},
                   {0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,1,0,1,1},
                   {0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,1,1,1,1},
                   {0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0},
                   {0,0,1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
                   {0,0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1},
                   {0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0},
                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
                   {0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
                   {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},
                   {0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0,0},
                   {0,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0},
                   {0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0},
                   {0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0},
                   {0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0},
                   {0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0},
                   {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0},
                   {0,1,1,1,1,0,0,1,1,1,1,1,0,1,0,0,1,1,1,1,0,0,0},
                  };
    
    public MyWorld()
    {    
        super(900, 2700, 1);
        level();
    }
    
    public void level()
    {
        for (int i = 0; i < 23; i++)
        {
            for (int j = 0; j < 73; j++)
            {
                if (map[j][i] == 0)
                { 
                    addObject(new Binary0(), 30+i*35, 30+j*35);
                }
            }
        }
        
        for (int i = 0; i < 23; i++)
        {
            for (int j = 0; j < 73; j++)
            {
                if (map[j][i] == 1)
                { 
                    addObject(new Binary1(), 30+i*35, 30+j*35);
                }
            }
        }
    }
    
}
I give you the world code
danpost danpost

2021/11/10

#
Use just one Actor subclass for Binary objects. Or, don't use any at all and just draw onto the background image. You can still determine where a click occurs and change the map and the image:
public void drawBinary(int x, int y)
{
    GreenfootImage img = new GreenfootImage(" "+map(x, y)+" ", 24, Color.WHITE, Color.BLACK);
    getBackground().drawImage(img, 30+35*x, 30+35*y);
}

public MyWorld()
{
    super(900, 2700, 1);
    for (int i=0; i<23; i++) for (int j=0; j<73; j++) drawBinary(i, j);
}

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        int x = (mi.getX()-30)/35, y = (mi.getY()-30)/35;
        map[y][x] = (map[y][x]+1)%2;
        drawBinary(x, y);
    }
}
ronald ronald

2021/11/10

#
well uh I'm having a little trouble with your code I draw in the world class if I don't use images (numero-0.png, numero-1png) in the actor class and the images (button-blue.png, button-green.png) go in the world class and to replace the binaries with the blue and green buttons i use getbackground with greenfoot.mouseclicked ??? map (x, y) in the drawBinary method is an undeclared method
ronald ronald

2021/11/10

#
ok i found the error it is not map (x, y) but map array y x in the drawBinary method There
ronald ronald

2021/11/10

#
interesting your code the 1 turn into 0 and the 0 into 1 do you know what the drawing in 0 and 1 represents? have you heard of the arecibo plaque? not easy to recognize the design of the arecibo plaque d or the idea of replacing the 0 and 1 with green or blue buttons or white and black rectangles
danpost danpost

2021/11/10

#
ronald wrote...
interesting your code the 1 turn into 0 and the 0 into 1 do you know what the drawing in 0 and 1 represents? have you heard of the arecibo plaque? not easy to recognize the design of the arecibo plaque d or the idea of replacing the 0 and 1 with green or blue buttons or white and black rectangles
You can make the 0 and 1 represent any pair of things. The only difference is what is drawn onto the background of the world. For example:
private GreenfootImage [] images =
{
    new GreenfootImage("button-green.png"),
    new GreenfootImage("button-blue.png")
};
 
private void drawBinary(int x, int y)
{
    getBackground().drawImage(images[map[y][x]], 30+35*x, 30+35*y);
}
There are more replies on the next page.
1
2