Processing beginner

24

Answers

  • you can work on the lines:

    • you could move them (left to right bouncing on the wall)

    • finally you could go 3D

  • edited May 2014

    Last question!

    I didn't understand what "state" is.

    Fields are used to store the data for the object and combined they make up the state of an object. As we're making a Book object it would make sense for it to hold data about the book's title, author, and publisher:

    public class Book {

    //fields
    private String title;
    private String author;
    private String publisher;
    

    }

    So title + author + publisher = state?

    Also if constructor is empty:

    Linemine() {
    
      }
    

    When you create an object (linemine = new Linemine();), is it given default values? i.e;

     float posX = 0.0f //stored in field
      float posY = 0.0f
      float stroWeight = 0.0f
    
  • edited May 2014

    I'm also struggling with the snake example:

    Snake s0;
    Snake s1;
    

    This is 2 reference variables, called "s0" and "s1", of type "Snake". Each will be pointing to the location in memory where the object "s0" and "s1" will be stored.

      s0 = new Snake(50);
      s1 = new Snake(25);
    

    This creates 2 objects, from class Snake;

    class Snake {
      // x and y positions
      int[] xpos;
      int[] ypos;
    
      // The constructor determines the length of the snake
      Snake(int n) {
        xpos = new int[n];
        ypos = new int[n];
      }
    

    So "s0" and "s1" are objects, that each contain 2 arrays (xpos & ypos) ?

    A little confused.

  • you wrote:

    So title + author + publisher = state?

    yes, correct. Or for the line its position and color.

    you wrote

    When you create an object (linemine = new Linemine();), is it given default values?

    correct. You can even have no constructor then it works with default values too

    you wrote

    I'm also struggling with the snake example:
    
    Snake s0;
    Snake s1;
    
    
    This is 2 reference variables, called "s0" and "s1", of type "Snake". 
    

    well, yes. They are objects instantiated from class Snake. ;-)

    you wrote :

    So "s0" and "s1" are objects, that each contain 2 arrays (xpos & ypos) ?

    yes.

    for practicing you could rewrite it using one array of type PVector in the class.

    you need practice.

  • What is an array of type PVector?

  • ??

    like PVector[] = new PVector[n];

    look up PVector in the reference pls

  • Excuse my ignorance, but from what I understood, I just understood PVector as a vector (having magnitude and direction).

    I didn't understand what an array of type PVector is.

  • edited May 2014

    I just understood PVector as a vector (having magnitude and direction).

    For all effects, in Java's point-of-view, PVector would be just a name for a class.
    In Java, classes are also used as object (non-primitive) data-types.

    Now if we wanna actually know what a particular class does, we gotta search for its "javadoc".
    Where its methods, constructors and (possibly) fields are described!

    http://processing.org/reference/PVector.html
    https://github.com/processing/processing/blob/master/core/src/processing/core/PVector.java

  • edited May 2014

    I didn't understand what an array of type PVector is.

    Again, a Java array is just an object which has an indexed fixed number of slots to hold values of a single data-type.
    It can store both primitive values, like an int or float, and reference values, like String or PVector.

  • yes, arrays can have all data-types or classes as their type.

    then it's a list of objects instatiated from that class.

  • you could make that to an array (and should for practizing!!)

    instead of

    linemine.display();
    linemine1.display();
    linemine2.display();
    linemine3.display();
    linemine4.display();
    linemine5.display();
    linemine6.display();
    
  • edited May 2014

    So "s0" and "s1" are objects, that each contain 2 arrays (xpos & ypos) ?

    Both s0 & s1 are variables of type Snake. They're not objects! Rather, they store references of that data-type!
    They behave as avatars or representatives for the objects they reference to. But nothing more than that!
    Those xpos & ypos are instance variables (non-static) from the objects they point to.

    Even though it's a lil' hard at 1st, we gotta learn how to decouple variables from their stored values!
    The reason why is b/c more than 1 variable can hold references for the same object in memory.
    Even though they can happen to have unrelated names from each other!

    Moreover, those references can also be stored inside fields from other objects!
    Like the array object referenced by variable pets below @ line #14:

    // forum.processing.org/two/discussion/4736/processing-beginner
    
    PVector dog = new PVector(100, 50);
    println(dog);
    println();
    
    PVector cat = dog;
    cat.set(TAU, EPSILON);
    
    println(dog);
    println(dog == cat);
    println();
    
    PVector[] pets = {
      cat, PVector.random3D(this), dog,
    };
    
    PVector.random2D(pets[2], this);
    
    println(dog);
    println(dog == pets[0]);
    println(dog == pets[1]);
    
    exit();
    

    As you can see above: dog, cat, pets[0] & pets[2] all point to the very same instance of PVector from line #3!
    Only pets[1] holds another PVector object reference! Which was instantiated @ line #15. >:)

  • edited May 2014

    Oh God. Programming is so stressful, so much to learn. I didn't understand much from the above.

  • it's all the engine, man

    concentrate on the driving. Where's your praticing? I gave you some tasks...

  • edited May 2014

    I got very frustrated with not understanding basic stuff, and I want to complete a degree in Computer Science... If I don't understand this simple stuff... then I won't get very far in this degree! #-o

    It started well, but then I experienced frustration. Feels like one step forward, two steps back. Will try to focus on the tasks given.

  • edited May 2014

    Ok GotToLoopy, so:

            Snake s0;
            Snake s1;
    

    s0 & s1 are variables of type Snake.

      // Initialize
      s0 = new Snake(50);
      s1 = new Snake(25);
    

    Doesn't that create object from the Snake class? Object s0 and s1?

    class Snake {
      // x and y positions
      int[] xpos;
      int[] ypos;
    

    And each object, s0 & s1, contain 2 array of ints (whom length is determined when you call the constructor and pass in the parameter), named xpos & ypos?

  • edited May 2014

    Chrisir, back to our code:

    Linemine linemine;
    Linemine linemine1;
    Linemine linemine2;
    Linemine linemine3;
    Linemine linemine4;
    Linemine linemine5;
    Linemine linemine6;
    

    That is at the top of the code. I believe those to be objects, instance of the class Linemine.

    I tried to make an array of the 7 objects, but failed. Here is my code:

    Linemine [] linemine = {linemine,linemine1,linemine2,linemine3,linemine4,linemine5,linemine6};

  • Don't let yourself be confused by GoToLoop. He is only going on about things that are in 99% irrelevant for a beginner. That is not the way of a good teacher. Concentrate on the driving instead of the engine and build up confidence this way.

    The LineMine issue

    It's not my code. It's yours. You're its master.

    look at the way you instantiated one object linemine.

    Repeat this way for the array:

    lines[0] = .........................

  • edited May 2014

    Let me get this right, at the top of the code we are working on is:

    Linemine linemine;

    This is a variable (reference/pointer), of type Class Linemine, called "linemine"? And inside that variable, there is currently no value? When we instantiate an object:

    linemine = new Linemine();

    the value inside that variable, will be the reference to the place where the object is stored in memory (1st physical address of where the object is stored in the heap zone)?

  • exactly!

  • with an Array of type Line you need to for-loop over the array and use new.

    you need to get used to work with arrays. This:

    Linemine [] linemine = {linemine,linemine1,linemine2,linemine3,linemine4,linemine5,linemine6};

    is not the right approach, since you want to replace linemine1,linemine2,linemine3,linemine4,linemine5,linemine6 with the array. They shouldn't appear in your code at all anymore.

  • Yes, it's not the right approach, but why did it fail to work?

    Error message was:

    "Can not reference a field before it is defined"

  • edited May 2014

    you would have to say

    Linemine [] linemine = new Linemine [5]; 
    

    first I guess. But this is only the beginnig

    read

    http://wiki.processing.org/w/From_several_variables_to_arrays

    and then

    More advanced questions

    From several variables to arrays
    From several arrays to classes
    

    from

    http://wiki.processing.org/w/Technical_FAQ

  • Okay, I read that. 2 things I'm unsure of:

        Ball(int r, color c)
          {
            // This actually calls the other constructor, providing the missing values
            this(random(r, width - r), random(r, height - r), random(-7, 7), random(-7, 7), r, c);
          }
    
          Ball(float x, float y, float sx, float sy, int r, color c)
          {
            posX = x;
            posY = y;
            speedX = sx;
            speedY = sy;
            radius = r;
            ballColor = c;
          }
    

    This actually calls the other constructor, providing the missing values

    Unsure how that is the case.

    an array in Java is an object

    An array is an object?

  • Ok, why don't you do the tasks?

    Your questions: 1) a class can have different constr with different parameters

    Here one is calling the other to instantiate the object

    2) an array is not a object.

    It is a list.

    A list of vars of type int or of type String or of type of a class

  • I'm focusing on the task of course. I'm going through all the codes on the links you provided, and seeing if I can manage to get all the codes on that page, working as expected, on processing, by making objects and testing them out with use of arrays. If I do succeed, then the task here will be very straightforward. The links have been very helpful. Thanks for your patience and support!

    I don't understand how:

    Ball(int r, color c)

    Calls:

    Ball(float x, float y, float sx, float sy, int r, color c)

    to instantiate the object, providing the missing values.

    I didn't understand. What missing values?

    Surely when an object is instantiated:

    Ball ball= new Ball(float x, float y, float sx, float sy, int r, color c)
    

    Isn't that enough?

    I don't understand why you would include 2 constructors. 1 to call the other.

  • Just for convenince a short constructor so it's easier to instantiate an object (not much to write to do so)

    The other constr. is longer an more complete

  • edited May 2014

    Don't let yourself be confused by GoToLoop... That is not the way of a good teacher.
    An array is not a object. It is a list.

    I wanted to stay away a lil' from the discussion here. But I've felt the urge to correct that! %-(
    Look at these Java's official links below:

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variablesummary.html
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

    An array is a container "object" that holds a fixed number of values of a single type.
    The 8 "primitive" data types are: byte, short, int, long, float, double, boolean, and char.

    As I've mentioned a couple of times already. Any datatype which doesn't belong to the 8 primitive 1s are object types!
    Yea, that includes arrays, strings, ArrayList, PVector, Object, etc.
    Moreover, classes & interfaces are used as object datatypes for variables & methods in Java!
    Also, object datatype variables store physical memory addresses (references or pointers) from objects!

  • He is only going on about things that are in 99% irrelevant for a beginner.

    So why the links I've posted refer to "/tutorial/java/nutsandbolts"?
    Seems pretty basic stuff for me! :P

  • I still feel from a beginners perspective an array is not an object.

    With an object you say

    Classname objName = ....

    With an array you write

    ClassName[] obj = ....

    and the word array doesn't occur

    And you don't write a class for an array

    Technical you are right but that's not the point - try to think from his perspective.... ;-)

  • edited May 2014

    Try to think from his perspective...

    I understand it. However, even though he's still starting off, he's in CS! (*)
    I believe I can be a lil' stricter to him! >:)

    ... and the word array doesn't occur... And you don't write a class for an array...

    Java places its array type in an exclusive diff. category called ArrayType! @-)
    It's the only object which doesn't have a formal datatype name. Neither it is instantiated by calling a constructor!
    Instead, Java arrays use square brackets [] to access & identify itself from the rest! 8-X
    Also it has an alternative instantiation format using curly braces {}! =:)

    There are some other classes w/ special treatments, like String, Object, Number's subclasses.
    But not in the same league as a Java array object! :-B

  • edited May 2014
    • Chrisir:

      Just for convenince a short constructor so it's easier to instantiate an object (not much to write to do so). The other constr. is longer an more complete

    I still haven't understood the need for 2 constructors. I don't understand how:

    Ball(int r, color c)

    Calls:

    Ball(float x, float y, float sx, float sy, int r, color c)

    As the second constructor above, already has "int r, color c", so I don't see the purpose of the first constructor. I just don't get it.


    • GoToLoop

    So array is an object, an instance of the class ArrayType?

    Instantiated by "{", the curly brackets/braces? And since it doesn't use a formal datatype name, instead uses "[]" to identify itself as an array?

    Take this as an example:

    Ball [] ball

    So the class here is "Ball". The object created from this class will be "ball". This object, will reside inside the array object? And that array object, will be of class type "ArrayType"? Instead of "ArrayType" being written, we have "[]"?

    The ball object is instantiated by using "new Ball()", and the array object is instantiated by using "{ }" or Ball [] ball = new ball[size] ?

    And the array object, uses "[]" to access the fields in the object? i.e ball[4] = ?


    • To both:

    I understand both of what you're saying in regards to learning. It was actually me who was asking a lot of questions regarding the details behind the scene, and GoToLoop saw that I was interested in learning about this, and responded accordingly.

    Also I did benefit a lot from learning about fields, which I wouldn't of otherwise knew about, as well as other things. In the beginning, I didn't understand much from what GoToLoop was posting, but I forced myself to study the links and research, and it's given me a better understanding of the subject, and as a result, I know why some errors arise, how objects are setup, and use classes and so on. Overall it makes programming a lot easier to carry out as I know what I'm doing and why things happen the way they do.

    Also my lecturer, has made it quite clear Computer Science is a tough degree, so there is a lot of things that I must understand, in order to do well in this course.

    I also understand Chrisir point, that basically, I need to practice and focus on the tasks, and understanding about the engine, will come later. Putting it to one side, and focusing on what needs to be done now. Both of you are right.

  • 1st short constructor: just for convenience (less typing)

    Memorize, accept, move on.

    I still haven't seen your PVector array nor lines array.

    Work with the code.

  • So array is an object, an instance of the class ArrayType?

    ArrayType would be its internal name. There's no class in Java that represents the array object! It's an unique type of object!

    So the class here is "Ball". The object created from this class will be "ball". This object, will reside inside the array object? And that array object, will be of class type "ArrayType"? Instead of "ArrayType" being written, we have "[]"?

    Let's separate things! No matter whether it's Ball[], int[], String[] or PVector[][], those are array declarations.
    A variable declared as such stores a reference for an array object. The accompanying type determines the value stored in it!

  • edited May 2014

    So the class here is "Ball". The object created from this class will be "ball".

    Objects don't have a name! Objects have datatype and memory address (reference/pointer)!
    That ball label would be just the name for that particular variable!

    Ball ball, turtle, Venus;            // declare variables which can store Ball references.
    ball = turtle = Venus = new Ball();  // instantiate a Ball object and assign it to them.
    

    As you can see above, the newly instantiated Ball object is referenced by all those 3 variables equally.
    All of those 3 variables have the 1st memory address from where the contiguous mem block was dynamically allocated in the heap!
    You gotta detach/decouple variables from their stored values! That's all that variables do, store a single value! <):)

  • edited May 2014

    As the second constructor above, already has "int r, color c", so I don't see the purpose of the first constructor. I just don't get it.

    There are times we wanna provide methods & constructors the ability to accept diff. # of parameters &/or types!
    That's called overloading. Which in turn is a type of polymorphism.

    Let's take for example the class PVector:
    http://processing.org/reference/PVector.html

    As you can read there, it has 3 overloaded constructors for its 3 float fields:

    1. PVector()
    2. PVector(x, y)
    3. PVector(x, y, z)

    • If we instantiate w/ an empty argument. Those 3 fields start off w/ their default values: 0.0f.
    • W/ 2 parameters, only the z field keeps default 0.0f.
    • And finally, passing 3 parameters, we set all of the 3 fields!

    It's true that Processing devs didn't use the this() trick:
    https://github.com/processing/processing/blob/master/core/src/processing/core/PVector.java

    But they coulda done so! Take a look at my own tweak using this(): :ar!

    class PVector {
      float x, y, z;
    
      PVector() {
      }
    
      PVector(float xx, float yy) {
        this(xx, yy, 0.0);
      }
    
      PVector(float xx, float yy, float zz) {
        x = xx;
        y = yy;
        z = zz;
      }
    }
    

    Just remember, only a constructor can call another overloaded constructor! L-)

  • edited May 2014
    • Chrisir, I'm working on the code. Will try post it asap. Still struggling a little in getting it to work.

    • GoToLoop:

    Ball [] ball. //This is an array declaration.

    "ball" will be the name of the variable that will hold the reference to the object. That object, of type Ball, will reside inside the array object? So it's a case of objects within objects?

    ball = turtle = Venus = new Ball();

    They all store different references? So we can 3 objects?

    class PVector {
      float x, y, z;
    
      PVector() {
      }
    
      PVector(float xx, float yy) {
        this(xx, yy, 0.0);
      }
    
      PVector(float xx, float yy, float zz) {
        x = xx;
        y = yy;
        z = zz;
      }
    }
    

    Not sure how that works. Does the first call the second, the second call the third? Completely unsure of what the code does.

  • edited May 2014

    "ball" will be the name of the variable that will hold the reference to the object.

    To an array object; not a Ball object! Generally, I prefer plural or collective names for arrays & other storage structures though!

    That object, of type Ball, will reside inside the array object?

    More precisely, Ball references can be stored in the array object's "slots" as its indexed elements.

    So it's a case of objects within objects?

    Rather references stored inside other objects' fields!
    Objects don't go anywhere. They stay at the same block of memory for their whole lifespan!

    They all store different references? So we can 3 objects?

    It's the keyword new which instantiates 1 object. Which involves setting apart a contiguous block of memory in the heap region.
    That is called dynamic memory allocation. That's what objects are made of!

    Afterwards, new returns the reference of the just instantiated object.
    From right to left, that same reference value is assigned to those 3 variables through the assignment = operators.

  • Okay, let me try again:

    "ball" will be the name of the variable that will hold the reference to the array. That reference will be the 1st physical address where the first object is stored? As an array can contain several objects, depending on the size specified during creation of the array?

    An array is a block of memory, and within it, is a block of memory for the object?

  • Does the first call the second,

    1st 1 does nothing!

    ... the second call the third?

    Yup, w/ 3rd parameter 0! So it's equivalent if we had called using 3 parameters w/ the 3rd 1 = 0! :P

  • edited May 2014

    better use plural, since there are many balls in the array:

    Ball[] balls; // This is an array declaration
    
  • edited May 2014

    That reference will be the 1st physical address where the first object is stored?

    There's only 1 object, the array instantiated w/ new!

    As an array can contain several objects,...

    An array can only contain values of a single type!

    An array is a block of memory,

    All objects are contiguous memory blocks! It's not array exclusive!!!

    ... and within it, is a block of memory for the object?

    Each object has an allocated block of memory in the moment of its creation. And they stay there until destroyed!
    An array "slot" is like a field variable. Each 1 merely stores a value of the declared type!

  • edited May 2014

    So:

     PVector() { //does nothing
      }
    
      PVector(float xx, float yy) { //passes the first 2 parameters when the constructor is called in setup() to "this" & 3rd constructor? 
        this(xx, yy, 0.0);
      }
    
      PVector(float xx, float yy, float zz) { // receives a call from the 2nd constructor, and the 2 parameters. Then assigns it to x, y. Since it received no 3rd parameter for float zz, z is assigned with 0.0f
        x = xx;
        y = yy;
        z = zz;
      }
    }
    

    I didn't understand the purpose of:

    this(xx, yy, 0.0);

    Now, regarding array of objects, that was not understood. Maybe because of my understanding of array is wrong. Here is what I understood regarding array of ints:

    1

  • oh dear, that's leading nowhere...

  • I don't think any of this will be asked in the exam at hand...

  • edited May 2014

    Ok, so I'm working my way, from several variables to finally getting to arrays, the link you gave me so that I can learn about arrays:

    http://wiki.processing.org/w/From_several_variables_to_arrays

        // First ball parameters
    float posX1, posY1; // Position
    float speedX1, speedY1; // Movement (linear)
    int radius1; // Radius of the ball
    color ballColor1; // And its color
    
    // Second ball parameters
    float posX2, posY2;
    float speedX2, speedY2;
    int radius2;
    color ballColor2;
    
    void setup()
    {
      size(600, 400);
      smooth();
    
      // Initialize the ball's data
      posX1 = 120;
      posY1 = 50;
      speedX1 = -2;
      speedY1 = 3;
      radius1 = 24;
      ballColor1 = #002277;
    
      // Again for the second ball
      posX2 = 220;
      posY2 = 150;
      speedX2 = 2;
      speedY2 = -3;
      radius2 = 32;
      ballColor2 = #007722;
    }
    
    void draw()
    {
      // Erase the sketch area with some light color
      background(#AAFFEE);
      // Compute the new ball position
      moveBall1();
      // And display it
      displayBall1();
      // And again for the second ball
      moveBall2();
      displayBall2();
    }
    
    void moveBall1()
    {
      // Move by the amount determined by the speed
      posX1 += speedX1;
      // Check the horizontal position against the bounds of the sketch
      if (posX1 < radius1 || posX1 > width - radius1)
      {
        // We went out of the area, we invert the h. speed (moving in the opposite direction)
        // and put back the ball inside the area
        speedX1 = -speedX1;
        posX1 += speedX1;
      }
      // Idem for the vertical speed/position
      posY1 += speedY1;
      if (posY1 < radius1 || posY1 > height - radius1)
      {
        speedY1 = -speedY1;
        posY1 += speedY1;
      }
    }
    
    void displayBall1()
    {
      // Simple filled circle
      noStroke();
      fill(ballColor1);
      ellipse(posX1, posY1, radius1 * 2, radius1 * 2);
    }
    
    void moveBall2()
    {
      posX2 += speedX2;
      if (posX2 < radius2 || posX2 > width - radius2)
      {
        speedX2 = -speedX2;
        posX2 += speedX2;
      }
      posY2 += speedY2;
      if (posY2 < radius2 || posY2 > height - radius2)
      {
        speedY2 = -speedY2;
        posY2 += speedY2;
      }
    }
    
    void displayBall2()
    {
      noStroke();
      fill(ballColor2);
      ellipse(posX2, posY2, radius2 * 2, radius2 * 2);
    }
    

    I understood the above code. On the link, it said:

    You can make one displayBall() routine with parameters: void displayBall(color ballColor, float posX, float posY, int radius) { noStroke(); fill(ballColor); ellipse(posX, posY, radius * 2, radius * 2); }

    I tried to do that on the code above, it didn't work. How do I make 1 displayBall() and 1 moveBall()?

  • show your trials - or move on to objects

  • Here is my attempt - failed:

    void draw()
    {
      // Erase the sketch area with some light color
      background(#AAFFEE);
      // Compute the new ball position
      moveBall(speedX1, speedY1, posX1, posY1, radius1);
      moveBall(speedX2, speedY2, posX2, posY2, radius2);
    
      // And display it
      displayBall(ballColor1, posX1,posY1, radius1);
       displayBall(ballColor2, posX2,posY2, radius2);
    
    }
    
    void moveBall(float speedX, float speedY, float posX, float posY, int radius)
    {
      // Move by the amount determined by the speed
      posX += speedX;
      // Check the horizontal position against the bounds of the sketch
      if (posX < radius || posX > width - radius)
      {
        // We went out of the area, we invert the h. speed (moving in the opposite direction)
        // and put back the ball inside the area
        speedX = -speedX;
        posX += speedX;
      }
      // Idem for the vertical speed/position
      posY += speedY;
      if (posY < radius || posY > height - radius)
      {
        speedY = -speedY;
        posY += speedY;
      }
    }
    
    void displayBall(color ballColor, float posX, float posY, int radius)
    {
      noStroke();
      fill(ballColor);
      ellipse(posX, posY, radius * 2, radius * 2);
    }
    
  • How does it fail?

  • The balls are not moving at all.

Sign In or Register to comment.