• 0

[JAVA] Sorting Numbers


Question

I am writing a program to sort numbers using selection sort from a file and draw them on the screen as bars like this:

2q00oli.png

The program uses a timer to sort once every so many seconds and redraws the bars on screen in order so the user can see the sorting. I am having problem displaying the bars my code is just creating a blank panel and there are no errors when I compile:

Driver:

import java.awt.*;
import javax.swing.*;
import java.io.*;

public class SortDriver
{
	public static void main(String[] args) throws IOException
	{
		JFrame frame = new JFrame ("Sort Animation");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

		frame.getContentPane().add(new SortPanel());

		frame.pack();
		frame.setVisible(true);
	}
}

Main Panel:

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

public class SortPanel extends JPanel
{
	public SortPanel() throws IOException
	{
		int n, num;

		n = 0;

		JFileChooser chooser = new JFileChooser();

		int status = chooser.showOpenDialog(null);

		File file = chooser.getSelectedFile();
		Scanner scan = new Scanner(file);

		//----------------------------
		// Loop to find array length.
		//----------------------------

		while (scan.hasNext())
		{
			num = scan.nextInt();
			n++;			
		}

		scan.close();		
		scan = new Scanner (file);

		int[] list = new int[n];	

		//----------------------------------------------
		// Loop to assign numbers from the text file to
		// the array.
		//----------------------------------------------
		for (int index = 0; index < list.length; index++)
		{
			if (scan.hasNext())
			{
				num = scan.nextInt();

				list[index] = num;			
			}
		}

		SortNum sort = new SortNum(list);

		//prints array values
		for(int value : list)
			System.out.println("" + value);

		setBackground (Color.white);
		setPreferredSize (new Dimension(50, 100));
	}
}

Sorting Code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SortNum extends JPanel
{
	private Timer timer;
	private final int DELAY = 2000;
	private int[] list; // array from file
	private int index = 0;
	private boolean finished = false;

	//--------------------------------------------
	// Method which conducts a single pass of the 
	// sort algorithm.
	//--------------------------------------------
	private void nextPass()
	{
		int min = index;
		for (int scan = index + 1; scan < list.length; scan++)
		{
			if (list[scan] < list[min])
			{
				min = scan;
			}
		}
		if (index != min) 
		{
			int swap = list[index];
			list[index] = list[min];
			list[min] = swap;
		}
		index++;
	}

	//--------------------------------------
	// Used to test if the sort is complete.
	//--------------------------------------
	private boolean finished()
	{
		if (index == (list.length - 1))
		{
			finished = true;
		}
		else
		{
			finished = false;
		}

		return finished;
	}

	//-----------------------------------------------
	// Sets up the sort method and starts the timer.
	//-----------------------------------------------
	public SortNum (int[] listNotSorted)
	{
		timer = new Timer(DELAY, new SortListener());

		list = listNotSorted;

		timer.start();
	}

	//-------------------------------------------------------
	// Listener that fires when the timer reaches its delay.
	//-------------------------------------------------------
	private class SortListener implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			if (finished == false)
			{
				nextPass();
				repaint();
			}
			else
			{
				timer.stop();	
			}			
		}
	}

	//-------------------------------
	// Draws the bars on the screen.
	//-------------------------------
	public void paintComponent (Graphics page)
	{
		super.paintComponent(page);
		int x = 20;
		int y = 100;
		for(int index = 0; index < list.length; index++)
		{
			page.fillRect(x, y, 10, -list[index]);
			x = x + 40;
		}
	}
}

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.