Weird Behavior with Java Objects: Supposedly the same process, different result : I am in dire need of help involving this weird behavior in Java Objects.

I have this ComponentPlane.class with two different versions. Difference is marked by (******)

First WORKING Version

    package app.pathsom.som.output;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JPanel;

    import app.pathsom.som.map.Lattice;
    import app.pathsom.som.map.Node;

    public class ComponentPlane extends JPanel{

	private Lattice lattice;
	private int componentNumber;
	private double minValue;
	private double maxValue;
	private double origMinValue;
	private double origMaxValue;
	
	public ComponentPlane(Lattice lattice, int componentNumber){
		this.lattice = new Lattice();
		this.componentNumber = componentNumber;
		initLattice(lattice);
		initComponentPlane();
	}
	
	private void initLattice(Lattice lattice){
		this.lattice.setLatticeHeight(lattice.getLatticeHeight());
		this.lattice.setLatticeWidth(lattice.getLatticeWidth());
		this.lattice.setNumberOfNodeElements(lattice.getNumberOfNodeElements());
		this.lattice.initializeValues();
		this.lattice.setNodeHeight(lattice.getNodeHeight());
		this.lattice.setNodeWidth(lattice.getNodeWidth());
		
		this.lattice.setTotalNumberOfNodes(lattice.getTotalNumberOfNodes());
		
		for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
			******this.lattice.getLatticeNode()[i] = new Node(lattice.getLatticeNode()[i]);******
		}
		
	}
	
	public Lattice getLattice(){
		return lattice;
	}
	
	private void initComponentPlane(){
		maxValue = lattice.getLatticeNode()[0].getDoubleElementAt(componentNumber);
		minValue = lattice.getLatticeNode()[0].getDoubleElementAt(componentNumber);
		for(int i = 1; i < lattice.getTotalNumberOfNodes(); i++){
			if(lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber) < 
					minValue){
				minValue = lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber);	
			}
			if(lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber) > maxValue){
				maxValue = lattice.getLatticeNode()[i].getDoubleElementAt(componentNumber);
			}
		}
		
		for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
			Node currNode = lattice.getLatticeNode()[i];
			int colorValue = (int)Math.round((currNode.getDoubleElementAt(componentNumber)
					- minValue) / (maxValue - minValue) * 1020);
			Color nodeColor = new Color(0);
			int caseValue = colorValue/256;
			
			if(caseValue == 0)
				nodeColor = new Color(0, (colorValue % 256), 255);
			else if(caseValue == 1)
				nodeColor = new Color(0, 255, 255 - (colorValue % 256));
			else if (caseValue == 2)
				nodeColor = new Color((colorValue % 256), 255, 0);
			else nodeColor = new Color(255, 255 - (colorValue % 256), 0);
			
			lattice.getLatticeNode()[i].setNodeColor(nodeColor);
		}
		
	}
	
	public double getMaxValue(){
		return maxValue;
	}
	
	public double getMinValue(){
		return minValue;
	}
	
	public void setOrigMaxMin(double maxValue, double minValue){
		this.origMaxValue = this.maxValue * (maxValue - minValue) + minValue;
		this.origMinValue = this.minValue * (maxValue - minValue) + minValue;
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.scale(0.5, 0.5);
		System.out.print("component: " + componentNumber);
		for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
			g.setColor(lattice.getLatticeNode()[i].getNodeColor());
			g.fillRect(lattice.getLatticeNode()[i].getxPos() - lattice.getNodeWidth() /2, 
					lattice.getLatticeNode()[i].getyPos() - lattice.getNodeHeight() /2
					, lattice.getNodeWidth(), lattice.getNodeHeight());
			g.setColor(Color.BLACK);
			g.drawRect(lattice.getLatticeNode()[i].getxPos() - lattice.getNodeWidth() /2, 
					lattice.getLatticeNode()[i].getyPos() - lattice.getNodeHeight() /2
					, lattice.getNodeWidth(), lattice.getNodeHeight());
		}
		System.out.println();
		
		g.setColor(Color.BLACK);
		double hund = 100;
		int intMaxValue = (int) (origMaxValue*hund);
		int intMinValue = (int) (origMinValue*hund);
		g.setFont(new Font("Arial", Font.PLAIN, 18));
		g.drawString(Double.toString(intMinValue/hund), 0, 470);
		g.drawString(Double.toString(intMaxValue/hund), 405, 470);
		
		for(int i = 0; i < 1024; i++){
			if(i < 256) g.setColor(new Color(0, (i % 256), 255));
			else if(i < 512) g.setColor(new Color(0, 255, 255 - (i % 256)));
			else if(i < 768) g.setColor(new Color((i % 256), 255, 0));
			else g.setColor(new Color(255, 255 - (i % 256), 0));
			double width = 350;
			g.fillRect((int)Math.ceil(i * width/1024) + 50, 455, (int)Math.ceil(width/1024), 15);
		}
		
		g.setColor(Color.BLACK);
		g.drawRect(50, 455, 350, 15);
	}
}


The only difference of the second NON-WORKING version is with this FUNCTION REPLACING the FUNCTION above

    private void initLattice(Lattice lattice){
		this.lattice.setLatticeHeight(lattice.getLatticeHeight());
		this.lattice.setLatticeWidth(lattice.getLatticeWidth());
		this.lattice.setNumberOfNodeElements(lattice.getNumberOfNodeElements());
		this.lattice.initializeValues();
		this.lattice.setNodeHeight(lattice.getNodeHeight());
		this.lattice.setNodeWidth(lattice.getNodeWidth());
		
		this.lattice.setTotalNumberOfNodes(lattice.getTotalNumberOfNodes());
		
		for(int i = 0; i < lattice.getTotalNumberOfNodes(); i++){
			******this.lattice.getLatticeNode()[i] = lattice.getLatticeNode()[i];******
		}
		
	}


I have also tried doing a third non-working version which is...

    private void initLattice(Lattice lattice){
		this.lattice.setLatticeHeight(lattice.getLatticeHeight());
		this.lattice.setLatticeWidth(lattice.getLatticeWidth());
		this.lattice.setNumberOfNodeElements(lattice.getNumberOfNodeElements());
		this.lattice.initializeValues();
		this.lattice.setNodeHeight(lattice.getNodeHeight());
		this.lattice.setNodeWidth(lattice.getNodeWidth());
		
		this.lattice.setTotalNumberOfNodes(lattice.getTotalNumberOfNodes());
		
		
		******this.lattice.setLatticeNode(lattice.getLatticeNode());******
		
	}


A Constructor in the Node.class (WHICH is USED in the first WORKING version is this one...

    public Node (Node node){
		this.xPos = node.xPos;
		this.yPos = node.yPos;
		this.numOfElements = node.numOfElements;
		this.cluster = -1;
		this.nodeIndex = node.getNodeIndex();
		for(int i = 0; i < this.numOfElements; i++){
			this.addElement(node.getDoubleElementAt(i));
		}
	}


My problems are

- The First one works FINE. It creates HEATMAPS or COMPONENTPLANES for EACH component number (meaning they differ from each other) BUT I cannot USE it AS the LINE with ****** which references to the ("this.addElement....") in the Node.class constructor gives me OUTOFMEMORY error so it LAGS and FREEZES whenever I have many COMPONENTPLANES to do. (I am actually doing an ARRAY of COMPONENTPlane objects) so I decided to try the second and third option. I have already increased my heap size SO this is OUT of the question

- If I use the second and third one, I end up with no LAGS even with large amount of ComponentPlanes (probably less memory taking up because of creating new Node objects or idk) BUT these CREATES WRONG HEATMAPS. All heatmaps are the same. And the thing is, all heatmaps are LIKE the LAST ELEMENT of the ComponentPlanes array (e.g. if i have TEN ComponentPlane objects, all heatmaps look exactly like the TENTH Component Object)

This is an image...
[All of the heatmaps are like this - the same as the last heatmap in the array][1]


is there a way to make the second and third one work?
Thank you. I hope you can help me as I really need to finish this one asap so i can proceed on the bigger modules of the project. Thank you.

    


  [1]: https://i.stack.imgur.com/GPXfM.png