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

2014/3/3

Double Matrice and Fill Polygon

1
2
VideoGame VideoGame

2014/3/3

#
Hi, We continue our work on our game. Thanks to some of you, we managed to code an hexagonal map. But now, we have a problem. We must do a ''double matrice'' ( "tableau à deux entrées" in french ). We don't no how to do it. Furthermore we have to assign a color in every sort of case. Here the code, we try to do;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Map here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Map extends World
{
private int tabColors [][];
    /**
     * Constructor for objects of class Map.
     * 
     */
    public Map()
    {  
        super(100, 60, 15);  
        remplisTableau();
        dessinePolygones();
    }
    
    private void dessinePolygones()
    {
        int size = getCellSize()*2; // Dimension de l'hexagone
        // Création d'un hexagone
        int[] xs = { 0, 0, size/2, size, size, size/2 };
        int[] ys = { size/4, size*3/4, size, size*3/4, size/4, 0 };  
        GreenfootImage image = new GreenfootImage(size+1, size+1);  
        
        // Remplir le fond d'écran avec des hexagones  
        for (int i=0; i<20; i++)  
        {
            for (int j=0; j<20; j++)  
            {
                if ((i+j)%2 == 0) // Indique l'espacement entre les hexagones 
                {
                    switch (tabColors) //Attribuer une couleur à chaque valeur du tableau
                    {
                         case 0:
                            setColor(Color.black);
                            break;
                         case 1:
                            setColor(Color.blue);
                            break;
                         case 2:
                            setColor(Color.yellow);
                            break;
                         case 3:
                            setColor(Color.brown);
                            break;
                         case 4:
                            setColor(Color.green);
                            break;
                         case 5:
                            setColor(Color.green);
                            break;
                         case 6:
                            setColor(Color.pink);
                            break;
                         case 7:
                            setColor(Color.white);
                        }
                    //ici, changer la couleur en fonction de l'autre tableau, puis fillPolygon aussi !
                    //Attention : il faut réinterpréter le i pour qu'il corresponde effectivement à la bonne colonne !
                    // parseInt(i/2)
                    image.drawPolygon(xs, ys, 6);
                    getBackground().drawImage(image, i*size/2, j*size*3/4);  
                }
            }
        }
    }

    private void remplisTableau() 
    {
       int tabColors [][]=
    }
}
VideoGame VideoGame

2014/3/3

#
know* And Thank you already for your answers
danpost danpost

2014/3/3

#
I see that your double array (or matrix) is to hold int values, but what are these values going to be for, or what values will you be using to load the array? what will be the size of the double array?
VideoGame VideoGame

2014/3/4

#
The size of the double array is: 20x20 (hexagonal map). The values is a little bit random but we can use 1 and 0 for value . I hope I answer to you're question
danpost danpost

2014/3/4

#
Then, you would initialize your array with:
private int tabColors = new int[20][20];
and fill it with
for (int i=0; i<20; i++) for (int j=0; j<20; j++) tabColorss[i][j] = Greenfoot.getRandomNumber(8);
and start your switch statement with
switch(tabColors[i][j])
You could create an array for the colors and use the value from tabColors for the index in that array. and dispense with the switch block.
Color[] colors = { Color.black, Color.blue, Color.yellow, Color.brown, Color.green, Color.green, Color.pink, Color.white };
for (i=0; i<20; i++) for (j=0; j<20; j++)
{
    image.setColor(colors[tabColors[i][j]]);
    image.drawPolygon(xs, ys, 6);
    getBackground().drawImage(image, i*size/2, j*size*3/4);
}
danpost danpost

2014/3/4

#
In fact, you could assign values to your array while you draw:
for (i=0; i<20; i++) for (j=0; j<20; j++)
{
    tabColors[i][j] = Greenfoot.getRandomNumber(8);
    image.setColor(colors[tabColors[i][j]]);
    image.drawPolygon(xs, ys, 6);
    getBackground().drawImage(image, i*size/2, j*size*3/4);
}
// or even
for (i=0; i<20; i++) for (j=0; j<20; j++)
{
    image.setColor(colors[tabColors[i][j] = Greenfoot.getRandomNumber(8)]);
    image.drawPolygon(xs, ys, 6);
    getBackground().drawImage(image, i*size/2, j*size*3/4);
}
VideoGame VideoGame

2014/3/4

#
Thank You ! But my explanation isn't really good. When I said that the values are random, I wanted to say that it's variable between 0 and 7. We have a number for each hexagon in a table and we don't know how to code this. ( for example; the first line the values are; 2/2/3/1/4/4/4/4/5/5/5/5/4/4/4/1/4/3/2 And the first column; 2/2/3/3/3/5/5/5/5/4/4/4/4/6/6/6/2/2 ) Really sorry for this and I hope you will understand me
danpost danpost

2014/3/4

#
Sure. Then forget what I said as far as assigning values above (everything else should be ok). You can assign the values when you declare the double array field:
int[][] tabColors = { { 2,2,3,1,4,4,4,4,5,5,5,5,4,4,4,1,4,3,2 },
                      { 2,2,3,3,3,5,5,5,5,4,4,4,4,6,6,6,2,2 },
                      // etc.
                      { 2,2,3,1,4,4,4,4,5,5,5,5,4,4,4,1,4,3,2 } };
I duplicated the first line for the last line to show how to end the array assignment.
VideoGame VideoGame

2014/3/4

#
Thank You ! I will test it and tell you back if it work =D
KuroGuroChan KuroGuroChan

2014/3/4

#
Hello, I'm VideoGame's mate. So i tried to fix it and i have this code actually. public Map() { //0=Montagne, 1=Eau, 2=Sable, 3=Crevasse, 4=Fôret, 5=Plaine, 6=Fleurs, 7=Spéciale static class tabColor() { int tabColor={ { { 2, 2, 3, 1, 1, 4, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 3, 3, 2 }, { 2, 7, 3, 1, 4, 4, 4, 3, 5, 5, 5, 5, 6, 4, 4, 1, 1, 1, 3, 2 }, { 3, 3, 3, 6, 6, 6, 4, 3, 6, 6, 5, 5, 6, 6, 6, 1, 2, 3, 3, 3 }, { 3, 3, 6, 6, 1, 2, 2, 3, 3, 6, 3, 5, 6, 7, 1, 2, 2, 3, 2, 2 }, { 3, 5, 5, 1, 6, 6, 2, 2, 3, 3, 3, 5, 3, 1, 1, 2, 2, 2, 3, 2 }, { 5, 5, 3, 6, 1, 6, 1, 2, 2, 3, 3, 3, 1, 5, 1, 6, 2, 2, 2, 2 }, { 5, 5, 5, 4, 6, 6, 1, 1, 2, 2, 0, 0, 1, 5, 3, 1, 6, 6, 6, 2 }, { 5, 5, 4, 4, 0, 1, 1, 1, 2, 1, 0, 1, 5, 5, 6, 6, 6, 4, 6, 6 }, { 5, 0, 4, 4, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 5, 4, 4, 4, 0, 4 }, { 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 0, 5, 0, 0, 4, 4, 4, 4 }, { 4, 4, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 1, 2, 2 }, { 4, 5, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 2, 2, 2 }, { 4, 5, 6, 5, 7, 5, 1, 4, 5, 5, 5, 5, 5, 5, 4, 4, 1, 4, 2, 2 }, { 6, 6, 6, 6, 6, 1, 4, 4, 7, 3, 5, 3, 7, 4, 4, 1, 3, 7, 6, 2 }, { 6, 6, 0, 3, 3, 3, 1, 4, 4, 3, 3, 3, 3, 4, 4, 5, 1, 3, 3, 6 }, { 6, 6, 3, 0, 0, 1, 0, 0, 3, 0, 0, 0, 4, 4, 5, 5, 7, 6, 1, 1 }, { 6, 6, 6, 2, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6 }, { 6, 7, 2, 2, 1, 0, 0, 3, 0, 0, 3, 3, 3, 0, 0, 2, 2, 2, 6, 6 }, { 2, 2, 2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 0, 0, 2, 2, 6 } } }; } } }
KuroGuroChan KuroGuroChan

2014/3/4

#
But i'm having some error all over, i'm big noob! Sorry
KuroGuroChan KuroGuroChan

2014/3/4

#
Ok great
VideoGame VideoGame

2014/3/4

#
import java.awt.*;
import java.awt.Color;  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
/** 
 * Write a description of class Map here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class Map extends World  
{  
private int tabColors [][];  
    /** 
     * Constructor for objects of class Map. 
     *  
     */  
    public Map()  
    {    
        super(100, 60, 15);    
        fillArray();  
        drawPolygon();  
    }  
      
    private void drawPolygon()  
    {  
        int size = getCellSize()*2; // Dimension de l'hexagone  
        // Création d'un hexagone  
        int[] xs = { 0, 0, size/2, size, size, size/2 };  
        int[] ys = { size/4, size*3/4, size, size*3/4, size/4, 0 };    
        GreenfootImage image = new GreenfootImage(size+1, size+1);    
        image.setColor(color[tabColors[i][j]]);  

        // Remplir le fond d'écran avec des hexagones    
        for (int i=0; i<20; i++)    
        {  
            for (int j=0; j<20; j++)    
            {  
                if ((i+j)%2 == 0) // Indique l'espacement entre les hexagones   
                {  
                    switch (tabColors[i][j]) //Attribuer une couleur à chaque valeur du tableau  
                    {  
                         case 0:  
                            setColor(Color.black);
                            break;  
                         case 1:  
                            setColor(Color.blue);  
                            break;  
                         case 2:  
                            setColor(Color.yellow);  
                            break;  
                         case 3:  
                            setColor(Color.brown);  
                            break;  
                         case 4:  
                            setColor(Color.green);  
                            break;  
                         case 5:  
                            setColor(Color.green);  
                            break;  
                         case 6:  
                            setColor(Color.pink);  
                            break;  
                         case 7:  
                            setColor(Color.white);  
                        }  
                    image.drawPolygon(xs, ys, 6);  
                    getBackground().drawImage(image, i*size/2, j*size*3/4);    
                }  
            }  
        }  
    }
    private int fillArray ()
    {  
       int [][] tabColors = {{ { 2, 2, 3, 1, 1, 4, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 3, 3, 2 },
                      { 2, 7, 3, 1, 4, 4, 4, 3, 5, 5, 5, 5, 6, 4, 4, 1, 1, 1, 3, 2 },
                      { 3, 3, 3, 6, 6, 6, 4, 3, 6, 6, 5, 5, 6, 6, 6, 1, 2, 3, 3, 3 },
                      { 3, 3, 6, 6, 1, 2, 2, 3, 3, 6, 3, 5, 6, 7, 1, 2, 2, 3, 2, 2 },
                      { 3, 5, 5, 1, 6, 6, 2, 2, 3, 3, 3, 5, 3, 1, 1, 2, 2, 2, 3, 2 },
                      { 5, 5, 3, 6, 1, 6, 1, 2, 2, 3, 3, 3, 1, 5, 1, 6, 2, 2, 2, 2 },
                      { 5, 5, 5, 4, 6, 6, 1, 1, 2, 2, 0, 0, 1, 5, 3, 1, 6, 6, 6, 2 },
                      { 5, 5, 4, 4, 0, 1, 1, 1, 2, 1, 0, 1, 5, 5, 6, 6, 6, 4, 6, 6 },
                      { 5, 0, 4, 4, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 5, 4, 4, 4, 0, 4 },
                      { 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 0, 5, 0, 0, 4, 4, 4, 4 },
                      { 4, 4, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 1, 2, 2 },
                      { 4, 5, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 2, 2, 2 },
                      { 4, 5, 6, 5, 7, 5, 1, 4, 5, 5, 5, 5, 5, 5, 4, 4, 1, 4, 2, 2 },
                      { 6, 6, 6, 6, 6, 1, 4, 4, 7, 3, 5, 3, 7, 4, 4, 1, 3, 7, 6, 2 },
                      { 6, 6, 0, 3, 3, 3, 1, 4, 4, 3, 3, 3, 3, 4, 4, 5, 1, 3, 3, 6 },
                      { 6, 6, 3, 0, 0, 1, 0, 0, 3, 0, 0, 0, 4, 4, 5, 5, 7, 6, 1, 1 },
                      { 6, 6, 6, 2, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6 },
                      { 6, 7, 2, 2, 1, 0, 0, 3, 0, 0, 3, 3, 3, 0, 0, 2, 2, 2, 6, 6 },
                      { 2, 2, 2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 0, 0, 2, 2, 6 },
                      { 2, 2, 3, 1, 1, 4, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 3, 3, 2 }}};
    }  
} 
We have this but there's a message error "cannot find symbol - variable color" We imported java.awt.Color; but.... And can we fill the polygon with color with fillPolygon variable
danpost danpost

2014/3/4

#
VideoGame wrote...
but there's a message error "cannot find symbol - variable color"
'color' should be 'colors' and you need to add my color array from three posts ago.
can we fill the polygon with color with fillPolygon variable
You can use fillPolygon in place of drawPolygon to fill each hexagon with color. You may want to set the color to white and drawPolygon at the same place to show separation between like, or similar, colors (use lightGray instead of white as a fill color). Also, remove the 'switch' block (it is not needed, now). And, move line 31 to inside the for loops.
VideoGame VideoGame

2014/3/4

#
import java.awt.*;
import java.awt.Color;  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
/** 
 * Write a description of class Map here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class Map extends World  
{  
private int tabColors [][];  
    /** 
     * Constructor for objects of class Map. 
     *  
     */  
    public Map()  
    {    
        super(100, 60, 15);    
        fillArray();  
        drawPolygon();  
    }  
      
    private void drawPolygon()  
    {  
        int size = getCellSize()*2; // Dimension de l'hexagone  
        // Création d'un hexagone  
        int[] xs = { 0, 0, size/2, size, size, size/2 };  
        int[] ys = { size/4, size*3/4, size, size*3/4, size/4, 0 };    
        GreenfootImage image = new GreenfootImage(size+1, size+1);    
        image.setColor(colors[tabColors[i][j]]);  
        console.log(filterInt(i=i/2));
        // Remplir le fond d'écran avec des hexagones    
        for (int i=0; i<20; i++)    
        {  
            for (int j=0; j<20; j++)    
            {  
                if ((i+j)%2 == 0) // Indique l'espacement entre les hexagones   
                { 
                    Color[] colors = { Color.black, Color.blue, Color.yellow, Color.brown, Color.green, Color.green, Color.pink, Color.white };  
                    for (i=0; i<20; i++) for (j=0; j<20; j++)  
                    {  
                        image.setColor(colors[tabColors[i][j]]);  
                        image.drawPolygon(xs, ys, 6);  
                        getBackground().drawImage(image, i*size/2, j*size*3/4);  
                    }  
                    switch (tabColors[i][j]) //Attribuer une couleur à chaque valeur du tableau  
                    {  
                         case 0:  
                            setColor(Color.black);
                            break;  
                         case 1:  
                            setColor(Color.blue);  
                            break;  
                         case 2:  
                            setColor(Color.yellow);  
                            break;  
                         case 3:  
                            setColor(Color.brown);  
                            break;  
                         case 4:  
                            setColor(Color.green);  
                            break;  
                         case 5:  
                            setColor(Color.green);  
                            break;  
                         case 6:  
                            setColor(Color.pink);  
                            break;  
                         case 7:  
                            setColor(Color.white);  
                        }   
                    image.drawPolygon(xs, ys, 6);  
                    getBackground().drawImage(image, i*size/2, j*size*3/4);    
                }  
            }  
        }  
    }
    private int fillArray ()
    {  
       int [][] tabColors = {{ { 2, 2, 3, 1, 1, 4, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 3, 3, 2 },
                      { 2, 7, 3, 1, 4, 4, 4, 3, 5, 5, 5, 5, 6, 4, 4, 1, 1, 1, 3, 2 },
                      { 3, 3, 3, 6, 6, 6, 4, 3, 6, 6, 5, 5, 6, 6, 6, 1, 2, 3, 3, 3 },
                      { 3, 3, 6, 6, 1, 2, 2, 3, 3, 6, 3, 5, 6, 7, 1, 2, 2, 3, 2, 2 },
                      { 3, 5, 5, 1, 6, 6, 2, 2, 3, 3, 3, 5, 3, 1, 1, 2, 2, 2, 3, 2 },
                      { 5, 5, 3, 6, 1, 6, 1, 2, 2, 3, 3, 3, 1, 5, 1, 6, 2, 2, 2, 2 },
                      { 5, 5, 5, 4, 6, 6, 1, 1, 2, 2, 0, 0, 1, 5, 3, 1, 6, 6, 6, 2 },
                      { 5, 5, 4, 4, 0, 1, 1, 1, 2, 1, 0, 1, 5, 5, 6, 6, 6, 4, 6, 6 },
                      { 5, 0, 4, 4, 1, 1, 1, 1, 1, 1, 3, 0, 0, 0, 5, 4, 4, 4, 0, 4 },
                      { 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 0, 5, 0, 0, 4, 4, 4, 4 },
                      { 4, 4, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 1, 2, 2 },
                      { 4, 5, 5, 5, 5, 1, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 2, 2, 2 },
                      { 4, 5, 6, 5, 7, 5, 1, 4, 5, 5, 5, 5, 5, 5, 4, 4, 1, 4, 2, 2 },
                      { 6, 6, 6, 6, 6, 1, 4, 4, 7, 3, 5, 3, 7, 4, 4, 1, 3, 7, 6, 2 },
                      { 6, 6, 0, 3, 3, 3, 1, 4, 4, 3, 3, 3, 3, 4, 4, 5, 1, 3, 3, 6 },
                      { 6, 6, 3, 0, 0, 1, 0, 0, 3, 0, 0, 0, 4, 4, 5, 5, 7, 6, 1, 1 },
                      { 6, 6, 6, 2, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6 },
                      { 6, 7, 2, 2, 1, 0, 0, 3, 0, 0, 3, 3, 3, 0, 0, 2, 2, 2, 6, 6 },
                      { 2, 2, 2, 2, 1, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 0, 0, 2, 2, 6 },
                      { 2, 2, 3, 1, 1, 4, 4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 1, 3, 3, 2 }}};
    }  
}
Always the same problem... it underlines "colors" in this line; " image.setColor(colors]); " -> cannot find symbol - variable colors ... ( Sorry, we are very new on Greenfoot And Thank you again for your answers )
There are more replies on the next page.
1
2