Results 1 to 2 of 2
  1. #1
    kkhat is offline Member
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Question Two-dimensional animations of Bubble Sort

    Howdy,

    I'm extremely new with graphics in java, but I'm trying to create a program in java that uses Xming to draw out an animation of bubble sort. My original design involved creating multiple rectangles that would display each number from a randomly generated list. Then pairs of rectangles would be "highlighted" in order to represent each number being compared and then switching (according to the bubble sort algorithm). Unfortunately I have no idea how to assign a shape a number and for it to be displayed in it. On top of that, im not even sure how im going to make the rectangles switch according to the algorithm for the number in the shape. Does anyone have any ideas on how I could approach this differently or maybe just suggest anything that may make this a little more clear?

    Here is my current code: Please excuse the sloppy architecture, I'm fairly new to the language as well.

    import java.util.*;
    import javax.swing.*;
    import java.lang.*;
    import java.awt.*;

    public class animate_sort {

    static int num;
    static int section_width = 290;

    public static Vector<Integer> RandomlyGenerate(int y) {
    Vector<Integer> temp = new Vector<Integer>(y);
    int upper = y;
    Random rGenerator = new Random();
    for (int idx = 1; idx <= upper; ++idx){
    int randomNumber = rGenerator.nextInt(100);
    temp.add(randomNumber);
    }
    return temp;
    }

    public static void main(String[] args) {
    System.out.println("How many numbers would you like to sort?");
    Scanner s = new Scanner(System.in);

    num = s.nextInt();
    Vector<Integer> vec = RandomlyGenerate(num);

    animate(vec);

    }

    public static void swap(Vector<Integer> vec, int i1, int i2) {
    int temp1 = vec.elementAt(i1);
    int temp2 = vec.elementAt(i2);
    vec.set(i1, temp2);
    vec.set(i2, temp1);
    }


    public static class DrawPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.drawLine(330, 0, 330, 500);
    g.drawLine(660, 0, 660, 500);
    g.drawLine(0, 250, 990, 250);
    g.drawString("Buble Sort", 130,225);


    int x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(x, 100, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(x, 100, 40, 40);
    x += 40;
    }
    }

    x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(330+x, 100, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(330+x, 100, 40, 40);
    x += 40;
    }
    }

    x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(660+x, 100, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(660+x, 100, 40, 40);
    x += 40;
    }
    }

    x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(x, 350, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(x, 350, 40, 40);
    x += 40;
    }
    }

    x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(330+x, 350, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(330+x, 350, 40, 40);
    x += 40;
    }
    }

    x = 20;
    if (num > 5) {
    for (int m = 0; m<num; m++) {
    g.drawRect(660+x, 350, section_width / num , section_width / num );
    x += (section_width / num);
    }
    } else {
    for (int m = 0; m<num; m++) {
    g.drawRect(660+x, 350, 40, 40);
    x += 40;
    }
    }

    }
    }


    public static void animate(Vector<Integer> vec) {
    JFrame frame = new JFrame();
    frame.pack();
    frame.setTitle("Sorting Algirithm Animations");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    frame. setSize(990, 500);
    frame.setLocationRelativeTo(null);

    DrawPanel panel = new DrawPanel();
    frame.add(panel);

    frame.setVisible(true);

    // multithreading? do all the animate_sorts simultaneously

    }

    /*
    public void animate_bubble(Vector<int> vec) { //top left


    }

    public void animate_insertion(Vector<int> vec) { //top middle


    }

    public void animate_shell(Vector<int> vec) { //top right


    }

    public void animate_merge(Vector<int> vec) { //bottom left


    }

    public void animate_quick(Vector<int> vec) { //bottom middle


    }

    public void animate_heap(Vector<int> vec) { //bottom right


    }
    */

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default Re: Two-dimensional animations of Bubble Sort

    Please go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    Also, it's never needed to import the classes in the java.lang package. The compiler does that for you.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Please help me to initialise my bubble sort
    By riddy in forum New To Java
    Replies: 4
    Last Post: 03-20-2013, 11:09 PM
  2. Question with bubble sort
    By Metastar in forum New To Java
    Replies: 22
    Last Post: 09-13-2010, 06:25 AM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Posting Permissions

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