import javax.swing.*;//The AWT is imported since it defines many classes that GUIs use.
import java.awt.*;
@SuppressWarnings("serial")
public class Heap extends JPanel{
private int start_x;
private int start_y;
private int width;
private int height;
private int num_nodes;
private int[] array;//array to read from to display on each node
private Heap_Node[] node_arr;//arr of Heap Node objs which stores data from this.array (passed as parameter in constructor)
public Heap(int n, int startX, int startY, int width, int height, int[] arr )
{
this.array = arr;
this.num_nodes = n;
this.start_x = startX;
this.start_y = startY;
this.width = width;
this.height = height;
this.node_arr = new Heap_Node[ arr.length ];
}
public int get_num_nodes(){return this.num_nodes;}
public void paintComponent(Graphics g)
{
super.paintComponent(g);//why do I need this again?
Graphics2D g2d = (Graphics2D)g;
int links_start_pt = this.width / 2;//each L, R links (the lines) starting point (which is half width of invisible square used to draw circle
int cur_num_nodes_at_this_level = 0;//reset to 0 for each new level b/c no node inserted in the new level yet
int level_capacity = 1;//doubles for each next level
int first_node_startX = this.start_x;//x position for each level's first node
int rem_node_startX = this.start_x;//x position for each level's non-first nodes
int first_node_startY = this.start_y;
int i = 1;//current array index (index 0 not being used here)
int first_label_startX = this.start_x + 10;//starting x position for node data
int first_label_startY = this.start_y + 16;//starting y position " "
int rem_label_startX = first_label_startX;
int j = 0;//cur level
System.out.println("level " + j + "'s capac
while ( this.num_nodes > 0 && this.num_nodes <= 15 )//draw the black outline around yellow nodes which is a slightly larger black filled circle drawn first
{
if ( cur_num_nodes_at_this_level == 0 )//if this is the first node of current level
{
//draw black border around yellow circle (it's really a slightly black circle)
g2d.setColor(Color.black);
g2d.fillOval(first_node_startX - 2, first_node_startY - 2, this.width + 2, this.height + 2 );
//draw yellow node
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( first_node_startX, first_node_startY, this.width , this.height, this.array[i] ,"yellow" );
System.out.println("Node " + i + ": " + this.node_arr[i].get_data());
//draw JLabel with cur array elem
g2d.setColor(Color.black);
g2d.drawString("" + this.node_arr[i].get_data(), first_label_startX, first_label_startY);//draw the node keys\data
//draw cur node's L, R links
g2d.setColor(Color.black);
g2d.drawLine(first_node_startX + links_start_pt, first_node_startY + this.height, first_node_startX - links_start_pt, first_node_startY + 50 );
g2d.drawLine(first_node_startX + links_start_pt, first_node_startY + this.height, first_node_startX + this.width + links_start_pt , first_node_startY + 50 );
cur_num_nodes_at_this_level++;
this.num_nodes--;
}
else//draw at remaining nodes startX since first_node_start_x needs to be kept for updating startX for next level's first node
{
rem_node_startX = rem_node_startX + 68;//Q: what's the offset?
rem_label_startX = rem_label_startX + 70;
//draw black border around yellow circle (it's really a slightly black circle)
g2d.setColor(Color.black);
g2d.fillOval(rem_node_startX - 2, first_node_startY - 2, this.width + 2, this.height + 2 );
//draw yellow node
g2d.setColor(Color.yellow);
g2d.fillOval( rem_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( rem_node_startX, first_node_startY, this.width , this.height, this.array[i] ,"yellow" );
System.out.println("Node " + i + ": " + this.node_arr[i].get_data() );
//draw JLabel with cur array elem
g2d.setColor(Color.black);
g2d.drawString(""+this.node_arr[i].get_data(), rem_label_startX, first_label_startY);//draw the node keys\data
//draw cur node's L, R links
g2d.setColor(Color.black);
g2d.drawLine(rem_node_startX + links_start_pt, first_node_startY + this.height, rem_node_startX - links_start_pt, first_node_startY + 50 );
g2d.drawLine(rem_node_startX + links_start_pt, first_node_startY + this.height, rem_node_startX + this.width + links_start_pt, first_node_startY + 50 );
cur_num_nodes_at_this_level++;
this.num_nodes--;
}
if ( cur_num_nodes_at_this_level == level_capacity )//once level capacity reached, we'll proceed to draw nodes @ next level (where capacity is twice cur level's)
{
level_capacity = 2 * level_capacity;
cur_num_nodes_at_this_level = 0;//reset cur nodes inserted in current level
//update positions for next level's node's startX, startY
first_node_startX = first_node_startX - 44;//Q: how much to offset?
first_node_startY = first_node_startY + 68;//Q: how much to offset?
rem_node_startX = first_node_startX;
//update startX, startY for labels (i.e. node keys\data) for next level
first_label_startX = first_node_startX + 2;
first_label_startY = first_node_startY + 20;
rem_label_startX = first_label_startX;
j++;
System.out.println("level " + j + "'s capacity: " + level_capacity);
}
i++;
}
System.out.println("Final value of i: " + i );
System.out.println("Array size: " + this.array.length );
System.out.println("Heap size: " + this.node_arr.length );
System.out.println(this.num_nodes);
// print_heap();//PROBLEM: why do I have to print inside the paintComponent, but if I call this method in main, it prints null for this node_arr (arr of Heap_Node objs)?
}
public void print_heap()//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("Current Data: " + this.node_arr[i].get_data() );
else
System.out.println("Current 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??
}
}
}