We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, So we're doing a lab on images and out teacher wants two of our images to show as a grid, two of the grid images I selceted show up as 1 image, while the other one is 21 seperate images. I'm having trouble figuring out how to set up my array for the image grid that only has 1 image. I will put my code below, I'm not very far yet, I think I know how to do the rest after I get the array part figured out.
PImage [] picHero = new PImage[16];
PImage [] picPlanets= new PImage[21];
PImage [] [] picAlien= new PImage[1][16];
PImage [] picKayla= new PImage[15];
PImage [] []picMechanic= new PImage[1][16];
PImage Bg,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20;
void setup() {
size(512,512);
PImage Mech = loadImage("MechanicMale.png");
for(int i=0; i<picMechanic.length;i++){
int row = i / 4;
int col = i % 4;
int m = i / 1;
picMechanic[m][i % 1] = Mech.get(col*30, row*25, 30, 25);
}
PImage Alien = loadImage("PterosFemale001.png");
for(int i=0; i<picAlien.length;i++){
int row = i / 4;
int col = i % 4;
int a = i / 16;
picAlien[a][i % 16] = Alien.get(col*30, row*25, 30, 25);
}
Bg=loadImage("seamless space.PNG");
picKayla[16] = loadImage("Kayla.png");
picHero[16] = loadImage("Old hero.png");
imageMode(CENTER);
}
void draw (){
background(0);
noTint();
image(Bg,width,height,Bg.width/2,Bg.height/2);
}
void keyPressed(){
if(key== 'a'){
}
if(key== CODED){
}
}
Alien,Kayla,Mechanic,and Hero are the ones that are girds but they are only 1 image in the form of a grid.
Answers
Could someone help me please
What really confuses me about your code is the following two lines:
and
These look like they're 2D arrays, but they're not, since they've only got one index that really matters. In any case, this shows us that you don't really understand Arrays. So.
Let's say you need to remember one number. The number is seven. You remember it using an int.
Great. Now let's say you need to remember two numbers. Seven and Thirty-six.
Great. Now let's say you need to remember 4000 numbers. You could do this:
No good. Instead, you could remember them as ONE LIST of numbers that is 4000 numbers long. This would be an array of numbers.
That makes space (in the memory of your computer) for the 4000 numbers - but it doesn't give them any meaningful values. You would need to use a loop to do that.
But maybe you don't need a list of numbers. Maybe you several lists of them.
This is a 12 by 12 grid of numbers - or (if it helps you to think of it this way) a list of several lists of numbers.
Of course, I'm using numbers. But you can have Arrays of other things too.
Hopefully now you can understand why your "2D" arrays are a little weird. They're a list of lists of images, except there's only one list!
Yeah, I've been changing my code a lot since I first posted it, The row by columns array would only work for the planets since there are 17 individuals of them, it's the images that have been confusing me, it's 1 image, but the character in the image is in 16 different positions, I was thinking that I needed an array of 16 so the program can show just one postion, but now I'm thinking that I actually need multiple images to make a grid.
This is my new code, The background appears, but the program won't show the planets, I think there's something wrong with my for loop.
key pressed()
as it siblingsmousePressed()
,keyReleased()
, etc, are executed (the code inside them) once when such events happens. So if you draw inside them, and also have some code drawing indraw()
, the stuff in draw will overwrite the stuff from keyPressed. Those will be displayed for 1/60th of a second. Instead use a boolean as a flag and do the call toimage()
in draw based on the state of the boolean.like
You came up with an algorithm to handle accessing your 2D array (do you really need it? don't know). That's ok. Usually we use a nested loop with 2D arrays, like:
I just used the algorithim that my teacher showed us on an example project.
Ok so I got help from my teacher and completed the necessary part of the assignment, but I want to still include the planets, I had made a bouncing project and I tried to incorporate that with the planets, could you help me figure it out, I commented the code I need help on out so you could see the regular program. boolean mech=false; boolean kayla=false; boolean alien=false; boolean planet=false; //int num=10; //float [] x; //float [] y; //float [] dx; //float [] dy; //color [] c; //int boomx=-10; //int boomy=10; PImage [][] picMechanic= new PImage[4][4]; PImage [][] picKayla= new PImage[4][4]; PImage [] picPlanets= new PImage[18]; PImage Bg, p1, Alien, Mech; int a,b;