We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So i'm currently using a picArray of 83 different images (.png's) to create a randomised graphic on a black background.
Instead of the background being black, i want it to be a RANDOM image, of the 4 images i've created and named bg0, bg2, bg3, bg4 in my data file.
I've tried using another picArray and it wouldn't work.
Here's my current code. (I currently have it so that just one image loads (bg2.png))
[code in comments]
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Thanks for your reply GoToLoop!
And thanks for your advice! As you can probably see, i'm still new and only a week in to my programming experiments.
Just a couple questions.
when you state,
i'm slightly confused by your terms as i haven't seen them in the processing references / haven't been taught them yet. i know there are some Java terms not mentioned in Processing references. could you comment on how they work?
for (int i = 0; i < PICS; pics[i] = loadImage("a" + i++ + ".png")); for (int i = 0; i < BACKS; bgs[i] = loadImage("bg" + i++ + ".png"));
do my background images still need to be in my picArray? or could they be loaded separately? as bg = "bg1.png"?.
Here's where i'm currently at.
Delete lines 13 to 186. Your images are now in pics[] and bgs[] so change line 217 as well.
There are other problems with the code but do that first as it hurts my eyes just looking at it.
I feel like i'm getting somewhere! thanks for all the help. This is where i'm at. (i'm struggling with phrasing the last line which randomises the image and their location within the sketch.
You could use ArrayList class if you want to keep adding new images.
I'm not sure I understood your needs though.
And please, please format your code in the question also, I couldn't even read your full question.
@GoToLoop why have you made
int PICS = 83, BACKS = 4
static? I understood the need for final, but why static? Who would use more than one instance of a processing based application?The original question (slightly revised) was,
"So i'm currently using a picArray of 83 different images (.png's) to create a randomised graphic of 10 images on a black background.
Instead of the background being black, i want it to be a RANDOM image, of the 4 images i've created and named bg0, bg2, bg3, bg4 in my data file.
I've tried using another picArray and it wouldn't work."
you've removed too much. these two lines were necessary for loading the images and backgrounds:
you don't need these, they've been replaced with pics[] and bgs[]:
static
&final
, I go full caps + underline for their names! :)>-static
member, I declare them asstatic final
too; but w/o full caps of course. :P@GoToLoop I did mean to say multiple instances of the very same sketch, but failed (oops!) to frame my question correctly. I myself have used multiple instances of PApplet in many sketches, but never more than a single instance of a single sketch.
I'll keep what you said in mind when I code from now on.
@koko That's a lot better. This is my try to solve your problem:
Correct me if I misunderstood you.
static
.smooth(SMOOTH);
tosmooth()
for it then. :P@GoToLoop That is one monster of a sketch, but yeah, you proved your point. :)>-
One thing confuses me, what exactly is a
List<Class>
? I thought it wasArrayList<Class>.
List is the
interface
forclass
ArrayList and many others:http://docs.Oracle.com/javase/8/docs/api/java/util/List.html
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
Lord_of_the_Galaxy, thanks for the code & comments made through your sketch.
Unfortunately, i'm struggling to see where there will be a random set of 10 images on top of a random set of 4 backgrounds? Normally there would have to be something along the lines of,
for(int picIdx=0; picIdx<10; picIdx++){
The random set of 10 images are also to be positioned on random places on the screen.
Line 30, you will need a loop, yes. Line 28 should be within that loop as that's the part you want to repeat. You'll need to change the position parameters though.
I think I failed to understand your question properly. Sorry!
So now this is what I think your question asks:
Each time user presses the mouse, a random image from bgs[] is selected as background and 10 random images from pics[] are drawn randomly on the screen.
Is that so?
In that case, this might be of help:
@GoToLoop Thanks!
Alternative version attempt: :ar!
@Lord_of_the_Galaxy,
final
keyword merely seals classes, methods & fields.For fields, it means they can't be reassigned once initialized. Not even w/
null
or itself!This won't compile for example: #-o
In another angle,
final
protects us against unintentional re-assignment for fields. :>https://Processing.org/reference/final.html
final
keyword has an extra performance boost when deployed for primitive types + String fields.But only for when the value assigned to those
final
fields is a compile-time literal though. L-)In those cases, the Java compiler replaces those fields w/ their actual value wherever they show up in the source code! \m/
Line 17 is incrementing i. Line 16 is also incrementing i...
Oops, thx! Fixed now! X_X
@GoToLoop I already knew this:
But failed to frame it correctly.
But i did not know this:
Thanks!
@GoToLoop koko said:
Your code (line 26 - 29) places random images at precise, predetermined locations on the screen as 5 columns and 2 rows.
Nailed it!
Currently, i'm seeing this...
Which is strange as my background images are the same size as my sketch? (800, 800) The sizes i want my PICS will be 80, 80, (which i've now stated in my sketch),
My two concerns are the size of the background and randomly positioning the 10 shapes in different locations in the sketch.
here is an image of what i've been trying to create.
the background looks like an imageMode() problem.
image(pics[int(random(0, PICS))], random(width), random(height), 80, 80);
This solved my random positioning problem... but i'm still trying to make the background fit the sketch...
It was an imageMode problem!! it's now fully working. Thank you for all donations of knowledge. It truly is a great time to be learning to programme, when all this online support is willing to help out a newbie. you guys are all awesome.
Much love.
@koko and @kooks I commented out imageMode(CENTER) in my code. So that can't be.
The only problem was another small mistake (the random positioning one) that I failedd to notice, but koko had already nailed it.