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
danpost danpost

2014/3/4

#
Move the colors array to before the for loops and move the image creation line to inside the loops. Basically, switch lines 30 and 40; and remove line 31. Also, brown is not a recognized color of the Color class.
danpost danpost

2014/3/4

#
Also, in your 'fillArray' method, you are declaring a new 'tabColors' variable array which you already declared near the top of the class as a field. Remove
int[][]
from the method and remove the outer set of curly brackets from around your array values.
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 i = 0;
        int j = 0;
        int[] xs = { 0, 0, size/2, size, size, size/2 };    
        int[] ys = { size/4, size*3/4, size, size*3/4, size/4, 0 };
        Color[] colors = { Color.black, Color.blue, Color.yellow, Color.black, 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);    
                    }  
        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     
                {    
                    GreenfootImage image = new GreenfootImage(size+1, size+1);
                    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.black);    
                            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 }}};  
    }    
}
This is our code now but; 1. There was a problem with "i and j" variable but we resolved it by giving a value "0" to i and j but it's correct ? 2. Then we have a problem with the variable "image" he can't find it 3. And for your proposition to remove int and the outer set of curly brackets, there's a message error; illegal start of expression .....
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 }};  
Thank you for your help
danpost danpost

2014/3/4

#
Oh, I see the main problem. You still have two double for loops in your code. This is what you should have:
import greenfoot.*;
import java.awt.Color;  

public class Map extends World    
{    
    private int tabColors [][];    

    public Map()    
    {      
        super(100, 60, 15);      
        fillArray();    
        drawPolygons();
    }    
        
    private void drawPolygons()    
    {    
        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 };
        Color[] colors = { Color.black, Color.blue, Color.yellow, Color.darkGray, Color.green, Color.green, Color.pink, Color.lightGray };    
        for (i=0; i<20; i++) for (j=0; j<20; j++)    
        {
            GreenfootImage image = new GreenfootImage(size+1, size+1);
            image.setColor(colors[tabColors[i][j]]);    
            image.fillPolygon(xs, ys, 6);
            image.setColor(Color.white);
            image.drawPolygon(xs, ys, 6);
            getBackground().drawImage(image, i*size/2, j*size*3/4);    
        }
    }

    private void fillArray()  
    {    
       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 }};  
    }    
}
I think I did that right.
VideoGame VideoGame

2014/3/4

#
Thank you for your big implication in our work. Always the same problem for array; we have to put
int [][] tabColors = ...
Otherwise ; illegal start of expression Then, when I try your code, "cannot find symbol - variable i " appear, so I try to give a value for i and j ( 20 ) like this ;
        int i = 20;
        int j = 20;
But I don't know if it's correct After that, I compile but there's again a problem; No syntaxe error but a new message ( "Greenfoot: BlueJ : Terminal ) who said this; java.lang.NullPointerException at Map.drawPolygons(Map.java:26) at Map.<init>(Map.java:12)
danpost danpost

2014/3/4

#
I tested and fixed the problems. Here is the revised code:
import greenfoot.*;
import java.awt.Color;  

public class Map extends World    
{    
    private 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 }};      

    public Map()    
    {      
        super(100, 60, 15);      
        drawPolygons();
    }    
        
    private void drawPolygons()    
    {    
        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 };
        Color[] colors = { Color.black, Color.blue, Color.yellow, Color.darkGray, Color.green, Color.green, Color.pink, Color.lightGray };    
        for (int i=0; i<20; i++) for (int j=0; j<20; j++)    
        {
            GreenfootImage image = new GreenfootImage(size+1, size+1);
            image.setColor(colors[tabColors[i][j]]);    
            image.fillPolygon(xs, ys, 6);
            image.setColor(Color.white);
            image.drawPolygon(xs, ys, 6);
            getBackground().drawImage(image, i*size/2, j*size*3/4);    
        }
    }
}
Though, you may not like how the hexagons are drawn onto the background.
VideoGame VideoGame

2014/3/4

#
Thank you very much ! The Hexagons are a little bit superimposed. Why ? Thank you again, you save our live =D !
danpost danpost

2014/3/4

#
To fix the horizontal superimposing, change the x coordinate of drawing the polygon to 'size' (instead of 'size/2'). Then you need to offset every other row by 'size/2'. so add '(j%2)*size/2' to the x coordinate. That makes line 46:
getBackground().drawImage(image, i*size+(j%2)*size/2, j*size*3/4);
VideoGame VideoGame

2014/3/4

#
Thank you very much ! It's finally done. Thank you again and again =D !
danpost danpost

2014/3/5

#
VideoGame wrote...
Always the same problem for array; we have to put
int [][] tabColors = ...
Otherwise ; illegal start of expression
Another fix would be (still in the 'fillArray' method
tabColors = new int[][] { { 2, 2, .... 
Then, when I try your code, "cannot find symbol - variable i " appear, so I try to give a value for i and j ( 20 ) like this ;
        int i = 20;
        int j = 20;
But I don't know if it's correct
That would stop the error. But just declaring them in the 'for' statement is better:
for (int i=0; i<20; i++) for (int j=0; j<20; j++)
After that, I compile but there's again a problem; No syntaxe error but a new message ( "Greenfoot: BlueJ : Terminal ) who said this; java.lang.NullPointerException at Map.drawPolygons(Map.java:26) at Map.<init>(Map.java:12)
This was due to the fact that your tabColors field declared near the top of the code was never set to anything (even though it appeared it was by your 'fillArray' method, which was declaring a new local variable array -- not the same array even though it had the same name). With the fix at the top of this post, this error should go away.
You need to login to post a reply.
1
2