Need some help with a sort

10-11  Source: Network gathering  Views:0 

Advertisement
I'm trying to make a program that produces a 2-D array and graphically sorts the numbers inside of it. In one column is the random numbers and in the other are the x values for the bars that represent the length of each random number. What I want the program to do is step through the array and find the smallest random number (in column 0) and move it to the appropriate spot in the array sorting from smallest to largest. As the random numbers in column 0 move I also want the x values in column 1 to swap the same way. I have this set up with a timer so that you can see the sort take place one step at a time but there's something wrong with the way I have my sort set up because it's not working properly. I just need some direction on how to get that part of it to work.
Here's the code for the main method:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Sort2
   public static void main (String[] args)
      JFrame frame = new JFrame ("Sort");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new SortPanel2());
      frame.pack();
      frame.setVisible(true);
}
Here's the panel code:
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SortPanel2 extends JPanel
     private final int DELAY = 1000;
    private final int WIDTH = 800, HEIGHT = 400, BARWIDTH = 20;
    public static int y=0;
    public static int index = 0, scan;
    private int h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15;
     private Timer timer;
     public int [][] randomnumbers = new int[15][2];
     public SortPanel2()
           timer = new Timer(DELAY, new SortListener());
           setPreferredSize (new Dimension(WIDTH, HEIGHT));
           setBackground (Color.white);
           timer.start();
          Random gen = new Random();
          for (int x=0; x < 15; x++)
               randomnumbers[x][0] = gen.nextInt(100)+10;
               randomnumbers[x][1] = (50*x) +50;
          h1 = randomnumbers[0][0] * 3;
          h2 = randomnumbers[1][0] * 3;
          h3 = randomnumbers[2][0] * 3;
          h4 = randomnumbers[3][0] * 3;
          h5 = randomnumbers[4][0] * 3;
          h6 = randomnumbers[5][0] * 3;
          h7 = randomnumbers[6][0] * 3;
          h8 = randomnumbers[7][0] * 3;
          h9 = randomnumbers[8][0] * 3;
          h10 = randomnumbers[9][0] * 3;
          h11 = randomnumbers[10][0] * 3;
          h12 = randomnumbers[11][0] * 3;
          h13 = randomnumbers[12][0] * 3;
          h14 = randomnumbers[13][0] * 3;
          h15 = randomnumbers[14][0] * 3;
          for (int y=0;y < 15; y++)
          System.out.println(randomnumbers[y][0]);
        public void paintComponent (Graphics page)
           super.paintComponent (page);
           page.setColor(Color.red);
           page.fillRect(randomnumbers[0][1], 0, BARWIDTH, h1);
           page.setColor(Color.black);
           page.fillRect(randomnumbers[1][1], 0, BARWIDTH, h2);
           page.setColor(Color.blue);
           page.fillRect(randomnumbers[2][1], 0, BARWIDTH, h3);
           page.setColor(Color.orange);
           page.fillRect(randomnumbers[3][1], 0, BARWIDTH, h4);
           page.setColor(Color.yellow);
           page.fillRect(randomnumbers[4][1], 0, BARWIDTH, h5);
           page.setColor(Color.green);
           page.fillRect(randomnumbers[5][1], 0, BARWIDTH, h6);
           page.setColor(Color.cyan);
           page.fillRect(randomnumbers[6][1], 0, BARWIDTH, h7);
           page.setColor(Color.magenta);
           page.fillRect(randomnumbers[7][1], 0, BARWIDTH, h8);
           page.setColor(Color.pink);
           page.fillRect(randomnumbers[8][1], 0, BARWIDTH, h9);
           page.setColor(Color.gray);
           page.fillRect(randomnumbers[9][1], 0, BARWIDTH, h10);
           page.setColor(Color.lightGray);
           page.fillRect(randomnumbers[10][1], 0, BARWIDTH, h11);
           page.setColor(Color.darkGray);
           page.fillRect(randomnumbers[11][1], 0, BARWIDTH, h12);
           page.setColor(Color.red);
           page.fillRect(randomnumbers[12][1], 0, BARWIDTH, h13);
           page.setColor(Color.yellow);
           page.fillRect(randomnumbers[13][1], 0, BARWIDTH, h14);
           page.setColor(Color.blue);
           page.fillRect(randomnumbers[14][1], 0, BARWIDTH, h15);
        private class SortListener implements ActionListener
           public void actionPerformed (ActionEvent event)
         int min;
         int temp1, temp2;
              while (index < randomnumbers.length)
                    min = index;
                    scan = index + 1;
                    System.out.println("Scan="+scan);
                    while (scan < randomnumbers.length)
                     if (randomnumbers[scan][0] < randomnumbers[min][0])
                          min = scan;
                          System.out.println("Minimum: "+randomnumbers[min][0]+" Minimum Address: "+ min);
                              temp1 = randomnumbers[min][0];
                              temp2 = randomnumbers[min][1];
                              randomnumbers[min][0] = randomnumbers[index][0];
                              randomnumbers[min][1] = randomnumbers[index][1];
                              randomnumbers[index][0] = temp1;
                              randomnumbers[index][1] = temp2;
                     scan ++;
                    index++;
                 repaint();
}
Related articles
  • Need some help with a sort 10-11

    I'm trying to make a program that produces a 2-D array and graphically sorts the numbers inside of it. In one column is the random numbers and in the other are the x values for the bars that represent the length of each random number. What I want the

  • Please I need some help with a table 10-11

    Hi All I need some help with a table. My table needs to hold prices that the user can update. Also has a total of the column. my question is if the user adds in a new price how can i pick up the value they have just entered and then add it to the tot

  • Need some help with a Macally enclosure and a spare internal drive 10-11

    Need some help with a Macally enclousure Posted: Oct 1, 2010 10:55 AM Reply Email Aloha: I have a Macally PHR-S100SUA enclousure that does not recognise,my WD 1001fals hard drive. It has worked just fine with other internal drives, but not this one?

  • Need some help with putting a folder in users directory 10-11

    I'm not sure how to do this, but what I want to do is put this file in C:/My Documents, but I need to be able to verify that C://My Documents exists, if not put it in C:/Program Files. Can any one help me out? try {                     String[] conta

  • Need some help with downloading PDF's from the net. 10-11

    need some help with downloading PDF's from the net.  Each time I try to click on a link from a website, if it takes me to a new screen to view a pdf, it just comes up as a blank black screen?  any suggestions?Back up all data. Triple-click the line o

  • Need some help with the Select query. 11-30

    Need some help with the Select query. I had created a Z table with the following fields : ZADS : MANDT VKORG ABGRU. I had written a select query as below : select single vkorg abgru from ZADS into it_rej. IT_REJ is a Work area: DATA : BEGIN OF IT_REJ

  • New to Solaris administration - Need some help with some issues 10-11

    Hello all, I am a new to Solaris administration and need some assistance with a few things. I was going to make separate posts but decided it would be easy to keep track of in one. I really do not know much about the OS but I do have a little Linux b

  • Need some help with a remove function 10-11

    Design and code a program that will maintain a list of product names. Use a String type to represent the product name and an array of strings to implement the list. Your program must implement the following methods: Add a product to the list Remove a

  • Need some help with social net site 10-11

    I need to create a site with editable user profiles, the site will feature sports stats that the user can update. I use Dreamweaver and MYSQL but I need some direction. Im starting from scratch here, are there any templates or free source code to go

  • I need some help with AVI and IDX files please. 10-11

    Hello there, I've just been given a hard drive with a load of media on it that I need to work with in FCP. There's a folder with many .AVI clips, and each clip has an accompanying directory file with the extension .IDX The format of the AVI files is

  • Need some help with the colour profile please. Urgent! Thanks 10-11

    Dear all, I need help with the colour profile of my photoshop CS6.  I've taken a photo with my Canon DSLR. When I opened the raw with ACDSee, the colour looks perfectly ok. So I go ahead and open in photoshop. I did nothing to the photo. It still loo

  • Need some help with a program Please. 10-11

    Well, im new here, im new to java, so i need your help, i have to make a connect four program, my brother "Kind of" helped me and did a program for me, but the problem is i cant use some of those commands in the program and i have to replace the

  • Hello, im new to mac and I need some help with a java problem. 10-11

    Hello, im new to mac. I just need someone who can help with a problem ive come across when playing online games that run java. The game is arcanists. its on the funorb website. really fun game and i love it, but i cant play it without my screen keep

  • Need some help with Apache vhost. Access a server on network. 10-11

    Not sure if this can be done. But here is my case: Me and my colleague is writing some database applications for out employer. And this server runs Apache, PHP and MySQL. We want to be able to access different vhosts using just the post after the ser

  • Snapper needs some help with photo workflow. 03-11

    I guys I am a photographer who has just moved over from a PC to a iMac and i am having a small problem with my workflow and hoping that maybe someone can help me out. When using my PC after covering one of my events I would plug my memory card into a

  • Hello I have a few questions and I need some help with my iTunes and  iPod 10-11

    Hello iTunes people. I have a little problem with my iTunes. I actually almost know nothing about iTunes really. But I have had a 80 gig ipod .. ummm I think it's a iPod classic maybe. A long time ago I just downloaded iTunes. I think one of my tech

  • Need some help with backing up external hard drive using Time Machine 10-11

    Hi all, I'm trying to work out the best way to back up multiple iPhoto libraries using Time Machine. At the moment I have got an iMac and Macbook Pro, both of which have an iPhoto library (with different content).  Currently these are not backed up a

  • Really Need Some Help with CME 8.6 using IOS as Firewall and Anyconnect VPN on Phones 10-11

    Hello, I have a 2911 Router with IOS Security and Voice enabled and we are using CME 8.6.  I am using a built-in Anyconnect VPN on 3 phones that are for remote users and thus I needed to enable security zones on the router which works because the rem

  • I need some help with my video - need to blur some footage. 10-11

    Hi! I was wondering if somebody could help me out with my video. I've got some video footage downloaded from the internet (with permission, of course) and I was wondering if there is any way I could blur the clip and then have it slowly fade into foc

  • Alright I give! I need some help with groups/busses and more please.. 10-11

    Hi all, Well, its now been a few weeks of my off and on testing with my new Logic Rig, I have every plug I can transfer over to UB that I can and now am trying to get deeper into things. First I struggled with learning the environment (I still dont g