That was a typo*** it should be:
this.node_array[i] = new Node(xPosition, yPosition, this.array[i] )
I have a separate Node class with constructor that has an int x and int y position because I would like to build my nodes to be separate objects to swap each node's value and the 3rd parameter of the Node constructor above which is the data value from the int array[] data member which is just a regular array with the values hardcoded (for now anyways).
*So it's the problem I have with why in the output method above in the Heap class, it outputs null for each entry in the this.node_array if I am clearly inserting a new Node objet in the code snippet in this post and OP. Appreciate any help.
At this stage, I just want to be able to output the nodes in the this.node_array array.
import java.awt.*;
import javax.swing.*;
public class Heap extends JPanel
{
private int xPosition;//initialized in constructor
private int yPosition;//same
private int array[];//same
private Node node_array[];//same
private int nodes_to_draw;//same (in main a hard coded int array for now to use bottom-up build heap)
//public Heap constructor
public void paintComponent(Graphics g)
{
Graphics2d g2d = (Graphics2d)g;
while ( nodes_to_draw > 0 )
{
//if still nodes to draw at this level
g2d.setColor(Color.yellow);
g2d.fillOval( first_node_startX, first_node_startY, this.width, this.height );
//add this position to cur Heap_Node obj (most important is the data and color to change during heap sort)
this.node_arr[i] = new Heap_Node( xPosition, yPosition, width , height, this.array[i] );
System.out.println("Node " + i + ": " + this.node_arr[i].get_data());//I can output the data at each node, but then it's always null in the output
// each node's data in the output method below which is where I am stuck
//if maximum number of nodes reached to draw at current level
//update total nodes to draw at next level
}
}
public void output_heap_data()//Problem here, I am unsure why it outputs null if I am inserting into this.node_arr in the paintComponents method above
{
for ( int i = 1; i < this.node_arr.length; ++i )
{
if ( this.node_arr[i] != null )
System.out.println("Data: " + this.node_arr[i].get_data());
else
System.out.println("Data: " + null);//PROBLEM I have is here: why is it null if I'm inserting into this.node_arr array in the paintComponents method above??
}
}
}
}
ALso, the reason I want to store the xPosition and yPosition in each Node object is to be able to find the graphic nodes drawn so that I can find the coordinates to change the value if I need to swap (for heap sort) by updating what the g.drawString method displays but if there is a simpler way I'd appreciate since I am just getting into Swing applets but I find it interesting