Use a function to display images

edited August 2015 in How To...

I have 8 images that I want to display via a function. Each implementation of the function needs to show a different image. What I cant work out is how to pass the image—for that function implementation—what image it needs to show...

ie.

PImage a, b;

void setup() {

a = loadImage("a.jpg");

b = loadImage("b.jpg");

}

void draw() {

myImage(???????, 40, 40); // to show a

myImage(???????, 80, 80); // to show b

}

void myImage(???????, int x, int y) {

image(???????, x, y);

}

Can anyone help clarify the ??????????'s ?

Thanks in advance.

Answers

  • sorry about the formatting! not sure how it works here!

  • edited August 2015
    PImage a, b;
    
    void setup() {
        size(800,800); 
        a = loadImage("a.jpg");       
        b = loadImage("b.jpg");
    
    }
    
    void draw() {
    
        myImage(a, 40, 40); // to show a
    
        myImage(b, 80, 80); // to show b
    
    }
    
    void myImage(PImage localImg, int x, int y) {
    
        image(localImg, x, y);
    
    }
    
  • you might want to consider a array of PImage

  • thank you so much, do you have a second to explain 'localImg' in relation to PImage as so I can learn as to how you did this?

  • the image localImg is known only in the function (it's a local var)

    a and b are know in the whole sketch (global var)

    the scope is different

    am I doing your homework?

  • edited August 2015

    anyway, localImg holds a copy of the reference to the memory adress of the image you are passing to it as a parameter.

    this also known as "call by reference"

    Hence when you change the local var you might change the global as well

    (dunno)

  • no (to the homework query), but you have just been a massive help and I appreciate your knowledge here.

  • edited August 2015

    a function has several parts, e.g.

    • a name,

    • it does return something (void!!! or an Image) and

    • it can get something (the parameters). The parameters can be zero (like in setup() : empty brackets), one or many. They are always type plus name. Thus you create a new local variable in the function.

    • it has commands between { and }

    • it is therefore like a factory (leather in, shoes out) or a machine. A sketch are machines working nicely together.

    • a function can also be seen as a new command: line() is an in-build command, but you could write your own command lineHorizontal(x,y,w);. Thus you enlarge the processing language.

    • functions are a very good concept to apply because you don't have one endless draw() then but single modules. The next step is using classes (OOP, object oriented programming, see tutorials)

    see

    http://portalsfordeveloping.wikia.com/wiki/Functions

  • thank you, much appreciated

  • Seriously though, you want an array of PImages. Then you need only pass the index into that array to your function.

    Also, you probably want to get rid of your function. It does the same thing as image() itself!

  • I see this more as a test case

  • edited August 2015

    Just an addendum:

    • Variables in Java can be classified as either field or local variable.
    • Field variables can be accessed anywhere and remain for the lifespan of its object instance.
    • While local variables can only be seen inside its own scope and cease to exist when that scope finishes.
    • Parameters are local variables too. Which are initialized w/ passed arguments to its function.
Sign In or Register to comment.