When accessing the bounds of an Area from an ArrayList<Area> outside of the class it was initiated in, the Rectangle received is incorrect

StackOverflow https://stackoverflow.com/questions/17507430

سؤال

I have worked through this code a number of times but cannot seem to understand it's behaviour.

I add a number of instances of Area to an ArrayList in a for loop, whilst also creating another Area which is an amalgamation of all those added to the array (however this amalgamated Area is never added to the ArrayList).

When trying to retrieve the first item of the ArrayList from another class it gives me the bounds of the amalgamated Area which had never been added in the first place?!?!

import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;


public class CorruptArray {
public ArrayList<Area> arrayList = new ArrayList<Area>();

public CorruptArray(){
        double x = 100.0;
        double y = 200.0;
        double width = 100.0;
        double height = 200.0;
        Area map = null;

        int i = 5;
        for(int j =0 ; j<i; j++){
            arrayList.add(new Area(new Rectangle2D.Double(x,y,width,height)));

            x+=10;
            y+=10;
            width+=10;
            height+=10;
        }

        int k = 0;
        for(Area area : arrayList){
            System.out.println("INITIAL BOUNDS ON ENTRY:  " + area.getBounds().toString());
            if(k == 0){
                map = area;
                k++;

            }else{
                map.add(area);

            }
            System.out.println("OVERALL JOINED AREA - map   " + map.getBounds().toString() + "\n");
        }

        TestArray test = new TestArray(this);
    }

private static class TestArray{
    TestArray(CorruptArray ca){
        System.out.println("\n\n");
        for(Area area : ca.arrayList){
            System.out.println("RETRIVAL OF BOUNDS FROM TestArray:  " + area.getBounds().toString());
        }
    }
}



public static void main(String args[]){
    new CorruptArray();
}

When running the programme this is the output I get:

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=100,y=200,width=100,height=200]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=100,height=200]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=110,y=210,width=110,height=210]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=120,height=220]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=120,y=220,width=120,height=220]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=140,height=240]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=130,y=230,width=130,height=230]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=160,height=260]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=140,y=240,width=140,height=240]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=180,height=280]




RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=100,y=200,width=180,height=280]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=110,y=210,width=110,height=210]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=120,y=220,width=120,height=220]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=130,y=230,width=130,height=230]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=140,y=240,width=140,height=240]
هل كانت مفيدة؟

المحلول

You don't add the amalgamated area, but you take the first area from the arraylist and modify it. It happens exactly here:

if (k == 0) {
   map = area;

Later your add the other areas from the list to the map - which is the first area on the list. You may want to create a new area based on the first one from the list instead:

if (k == 0) {
   map = new Area(area);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top