Hello
I would like to know how to change an image to a map in world by clicking on it
thanks in advance
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]); } } } }
MyWorld world = (MyWorld) getWorld(); Greenfoot.setWorld(world);
public void act() { if (!Greenfoot.mouseClicked(this)) return; //(no click -- do nothing) ...
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]); }
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,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) { 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); } } } } }
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); } }
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); }