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

2021/11/14

PAINT GRID COLOR

1
2
ronald ronald

2021/11/14

#
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    int width = 900;
    int height = 600;
    
    int rows = 25;
    int cols = 25;
    
    public Background()
    {    
        super(900, 600, 1);       
    }
    
    public void act()
    {
        GreenfootImage gfim = new GreenfootImage(width, height);
        int rectWidth = getWidth() / cols;
        int rectHeight = getHeight() / rows;
        
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (Greenfoot.mouseClicked(this))
                {
                    MouseInfo mi = Greenfoot.getMouseInfo();
                    int x = mi.getX();
                    int y = mi.getY();
                    
                    addObject(new Blue(), x+rectWidth, y+rectHeight);
                    //gfim.setColor(Color.BLUE);
                    //gfim.fillRect(x, y, rectWidth, rectHeight); 
                }
            }
        }
        drawGrid();
    }
    
    public void drawGrid()
    {
        GreenfootImage grid = new GreenfootImage(width, height);
        grid.setColor(Color.BLUE);
        for (int i = 0; i <= width; i+=25)
        {
            for (int j = 0;  j <= height; j+=25)
            {
                grid.drawRect(i, j, 25, 25);
            }
        }
        setBackground(grid);
    }
}
Hello i am creating a grid where i can insert blue squares, as the squares move when i click i am wondering if an array of columns and rows would fit them efficiently between columns and rows I'm trying to do with an image to insert and a color like setColor Thank you for your help
danpost danpost

2021/11/14

#
ronald wrote...
<< Code Omitted >> i am creating a grid where i can insert blue squares, as the squares move when i click i am wondering if an array of columns and rows would fit them efficiently between columns and rows I'm trying to do with an image to insert and a color like setColor
Try this:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        int x = (mi.getX()/cols)*cols+getWidth()/cols/2;
        int y = (mi.getY()/rows)*rows+getHeight()/rows/2;
        addObject(new Blue(), x, y);
    }
}
Cut line 42 and insert it at line 18 (after the super call).
ronald ronald

2021/11/14

#
thank you danpost for this formula to effectively insert the blue square between the columns and rows I added -5 to int x = mi.getX () on the other hand drawGrid (), I left in the act method because it does not work with the constructor I was wondering what it would be like if it was with a color like setColor and not an image can you clarify? thank you
danpost danpost

2021/11/14

#
ronald wrote...
I was wondering what it would be like if it was with a color like setColor and not an image can you clarify?
Then you would probably use something like the following:
getBackground().setColor(Color.BLUE);
getBackground().fillRect(x, y, 25, 25);
with x and y being without the halved addition at the ends.
danpost danpost

2021/11/14

#
Using an array might be something like this:
import greenfoot.*;

public class MyWorld extends World
{
    private static final Color[] COLOR = { Color.WHITE, Color.BLUE };
    private static final int ROWS = 25, COLS = 25;
    
    private int[][] grid = new int[COLS][ROWS];
    private int wide; // rectWidth
    private int high; // rectHeight
    
    public MyWorld()
    {
        super(900, 600, 1);
        wide = getWidth()/COLS;
        high = getHeight()/ROWS;
        for (int y=0; y<ROWS; y++) for (int x=0; x<COLS; x++) drawRect(x, y);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mi = Greenfoot.getMouseInfo();
            int x = mi.getX()/wide;
            int y = mi.getY()/high;
            grid[y][x] = (grid[y][x]+1)%COLOR.length;
            drawRect(x, y);
        }
    }
    
    private void drawRect(int x, int y)
    {
        GreenfootImage bg = getBackground();
        bg.setColor(COLOR[1]);
        bg.drawRect(x*wide, y*high, wide, high);
        bg.setColor(COLOR[grid[y][x]]);
        bg.fillRect(x*wide+1, y*high+1, wide-1, high-1);
    }
}
ronald ronald

2021/11/14

#
danpost wrote...
Cut line 42 and insert it at line 18 (after the super call).
thanks for the code I tried several times drawGrid () in the constructor, it works but when I do reset, the colors stay and don't go is this normal?
ronald ronald

2021/11/14

#
for getBackground (). fillRect (x, y, 25, 25); you mean without the formula of
danpost wrote...
int x = (mi.getX()/cols)*cols+getWidth()/cols/2; int y = (mi.getY()/rows)*rows+getHeight()/rows/2;
danpost danpost

2021/11/14

#
ronald wrote...
for getBackground (). fillRect (x, y, 25, 25); you mean without the formula
That formula was flawed anyway. Should have been:
int x = (mi.getX()/rectWidth)*rectWidth+rectWidth/2;
int y = (mi.getY()/rectHeight)*rectHeight+rectHeight/2;
Also, it should have been:
getBackground().fillRect(x, y, rectWidth, rectHeight);
danpost danpost

2021/11/14

#
ronald wrote...
I tried several times drawGrid () in the constructor, it works but when I do reset, the colors stay and don't go is this normal?
Show current class codes (normal? code only does what the compiler believes it is coded to do).
ronald ronald

2021/11/14

#
OK that's why fillrect doesn't work also I give you the codes thank you
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    int width = 900;
    int height = 600;
    
    int rows = 25;
    int cols = 25;
    
    public Background()
    {    
        super(900, 600, 1); 
        //drawGrid();
    }
    
    public void act()
    {       
        if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mi = Greenfoot.getMouseInfo();
            int x = (mi.getX()/cols) * cols+getWidth()/cols/2 - 5;
            int y = (mi.getY()/rows) * rows+getHeight()/rows/2;              
            addObject(new Blue(), x, y);
            
            String key = Greenfoot.getKey();
            if ("c".equals(key))    addObject(new Cyan(), x, y);
            if ("g".equals(key))    addObject(new Green(), x, y);
            if ("o".equals(key))    addObject(new Orange(), x, y);
            if ("p".equals(key))    addObject(new Pink(), x, y);
            if ("r".equals(key))    addObject(new Red(), x, y);     
        }   
        drawGrid();
    }
    
    public void drawGrid()
    {
        GreenfootImage grid = new GreenfootImage(width, height);
        grid.setColor(Color.BLACK);
        for (int i = 0; i <= width; i+=25)
        {
            for (int j = 0;  j <= height; j+=25)
            {
                grid.drawRect(i, j, 25, 25);
            }
        }
        setBackground(grid);
    }
}
public class Blue extends Actor
{
    /**
     * Act - do whatever the Blue wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        getImage().scale(25, 25);
    }
}
ronald ronald

2021/11/14

#
sometimes it sparkles as soon as I close and open the scenario
danpost danpost

2021/11/15

#
If you want 25 rows and 25 columns, then the rectangle will not be a square of 25 by 25. With a world of 900 by 600, the rectangle will be 36 by 24. Also, all the x and y calculations need to be based on a rectangle width of 36 and a rectangle height of 24 (as per my code using the array above).
ronald ronald

2021/11/15

#
it is true you are right I have a little trouble with math what would be the formula if I put 36 for private static final rows and 24 for private static final cols for int x and int y without changing all the code, I think it can work thank you
danpost danpost

2021/11/15

#
ronald wrote...
what would be the formula if I put 36 for private static final rows and 24 for private static final cols for int x and int y without changing all the code,
See lines 9, 10, 15, 16, 25 and 26 in the code with arrays above. Also, note that the way that is coded, you can change the values in lines 6 and/or 14 and nothing else will need to be changed for it to work properly. That is the proper way to code something to make it easily modifiable. Do not change rows and cols to 36 and 24 if you want 25 rows and 25 columns. That would confuse anyone trying to understand your codes.
ronald ronald

2021/11/15

#
thank you danpost I just understood your formula of int x and int y I took a paper and a pencil and made some calculations, I understood the numbers, it seems logical finally you always have to have a paper and pencil, that's what I started to do
There are more replies on the next page.
1
2