Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Trouble with drawing a heap

  1. #1
    Join Date
    May 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default Trouble with drawing a heap

    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;
    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.add( new Canvas() );
    	//draw the initial binary heap here only
    	private class Canvas extends JPanel
    		public Canvas() {
    	    public Dimension getPreferredSize() {//overridden method (to draw inside Canvas, NOT JFrame)
    	        return new Dimension(360,360);
    		public void paintComponent(Graphics 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.fillOval(start_x, start_y, node_width, node_height);
    						g2d.drawOval(start_x, start_y, node_width, node_height);						
    					else//draw sub-sequent nodes at cur level (displaced by some fixed x-position val)
    						subsequent_x_offset += 48;
    						g2d.fillOval(subsequent_x_offset, start_y, node_width, node_height);
    						g2d.drawOval(subsequent_x_offset, start_y, node_width, node_height);
    			}//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(); 

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    On vacation
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Trouble with drawing a heap

    when you minimize the Swing application, the yellow circles re-align, why is that so?
    What variables' values are changed? Where are they changed? Add some println statements to show when the values are changed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. heap size vs heap used
    By aueddonline in forum Java Theory & Questions
    Replies: 5
    Last Post: February 10th, 2012, 01:59 PM
  3. Heap Memory
    By vamsi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 19th, 2011, 12:48 PM
  4. binary heap
    By dabdi in forum Collections and Generics
    Replies: 0
    Last Post: November 16th, 2011, 05:43 PM
  5. Implementing a 5-heap with an array
    By TBBucs in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 12th, 2010, 10:56 PM