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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Animating Graphical Sort

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Animating Graphical Sort

    I am working on an assignment called "Graphical Sort", which is basically animating sort algorithm graphically.

    I just need help on animating the sorting process.

    I tried using Thread, but the program hangs till the threading process is completed, then it shows the final result.

    Below are the picture of how my program looks like:
    SkXi8.jpg
    gdD38.jpg

    Below is the JPanel class I made, which I paint on:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
     
    public class PaintPanel extends JPanel
    {
        // Create an array of 34 element size
        int[] Arr = new int [34];
        // Set default X pointer to 20
        int x = 50;
        // Declare Y pointer to 660
        int y = 660;
        // Set the length of array to n variable
        int n = Arr.length;
     
     
        /*
         * main method
         * @param none
         * @return none
         */
        public PaintPanel ()
        {
            randomNums ();
            revalidate ();
            repaint (0, 150, 800, 700);
        }
     
     
        /*
         * Generates random numbers between 50 and 750 and stores it into the Arr variable
         * @param none
         * @return none
         */
        public void randomNums ()
        {
            // call randomGenerator object
            Random randomGenerator = new Random ();
            // Loop 33 times = Generates 33 random integers
            for (int i = 0 ; i <= 33 ; ++i)
            {
                // Generate random Number
                int randomInt = randomGenerator.nextInt (700);
                // Conditional statement, if any number is less than 50, then discard it and generate new number
                if (randomInt > 50)
                    // Assign each random number into Arr Element
                    Arr [i] = randomInt;
                else
                {
                    // Regenerate Random Number
                    randomInt = randomGenerator.nextInt (700);
                    // Assign it again
                    Arr [i] = randomInt;
                }
            }
        }
     
     
        /*
         * Bubble Sort Algorithm
         * @param none
         * @return none
         */
        public void bubble ()
        { //Pre: a is an array with values. It is of size n
            //Post: the values in a are put in ascending order
            int temp;
            //int a[] = Arr;
            for (int i = 0 ; i < n - 1 ; i++)
            {
                for (int j = 0 ; j < n - 1 - i ; j++)
                { // compare the two neighbours
                    if (Arr [j + 1] < Arr [j])
                    { //swap the neighbours if necessary
                        temp = Arr [j];
                        Arr [j] = Arr [j + 1];
                        Arr [j + 1] = temp;
                        repaint (0, 150, 800, 700);
                    }
                }
            }
        }
     
     
        /*
         * Paints 33 rectangle Strips to the screen
         * @param Graphics g
         * @return none
         */
        public void paintComponent (Graphics g)
        {
            super.paintComponent (g);
            // Call Graphics2D Object
            Graphics2D g2 = (Graphics2D) g.create ();
            // Create Paint Object with gradient Fill
            Paint p = new GradientPaint (
                    0, 0, new Color (0x44A2FF),
                    getWidth (), 0, new Color (0x0CBEAE),
                    true
                    );
            // Set the gradient fill to the Graphics2D Object
            g2.setPaint (p);
     
            // Loop through the Array and display series of Rectangular Strips
            for (int i = 0 ; i < Arr.length ; ++i)
            {
                // Fill out the Rectangle
                g2.fillRect (x, y, Arr [i], 8);
                y = y - 15;
            }
            g2.dispose ();
        }
    }

    What should I use to animate the process. I also want to show which rectangular strips are being compared during the process of sorting.

    Thank You


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Animating Graphical Sort

    Normally with animation the program makes a change, calls repaint() and the painting method is called to display that change. Then another change is made, repaint() is called etc

    How is the posted code executed for testing? There is not a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Animating 2d graphics
    By Brownb92 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 5th, 2012, 01:39 PM
  2. Graphical Issue
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2012, 04:30 PM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. Slot machine, animating the reels to spin
    By LukeDavison in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2011, 10:59 AM
  5. Help!how to get a graphical display of this game
    By makarov in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2009, 11:15 AM

Tags for this Thread