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.