Hi,
Although I wouldn’t say I’m a complete novice, I’ve been unsucessfully trying to do something quite simple : add objects to an ArrayList
Code is somethin like this :
ArrayList<Object> objects = new ArrayList<Object>();
//OR//
ArrayList<Object> objects = newArrayList();
void setup()
{
}
void draw()
{
Object test = new Object();
objects.add(test);
println(objects.size());
//OR//
objects.add(new Object());
println(objects.size());
}
The println always returns 0, and trying to use a foreach loop brings up nothing, there is no error message and the code compiles just fine. I feel like I’ve missed something very simple but I can’t figure out what it is
ArrayList<Circle> circleArray = new ArrayList();
void draw()
{
if(keyPressed)
{
Circle test = new Circle(mouseX, mouseY);
circleArray.add(test);
}
println(circleArray.size());
for(Circle circles : circleArray)
{
circles.display();
}
}
The other class simply has a constructor that takes 2 floats x and y as inputs, and display() draws a circle with x and y and a fixed size.
It works just fine until I add “background(0);” at the beginning of my draw() method, after which nothing is added to my ArrayList (println returns 0.0), so I guess it has other properties
My Circle class was absolutely identical to yours besides mine using this.x and this.y instead of x and y for the display() method (not that I think it makes a difference)
However, while keyPressed didn’t work, using mousePressed || keyPressed fixed the issue, and after that, simply putting “keyPressed” was working again, so I’m confused
Here’s another one where it doesn’t work properly, it’s basically a graph you can put points on by simply clicking (graph is just there to draw the background and PrimeChecker checks whether a number’s polar coordinates are prime, they’re not used here)
//
ArrayList<Point> pointArray = new ArrayList();
int scale;
int numberOfPoints;
boolean checkPrime;
boolean placePoint = true;
void setup()
{
numberOfPoints = 400;
scale = 50;
checkPrime = true;
fullScreen();
}
void draw()
{
background(0);
translate(width/2, height/2);
GraphDrawer graph = new GraphDrawer(scale);
graph.circles();
graph.graph();
if(mousePressed || keyPressed)
{
Point test = new Point(mouseX, mouseY);
pointArray.add(test);
println(pointArray.size());
}
Point plotting = new Point(100, 100, 10);
plotting.plotter(numberOfPoints, scale, checkPrime);
for(Point points : pointArray)
{
points.display();
println("test");
}
}
//
class Point
{
ArrayList<Point> pointArray = new ArrayList();
float radius;
float angle;
float x;
float y;
int r;
int numberOfPoints;
int scale;
boolean isPrime;
boolean checkPrime;
int pouet = 0;
//Constructor for polar coordinates
Point(float _radius, float _angle)
{
radius = _radius;
angle = _angle;
}
//Constructor for carthesian coordinates
Point(float _x, float _y, int _radius)
{
x = _x;
y = _y;
radius = _radius;
}
//Display for carthesian coordinates
void display()
{
//Color and fill setup
noStroke();
fill(255, 10, 10);
//Plotting our point on the graph
println(x);
circle(x, y, radius);
}
Thanks again for your help, I fixed up a few things in my code, one of them being the one you pointed out, and it is working fine now!
As I suspected, it was a stupid mistake, but an important one nonetheless