I want to make points relative to a Grid instance (there will be only one instance of Grid). Relative means the point coordinates will change accordingly to changes made in grid.
To build a new point I need access to data in Grid instance so I can make the proper calculations.
Also each instance point must retain a unique data referent to the state of a Grid property at the moment the point was built.
I thought in some possible approaches.
Make the points and store them inside Grid class. In a collection of some kind, or an object. That’s ok, but not very beautiful and ‘oopy’ I think. The Grid class will be doing to much things, and the work to keep track of each point will be convoluted. There will be a lot ways to create a point, they must be updated every cycle, and do some other stuff, so the class will be like “polluted”.
Make a sub class Gpoint, extending Grid. But then, i’ll have to provide the instance of Grid to every point, right? A constructor like constructor(Grid, x, y) If so, doesn’t it makes extending useless?
Make a class like Gpoints (plural) that gets an instance of Grid (the instance), and deal with storing all the points. Then make GPoint that extends Gpoints class and use super(Grid) to get a ref to the Grid instance.
Well, all that still not seems very good an answer yet…
Please advise : )
thanks.
ps:
here a skeleton i’m working with
class Gpoints {
constructor(Grid) {
this.grid = Grid;
this.Gpoints = [];
}
make_gpoint(x, y, label){
let gp = new Gpoint(x, y, label);
this.Gpoints.push(gp);
}
} //class
class Gpoint extends Gpoints {
constructor(x, y, label) {
super(grid);
this.x = x;
this.y = y;
this.gx;
this.gxy;
this.label = label;
this.g_width;
}
}//class
and then something like:
gps = new Gpoints(grid);
gp1 = gps.make_gpoint(100, 100, 'gp1');
Thanks @svan. Great example.
Actually my question is more of conceptual nature, about designing the inheritance chain. As I’m working I’m starting to think that perhaps the best is to keep the “storage” inside the grid class. But I’m not sure yet : )
Anyway thanks for your time
I don’t know if it will help you or not, but it is possible to use the code that I posted to display an array of objects with a unique id assigned to each object. When using mousePressed() the id may be retrieved.