implementation of class or structure

edited December 2013 in Programming Questions

hy there, i want to create a variable to store different kind of variables. that i could pass to functions which need more than one parameter. i am looking for something like the "color" class or what it is.______ instead of passing three or four variables you can just pass one color object.

bsp: 
color c = color(204, 153, 0);
background( c);

this is how the color would work and below is what i would like to do. any help is wellcomed, what could also help is how did they implement the "color" class. by the way is it a class or something else. thanks in advance

Func1(String, int, float); // function declaration
myThing mc = myClass("hello", 1,2.0); // object init
func1(mc);  // call function with object as param

Answers

  • the formatting went wrong there , i hope you still understand want i wanted to write

  • i think i know how they did it with the color, its probaly the fill or background method that's been implemented to accept an object of type color, if anyone knows more, leave a comment

  • edited December 2013

    For correctly formatting posted code here, please highlight it and hit CTRL+K! (%)

  • Neither color(), nor fill(), stroke(), background() etc., accept object types. All of them are primitive float data-types!

  • The color "class" in Processing is a special case. Don't look to it for a lot of inspiration, as it'll probably just be confusing.

    What you're looking for sounds a lot like a class that encapsulates the different kinds of data you want to group together.

    You could also look into varargs.

  • edited December 2013

    The color "class" in Processing does not exist.

    In Processing the color data type is the same as a Java int Before compiling the sketch code the Processing pre-processor replaces all occurrences of color with int. The only exception is when color is followed by a (, in which case it is a call to the color() method.

    It is possible to define Java methods that have a variable number of parameters, for example the code below has a method called myMethod where you can pass any number of parameters (including none) of any type.

    public void setup() {
      size(200, 200);
      myMethod();
      myMethod("A string", 2.3, true);
    }
    
    public void myMethod(Object... params) {
      println("\nNumber of params = " + params.length);
      for (Object param : params)
        println("  " + param.getClass().getSimpleName());
    }
    

    This creates the following output

    Number of params = 0

    Number of params = 3

    String

    Float

    Boolean

  • I think what the OP wants is some basic object oriented programming. I threw together a tutorial going over the basics of OOP in Processing, it might help: http://staticvoidgames.com/tutorials/objects/index.jsp

  • Also, Dan Shiffman's teaching videos are awesome.

    Here he is on objects: http://icm.shiffman.net/7.0/

  • Are looking for a function that accepts a custom obj? That's simple:

    class Ob{
    // all stuff
    }
    
    void gimmeSomeOb(Ob instance){
    // calcs with  passed instance of Ob
    }
    
    // using
    Ob o = new Ob();
    gimmeSomeOb(o);
    

    also you can return your object in a function.

    Ob compareObs(Ob one, Ob other){
    // test and 
       return one // of them :  )
        }
    
Sign In or Register to comment.