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

2013/12/8

Looping

Westga2016 Westga2016

2013/12/8

#
Hello I'm having problem doing a loop I don't know if im doing this right or not. i hope some one could help me please. public void redDot(){ Pixel aRandomPix = getPixel(240000); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240001); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240002); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240003); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240004); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240005); aRandomPix.setColor(Color.RED); // end first row of dot aRandomPix = getPixel(240600); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240601); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240602); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240603); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240604); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(240605); aRandomPix.setColor(Color.RED); // end second row of dot aRandomPix = getPixel(241200); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241201); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241202); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241203); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241204); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241205); aRandomPix.setColor(Color.RED); // end third row of dot aRandomPix = getPixel(241800); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241801); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241802); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241803); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241804); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(241805); aRandomPix.setColor(Color.RED); // end forth row of dot aRandomPix = getPixel(242400); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(242401); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(242402); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(242403); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(242404); aRandomPix.setColor(Color.RED); aRandomPix = getPixel(242405); aRandomPix.setColor(Color.RED); // end fifth row of dot int i = 0; while ( i < 242405) { aRandomPix = getPixel(240000); aRandomPix.setColor(Color.RED); i = i + 1; }
Adept_Red Adept_Red

2013/12/8

#
Just looking at your loop... it will loop 242405 times, however every time it loops, aRandomPix gets the value getPixel(240000). So from what I am seeing, every loop does the same thing and nothing changes. So what exactly is it you're trying to do? I'm going to go out on a limb and guess that perhaps you're trying to achieve something like...
//each loop will get the next pixel from 240000 to 242404 
aRandomPix = getPixel(240000) +i;
Also you might want to make the loop <= becuase it will currently stop before 242405.
Westga2016 Westga2016

2013/12/8

#
Thanks for Responding. I'm trying to change the color of the slides on the project im working on for example its like increase red and so on.
Adept_Red Adept_Red

2013/12/8

#
So you are trying to change the color of a line of pixels in an image? Well looks like you were on the right track then, assuming you knew the first (240000) and last (242405) pixels in the line. Have you tried implementing any variations of the code I showed you?
Westga2016 Westga2016

2013/12/8

#
I did. When I use this: int i = 0; while ( i <= 242405) { aRandomPix = getPixel(240000); aRandomPix.setColor(Color.RED); i = i + 1; ; it does change anything at all. When I do this : int i = 0; while ( i <= 242405) { aRandomPix = getPixel(240000)+1; aRandomPix.setColor(Color.RED); i = i + 1; ; I get an error saying cannot apply operator and pixel int
Adept_Red Adept_Red

2013/12/8

#
errr that should probably be inside the brackets (my bad), also it's + i not +1. Do you see why that is?
aRandomPix = getPixel(240000 + i);
Westga2016 Westga2016

2013/12/8

#
Yes i do. It works now. Thank you so much for your help.
You need to login to post a reply.