Voltaren Istanbul Voltaren Istanbul - 22 days ago 5x
Java Question

new to java, want to create a loop that makes objects

I'm trying to make 15 different instances of a polygon with each one having a different radius, starting with 225 and each one being 15 pixels shorter than the other, The second and third fields are the coordinates of where the objects center will be places. I know the [i] doesn't work, and I have read that I should use an array, but i am having trouble implementing it to my code:

public void paintComponent(Graphics g)
Graphics2D g2 = (Graphics2D) g;

int i;
int number = 15;
int radiussize = 225;

Shape object[i]= new Shape(radiussize-(i*15),250,300);

I want the result to be 15 objects named:

object0, object1, object2...object15.


If you want to do it in a loop, you cannot have names for the variables. If you want to store them you should make the array of the objects and create them in this way:

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    int number = 15;
    int radiusSize = 225;

    Shape[] objects = new Shape[number];

    for(int i=0; i<number; i++){
        objects[i] = new Shape(radiuSsize - (i * 15), 250, 300);