Hi, I'm trying to build a heap sort Swing application and I decided to keep it simple for now so I'll only heap sort 3 nodes so I got the nodes (yellow circles) to draw and show up as a tree shape (binary heap), and the heap sorting not animation not started yet since I need to display the initial binary heap first but for some reason, when you minimize the Swing application, the yellow circles re-align, why is that so? The complete program below that compiles and runs, appreciate all the help.
import javax.swing.*; import java.awt.*; import javax.swing.Timer; import java.awt.event.*; import java.lang.reflect.InvocationTargetException; @SuppressWarnings("serial") public class Heap_Graphical extends JFrame { private int node_height = 30;//imaginary rectangle's ht to draw the circle (node) private int node_width = 30;//imaginary rectangle's width to draw the circle (node) private int canvas_width = 360;//width of JFrame private int canvas_height = 360;//height of JFrame private int start_x = (canvas_width/2) - node_width/2; private int start_y = 10; private int delay = 250; private Color color_arr[] = {Color.BLUE, Color.GREEN}; private int cur_index = 0; private int iterations = 0; private Timer t = null; private Color cur_color; private ActionListener updateTask; private int incrementer = 0;//used to alternate b/t odd et even int for which color to use private int level = 0; private int level_capacity = 0; private boolean isInitialHeapCreated = false; public Heap_Graphical( ) { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); this.add( new Canvas() ); this.pack(); } //draw the initial binary heap here only private class Canvas extends JPanel { public Canvas() { setBorder(BorderFactory.createLineBorder(Color.black)); } public Dimension getPreferredSize() {//overridden method (to draw inside Canvas, NOT JFrame) return new Dimension(360,360); } public void paintComponent(Graphics g) { super.paintComponent(g); final Graphics2D g2d = (Graphics2D)g; //1st step: draw initial heap //Prob: Why do the nodes change position when the Java application is minimized? if ( !isInitialHeapCreated )//don't redraw initial heap each time (I think this is not proper way, look up) { int num_nodes_drawn = 0; int level_capacity = (int)Math.pow(2, level);//initially @ level 0 we can hold a max of 2^1 or 1 node int cur_num_nodes_at_cur_level = 0;//always reset to 0 int subsequent_x_offset = start_x; //vars for dealing w/ drawing the txt graphic while ( num_nodes_drawn < 3 )//3-nodes for now { //when cur level reached node capacity, update next level's capacity and also update the new position for // all nodes to be located in next level if ( cur_num_nodes_at_cur_level == level_capacity ) { level += 1; level_capacity = (int)Math.pow(2, level);//update level capacity cur_num_nodes_at_cur_level = 0;//reset for next level where initially no node drawn for next level yet start_x -= 25; start_y += 45; subsequent_x_offset = start_x;//reset x-offset to be based on where 1st node of new level is } //draw first node of new level if ( cur_num_nodes_at_cur_level == 0 )//if there are still nodes and no nodes drawn for cur level yet { g2d.setColor(Color.YELLOW); g2d.fillOval(start_x, start_y, node_width, node_height); g2d.setColor(Color.BLACK); g2d.drawOval(start_x, start_y, node_width, node_height); cur_num_nodes_at_cur_level++; } else//draw sub-sequent nodes at cur level (displaced by some fixed x-position val) { subsequent_x_offset += 48; g2d.setColor(Color.YELLOW); g2d.fillOval(subsequent_x_offset, start_y, node_width, node_height); g2d.setColor(Color.BLACK); g2d.drawOval(subsequent_x_offset, start_y, node_width, node_height); cur_num_nodes_at_cur_level++; } num_nodes_drawn++; } }//END IF //2nd step: animate heap sort (add corresponding arr to show currently sorted end later) (this needs to use the actual // heap sort of arr to determine how to re-draw the heap sorting animation ) (not sure how to incorporate animation // after initial heap drawn since the timer needs to be listener in constructor so need to use some boolean...) } } //entry point public static void main(String[] args) throws InvocationTargetException, InterruptedException { SwingUtilities.invokeLater(new Runnable() {//Q: what' difference w/ SwingUtitilities.invokeLater vs EventQueue.invokeLater? (look up later) public void run() { new Heap_Graphical(); } }); } }