Results 1 to 4 of 4
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Add animation to my selection sort?

    Could anyone point me in the right direction to adding animation to my selection sort program? Right now it paints the collection of items into a bar graph, then when I sort it will sort them and paint the new collection all sorted. I would like to animate the GUI to show the items being sorted. Any help is appreciated.

    Java Code:
    public class SortableDataOrganizer extends DataOrganizer {
    	
    	// a sort button
    	private Abutton sortButton;
    	// x and y position of sort button
    	private final int BUTTONX = 30, BUTTONY = 220;
    	// Size of collection two to start
    	private final int SIZE=0;
    	
    	/**
    	 * Constructor that initializes sort button and second collection
    	 */
    	SortableDataOrganizer(){
    		// instantiate collections
    		collectionTwo = new DataCollection2(SIZE); // Instantiate with size of zero
    		// instantiate sort button
    		sortButton = new Abutton("Sort", Color.red, BUTTONX, BUTTONY);
    	}
    	
    	
    	
    	/**
    	 * Here is where the sorting takes place
    	 * 
    	 */
    	public void sortAction(){
    		//While there is still items in original collection
    		while(collection.getSize()>0){
    			// variables to keep track of selected item
    			Item maximumItem = null;
    			Item someItem= null;
    
    			collection.reset(); // We start at the beginning
    			collectionTwo.reset(); // We start at the beginning
    			//while there is a next
    			while (collection.hasNext()) {
    				// What's next?
    				// update someItem so goes through collection
    				someItem = (Item) (collection.next()); 
    				// set the color of the new collection with selected items
    				someItem.setColor(COLLECTIONTWO_COLOR); 
    				someItem.setY(firstItemYCoord); // Position
    				//Here we compare each item with the my value using Comparable interface
    				//in Item
    				if((maximumItem == null)||(someItem.compareTo(maximumItem))==1)
    					maximumItem = someItem; // we keep track of it
    			}
    			// if there's actually a maximum
    			if (maximumItem != null) {
    				selected=maximumItem;
    			}
    			collection.reset(selected); // make it the selected for removal
    
    			collection.remove(); // remove it
    
    			collectionTwo.add(selected); // add it to the new collection
    		}
    	}
    	
    
    
    	/**
    	 * The "graphical" method of the class is the paint method.
    	 * @param pane
    	 */
    	public void paint(Graphics pane){
    		// still paint everything from before
    		super.paint(pane);
    		// paint sort button
    		if (sortButton != null)
    			sortButton.paint(pane);
    		// painting collectionTwo
    		if (collection != null) {
    			Item element; // an element
    			// x coordinate for painting
    			int xCoord = firstItemXCoord;
    			// start from beginning
    			collectionTwo.reset();
    			// while collectionTwo has a neighbor
    			while (collectionTwo.hasNext()) {
    				// update element to be the next element
    				element = collectionTwo.next();
    				//paint it
    				element.paint(pane, xCoord, firstItemYCoord+400);
    				//update x position
    				xCoord += Item.OVERALL_WIDTH;
    
    			}
    		}
    	}
    	
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Add animation to my selection sort?

    First, please don't comment every statement. It makes your code difficult to read. Only comment situations where you do something that is not
    obvious or that is counter-intuitive.

    You need to decide how you want to animate the sort. For example, if you have bars (like a bar graph) of different heights, do you want to display them
    after then have been swapped or do you want to actually animate them trading places?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Add animation to my selection sort?

    I will remove unnecessary comments, thank you. My sort method sorts low to high, so I would like the animation to show each item being selected and then put into a new colleciton (bar graph).

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Add animation to my selection sort?

    Then you could probably use a Bar class which holds the height of the bar and the location of the bar on a horizontal base. Then as you are sorting, you modify the coordinates of the base and then repaint. Continue to do so using a timer to time repaints and coordinate updates and the two bars appear to swap locations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Selection sort to quick sort conversion without Nodes?
    By VettesRus in forum New To Java
    Replies: 0
    Last Post: 11-03-2013, 02:54 AM
  2. Selection Sort. please help!
    By cassato in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 10:26 PM
  3. Is this a Selection Sort?
    By Metastar in forum New To Java
    Replies: 2
    Last Post: 10-22-2010, 05:00 AM
  4. Problem with selection sort
    By Metastar in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 02:18 AM
  5. selection sort
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •