I have a class Pellets, it is the coordinates of the points of impact on the target and other supporting information.

In the program, I create an ArrayList<Pellets> and add hit points to it, but there is one problem - the center of the target.

In the program, I set the center of the target, this is the corresponding Pellets object, respectively, and my points of impact go to this center. Those. conditionally speaking, an array of Pellets is created in which the first element is the center of the target, and all the rest are already hits, but this is somehow not beautiful.

Please tell me how to implement in Java so that you can refer to this group like this:

 Target.getCenter.getX <- выдает Х координату центра, Target.GetPellets(i).getX <- выдает Х координату попадания. 

 public class Pellets { private int x, y, scale, size; private Color color; private int halfSize; Pellets(int xPellets, int yPellets, int scalePellets, int sizePellets, Color cPellets){ x = xPellets; y = yPellets; scale = scalePellets; size = sizePellets; color = cPellets; } public int getX(){ return x; } public int getY(){ return y; } public int getScale(){ return scale; } public int getSize(){ return size; } public Color getColor(){ return color; } public int getHalfSize() { halfSize = size / 2; return halfSize; } } 

    1 answer 1

    Create a Target class:

     public class Target { private Pellets mCenter; private ArrayList<Pellets> mPellets; public Target(Pellets pellets) { mCenter = pellets; mPellets = new ArrayList<>(); } public void addPellet(Pellets pellets) { mPellets.add(pellets); } public Pellets getPellet(int i) { return mPellets.get(i); } public Pellets getCenter() { return mCenter; } } 

    Then you can create an object of the Target class:

     Pellets center = new Pellets(...); Target target = new Target(center); 

    and add new hit points to target :

     Pellets pellets = new Pellets(...); target.addPellet(pellets); 

    or receive them and their attributes:

     int x = target.getPellet(1).getX(); 

    I also recommend that the Target class implement a check, for example, in the getPellet(...) method for the presence of an element with a passed index, and the like.