Separate objects in an ArrayList

please format code with </> button * homework policy * asking questions

Hello fellow processing members,
I am working on a project where Iam trying to make a program using an ArrayList.
The goal is to have a square at x:100 y:100 and being able to drag it to a new location, once I have dragged it to that new location the ArrayList has to add an object to the list and put a new square on the screen at x:100 y:100 to drag to a new location.

I know how to make the list and add objects, but I cant separate the object from each other. They stay in the same place.
Edit: I am new here sorry if I defined the code wrong wit the “<>”

my main code is looking like this so far:

 int x = 100;
int y = 100;
int sz = 100;
boolean isGekozen;
ArrayList<Tegenstander> tegenstanders;

void setup(){
 fullScreen();
 background(0);
 tegenstanders = new ArrayList<Tegenstander>();
 tegenstanders.add(new Tegenstander());
}

void draw(){
 background(0);
    
  
 for (int i = tegenstanders.size() - 1; i >= 0; i--) {
  Tegenstander t = tegenstanders.get(i);
  t.display();
  t.isGekozen();
  
}
    
  println(tegenstanders.size());
   
}

The class:

class Tegenstander{

void display(){
  fill(100);
  
  rect(100, 100, sz, sz);
}
boolean isGekozen (){
  fill(100);
      rect(x, y, sz, sz);
  if(mouseX> x && mouseX < x+100 && mouseY > y && mouseY < y+100){
    cursor(HAND);
    stroke(255);
    if(mousePressed){
      strokeWeight(5);
      x = mouseX-50;
      y = mouseY-50;
      return true;
       }
    else{
      strokeWeight(2);
      return false;
    }
  }
  else{
    cursor(ARROW);
    noStroke();
    return false;
  }
}


}
1 Like

https://discourse.processing.org/faq#format-your-code

If it is formatted correctly it is easy to read and we can easily copy it and try it out.

:)

1 Like

Am I missing something, it looks like you have no Tegenstander class to initialize. Your code should work providing that you first create a class.

also you will need a mechanism that tracks the mouse location, checks if its pressed, then checks the location again on release.

A similar question has been asked recently about drag and drop.

i find it a little difficult to see through your code. apparently there is a class lurking in the back that takes care of when the first object has been moved. So you leave out the important part: what does you code actually do when its time to create a new object and thus a new entry in your list.

i assume that you will probably need to store the position of each object somewhere.
if it is a class, you will store the x,y coordinates in that object itself.

so the process would look something like this:

  1. create a new object at 100, 100.

  2. move the object to a new location
    -> when the object is placed (mouseRelease)
    a) update the coordinates of THIS object to the new coordinates
    b) create a new instance of the object at 100,100 by adding it to the ArrayList
    c) object count += 1

  3. repeat from step 2

Maybe you’re not updating each objects x,y values ?

As you seem to use the reference example for ArrayList I also notice that you do not declare the list by specifying its intended content:

ArrayList tegenstanders; should maybe be: ArrayList '<'Tegenstander> tegenstanders
(the ‘<’ is there so the editor here doesn’t think its a code :wink: its of course <)
same goes for saying tegenstanders = new ArrayList(); in setup():

personally I would not name the arrayList so similar to your class name, its confusing…

2 Likes

Thank you Iwil do that

I have the class and the drag and drop code I will put that one up too

How do I store those X and Y values in the object in the list? I will put the class after my main code too

first of all, this might be interesting to you: https://processing.org/examples/variablescope.html

depending on where you declare your variables they can be accessed (or not) from different methods. everything outside setup() and draw() will be visible from everywhere (aka global variable).

values you declare inside your class “belong” to it, since they are only accessible from there. (more or less :-))

a class would look like this:

class Tegenstander {
//--------------------- variables "inside" the class' {}
float x;
float y;
//--------------------- a constructor - used to initialize an instance of the class
  Tegenstander () {
  }
//--------------------- class methods
  void update() {
  }
}

as you already have done, you declare that there IS an object of this class first
Tegenstander someName;. similar to saying: int a;

then you actually “instantiate” your object: someName = new Tegenstander();
(or in your case even make a list of many of these, as you already have done.

so every object you create has the methods and the variables at its disposal that you have described inside your class{}

typically, from the “inside” your methods can happily use your variables:

void update () {
x = 232134239; 
// calculate wonderful stuff here, change things
}

from the outside you cannot access the variables, unless you make specific methods for it (I hear that’s good practice…)

int getX () {
return x;
}

so you might say (for example in draw())

int a = someName.getX(); retrieving the value from the object or, if you have many instances (e.g. in an array) int a = someName[n].getX(); where you have to know the correct object you want to address.

just as you did with calling the display() method in your code with t.display()

finally you can access the field (that’s what that variable is usually called) directly by writing:
int a = nyName.x;
or change it
myName.x = 777;

3 Likes

First of all thanks for your reply, I know what I have to do now.
I understand what you are telling me and I know this would help me a lot, but I dont know how to do what you are telling me.

You wrote this line inside the class but it confuses me.
what does this line inside the class do? Also how do I call it in the main code?

Tegenstander(){

}

After trying what you suggested I got an error when I typed this line in processing because “someName” isn’t an array.

int a = someName[n].getX();

How do I fix that error or did I miss something in my code?

my maincode looks like this now:

ArrayList<Tegenstander> tegenstanders;
Tegenstander someName = new Tegenstander();

void setup(){
  fullScreen();
  background(0);
  tegenstanders = new ArrayList<Tegenstander>();
  tegenstanders.add(new Tegenstander());
 
}

void draw(){
  background(0);
    
  
 for (int i = tegenstanders.size() - 1; i >= 0; i--) {
  Tegenstander t = tegenstanders.get(i);
  t.update();
  t.getX();
  t.getY();
  }
    
  println(tegenstanders.size());
  
    
}

And like you said I made a void update and the getX function in my class. You wrote:

float x;

int getX(){
return x;
}

Is that possible? shouldn’t they be either a float or an int?

class Tegenstander{
int x;
int y;

Tegenstander(){
}
    
void update(){
  fill(100);
  
  rect(x, y, 100, 100);
  if(mouseX> x && mouseX < x+100 && mouseY > y && mouseY < y+100){
    cursor(HAND);
    stroke(255);
    if(mousePressed){
      strokeWeight(5);
      x = mouseX-50;
      y = mouseY-50;      
       }
    else{
      strokeWeight(2);      
    }
  }
  else{
    cursor(ARROW);
    noStroke();    
  }
  
}
int getX(){
  return x;
  }
  
int getY(){
  return y;
}

}
1 Like

This is the constructor of your class.

It is called whenever you write: new Tegenstander();
first you “declare” that there is a variable or an object, giving it a name and telling the application what kind of “thing” it is.

int a; says: “I name a variable ‘a’ and it holds an integer”
Tegenstander anyNameYouWant, does the same, only that its type is that of the class you wrote yourself.

it is executed once, in the beginning. that’s what makes this particular object come alive and be actually usable to you. that’s what is usually called an ‘instance’ of a class.

After trying what you suggested I got an error when I typed this line in processing because “someName” isn’t an array.

int a = someName[n].getX();

I just wanted to use a different name than yours, to point out that it could be any.
now you have a little confusion in your code…

  1. make an ArrayList -> its name is ‘tegenstanders’ -> you declare (!) your intention
  2. instantiate that ArrayList -> tegenstander = new ArrayList<Tegen… -> you create an actual object (the array list)
  3. tegenstanders.add(new Tegenstander()) -> you create the actual object and add it to the list

You don’t need a ‘someName’ object, what you actually do is happening later when you use:

Tegenstander t = tegenstanders.get(i);

here you name a new object ‘t’.

so you have to use that name:
int a = t.getX();
(haha and yes, I was a little sloppy there, if the variable is a float, the method must return one too. so it should be 'float getX() {…)

as far as I am concerned, I don’t like ArrayLists, because you have to tease out the objects inside to use them.
So you make an Arraylist, add objects to them and so on.
Later you have to get each object from that list and shove it into another one.

for (int i = tegenstanders.size() - 1; i >= 0; i–) {
Tegenstander t = tegenstanders.get(i);
t.update();
t.getX();
t.getY();
}

Of course its an advantage to not have to know how large the list will be.
But I prefer to work with arrays…

for (int i = tegenstanders.size() - 1; i >= 0; i–) {
t[i].update();
t[i].getX();
t[i].getY();
}

but that’s just a personal flaw of mine :slight_smile:

3 Likes

Thanks for your help, it worked!
It is not flawless, if make a new object it makes like 5 at a time and the change in strokeweight and mouse when you drag is glitchy after about 2 or 3 squares.

for (int i = tegenstanders.size() - 1; i >= 0; i–) {
t[i].update();
t[i].getX();
t[i].getY();
}

this did not work though… without “[i]” it does.

ArrayList<Tegenstander> tegenstanders;


void setup(){
  fullScreen();
  background(0);
  tegenstanders = new ArrayList<Tegenstander>();
  tegenstanders.add(new Tegenstander());
 
}

void draw(){
  background(0);
    
  
 for (int i = tegenstanders.size() - 1; i >= 0; i--) {
  Tegenstander t = tegenstanders.get(i);
  t.update();
  t.getX();
  t.getY();
  }
    
  println(tegenstanders.size());
  
  
  if(keyPressed && key == 'v'){
    tegenstanders.add(new Tegenstander());
  }
  
    
}
class Tegenstander{
int x;
int y;
Tegenstander(){
   x = 100;
   y =100;
}
    
void update(){
  fill(100);
  
  rect(x, y, 100, 100);
  if(mouseX> x && mouseX < x+100 && mouseY > y && mouseY < y+100){
    cursor(HAND);
    stroke(255);
    if(mousePressed){
      strokeWeight(5);
      x = mouseX-50;
      y = mouseY-50;      
       }
    else{
      strokeWeight(2);      
    }
  }
  else{
    cursor(ARROW);
    noStroke();    
  }
  
}
int getX(){
  return x;
  }
  
int getY(){
  return y;
}

}
1 Like

yes, this does not work for an array list, but with an array :slight_smile:

Aaaah Like so. The problem with an array is that for my project I don’t want a predetermined number inside the array, but you are right an array would be eassier

i hate array lists :smiley:

Yet you seem to know a lot about them. Thanks for helping me!

Know your enemies I always say ! Arrrrr !

Lmao you are very wise