I have a 5x5 grid of squares. They are all the same color right now. I want to change the color of the square. First, i want to change one square. Then two squares, then 3 squares at one at a time randomly. Any help?
public class Grid extends Actor
{
GreenfootImage img = new GreenfootImage (75, 75);
public void act()
{
//need help here
for(int i = 0; i < 1; i++){
for (int r = 0; r < 5; r++){
for (int t = 0; t < 5; t++){
//img.setColor(new Color(200, 24, 32));
img.drawRect(0, 0, 50, 50);
//img.fillRect(0, 0, 50, 50);
//setImage(img);
}
}
}
if (Greenfoot.mousePressed(this))
{
img.setColor(new Color(200, 24, 32)); // makes a square red
img.fillRect(0, 0, 50, 50);
img.setColor(java.awt.Color.black);
img.drawRect(0, 0, 50, 50);
setImage(img);
}
}
public Grid()
{
img.setColor(java.awt.Color.gray); // makes the squares gray
img.fillRect(0, 0, 50, 50);
img.setColor(java.awt.Color.black); // makes the outline of squares black
img.drawRect(0, 0, 50, 50);
setImage(img);
}
}import greenfoot.*;
public class MyWorld extends World
{
private int level;
public MyWorld() {this(1);}
public MyWorld(int lvl)
{
super(500, 500, 1);
level = lvl;
int[][] grid = new int[5][5];
for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
{
grid[i][j] = 0;
}
for (int num=0; num<level; num++)
{
int fill = Greenfoot.getRandomNumber(grid.length*grid[0].length);
if (grid[fill/grid.length][fill%grid[0].length] == 0)
{
grid[fill/grid.length][fill%grid[0].length] = 1;
}
else num--;
}
GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
{
if (grid[i][j]==1) img.setColor(Color.RED);
else img.setColor(Color.BLACK);
img.fillRect(i*getWidth()/grid.length, j*getHeight()/grid[i].length, getWidth()/grid.length, getHeight()/grid[i].length);
}
setBackground(img);
}
}private static final int HEIGHT = 50;
private static final int WIDTH = 50;
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
for(int i = 0; i < 1; i++){
for (int r = 0; r < 5; r++){
for (int t = 0; t < 5; t++){
Grid g = new Grid();
addObject(g,75 + t*WIDTH,50+ i*HEIGHT*1+r*HEIGHT);
}
}
} import greenfoot.*;
public class MyWorld extends World
{
private int level;
public MyWorld() {this(1);}
public MyWorld(int lvl)
{
super(500, 500, 1);
level = lvl;
int[][] grid = new int[5][5];
for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
{
grid[i][j] = 0;
}
for (int num=0; num<level; num++)
{
int fill = Greenfoot.getRandomNumber(grid.length*grid[0].length);
if (grid[fill/grid.length][fill%grid[0].length] == 0)
{
grid[fill/5][fill%5] = 1;
}
else num--;
}
/*GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
{
if (grid[i][j]==1) img.setColor(Color.RED);
else img.setColor(Color.BLACK);
img.fillRect(i*getWidth()/grid.length, j*getHeight()/grid[i].length, getWidth()/grid.length, getHeight()/grid[i].length);
}
setBackground(img);*/
for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
{
addObject(new Grid(grid[i][j], getWidth()/grid.length, getHeight()/grid[i].length), i*getWidth()/grid.length+getWidth()/grid.length/2, j*getHeight()/grid[i].length+getHeight()/grid[i].length/2);
}
}
}
import greenfoot.*;
public class Grid extends Actor
{
private int flashing;
public Grid(int i, int wid, int hei)
{
flashing = i;
GreenfootImage img = new GreenfootImage(wid, hei);
img.setColor(Color.BLACK);
img.fill();
setImage(img);
}
public void act()
{
if (flashing>0 && flashing<5*50)
{
if (++flashing%25==0)
{
getImage().setColor(flashing%50==25?Color.RED:Color.BLACK);
getImage().fill();
}
}
}
}import greenfoot.*;
public class Grid extends Actor
{
private int flashing;
public Grid(int i, int wid, int hei)
{
flashing = i;
GreenfootImage img = new GreenfootImage(wid, hei);
img.setColor(Color.GRAY);
img.fill();
img.setColor(Color.BLACK);
img.fillRect(1,1,img.getWidth()-2, img.getHeight()-2);
setImage(img);
}
public void act()
{
if (flashing>0 && flashing<5*50)
{
if (++flashing%25==0)
{
getImage().setColor(flashing%50==25?Color.RED:Color.BLACK);
getImage().fillRect(1,1,getImage().getWidth()-2, getImage().getHeight()-2);
}
}
}
}grid[fill/grid.length][fill%grid[0].length] = 1;