Reputation: 11
I made a 2d array, and need to map the elements [i][j] = 0,1,2,3,4,
or 5
to colored pixels to be printed. This is what I have.
The array of pixels, integer value representing a different color
static int [][] marioArray = new int [][]{
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,0,0,2,2,2,3,3,4,3,0,0,0,0,0},
{0,0,0,2,3,2,3,3,3,4,3,3,3,0,0,0},
{0,0,0,2,3,2,2,3,3,3,4,3,3,3,0,0},
{0,0,0,0,2,3,3,3,3,4,4,4,4,0,0,0},
{0,0,0,0,0,3,3,3,3,3,3,0,0,0,0,0},
{0,0,0,0,1,1,5,1,1,5,1,1,0,0,0,0},
{0,0,0,1,1,1,5,1,1,5,1,1,1,0,0,0},
{0,0,1,1,1,1,5,5,5,5,1,1,1,1,0,0},
{0,0,3,3,1,5,3,5,5,3,5,1,3,3,0,0},
{0,0,3,3,3,5,5,5,5,5,5,3,3,3,0,0},
{0,0,3,3,5,5,5,5,5,5,5,5,3,3,0,0},
{0,0,0,0,5,5,5,0,0,5,5,5,0,0,0,0},
{0,0,0,2,2,2,0,0,0,0,2,2,2,0,0,0},
{0,0,2,2,2,2,0,0,0,0,2,2,2,2,0,0}};
The method to print a pixel
public static void drawPixl (Graphics g, int x, int y, int scale, Color color)
{
g.setColor(color);
g.fillRect(x,y,scale,scale);
}
My attempt at a for loop with my array
public static void drawMario (Graphics g, int x, int y, int scale)
{
int i;
for(i = 0, i < 16, i++){
int j;
for(j = 0, j <16, j++){
if (marioArray[i][j] == 1){
drawPixl(g,scale*i,scale*j,scale,Color.red);}
if (marioArray[i][j] == 2){
drawPixl(g,scale*i,scale*j,scale,new Color(94, 38, 18));}
if (marioArray[i][j] == 3){
drawPixl(g,scale*i,scale*j,scale,new Color(255, 193, 37));}
if (marioArray[i][j] == 4){
drawPixl(g,scale*i,scale*j,scale,Color.black);}
if (marioArray[i][j] == 5){
drawPixl(g,scale*i,scale*j,scale,Color.blue);}
else;
}
}
}
I tried a nested for loop, an enhanced for loop, and a while loop, but I'm not having any luck I guess
Edit:
I tested the drawPixl()
and it works fine. The only errors pop up next to each of the for loops in drawMario
, where it states:
1. Syntax error on token '<', ( expected
2. Syntax error, insert ';;) Statement' to complete ForStatement
3. the method i(int, int) is undefined for the type Animation
Also, int scale is used both to locate where the pixel should be and to give it the right size
Upvotes: 0
Views: 160
Reputation: 11620
I would go with more dynamic approach. Here you will never get AOB exception, and you don't have to change loop parameters when input array size changes.
for(int i = 0; i < marioArray.length; i++){
int [] row = marioArray[i];
for(int j = 0; j <row.length; j++){
Color color = null;
switch(row[j]){
case 1:
color = Color.red; break;
case 2:
color = Color(94, 38, 18); break;
...
}
if(color!=null){
drawPixl(g,scale*i,scale*j,scale,color);
}
}
}
Your code will be cleaner, in switch statement you will only choose Color
, so you don't have to copy whole drawPixl
method to every row.
In Java you don't have to declare loop index outside of an loop,, unless you need it somehow outside of a loop.
Upvotes: 2
Reputation: 5861
public static void drawMario (Graphics g, int x, int y, int scale)
{
int i;
//for(i = 0, i < 16, i++){ --> HERE IS THE ERROR, YOU NEED TO ADD ; AFTER 0 AND 16
for(i = 0; i < 16; i++){ // --> AFTER ADDING ;
int j;
//for(j = 0, j <16, j++){ --> HERE IS THE ERROR, YOU NEED TO ADD ; AFTER 0 AND 16
for(j = 0; j <16; j++){ // --> AFTER ADDING ;
if (marioArray[i][j] == 1){
drawPixl(g,scale*i,scale*j,scale,Color.red);}
if (marioArray[i][j] == 2){
Upvotes: 2
Reputation: 10555
Use semicolon in the for
loop:
int i;
for(i = 0; i < 16; i++){
int j;
for(j = 0; j <16; j++){
Upvotes: 2