1

i've been struggling with the delay timer. how can i make the swapping visible to observer? and by putting color on selected bar while the swapping is processing? This is a selection sort by the way.

selectionSort2.java

 /**
 *
 * @author Yuvinng
 */

 import java.awt.*; 
 import javax.swing.*;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.*;
 public class SelectionSortPanel2  extends JPanel{

protected JButton selection;
private final int width=400,height=400;
private static Random generator;
private int[] list=new int[100];
private Timer timer;


public void selectionSort(int[] list)
{
int min;
int temp;
for(int index=0; index<list.length-1;index++)
{
min=index;
for(int scan=index+1;scan<list.length;scan++)
 if(list[scan]<(list[min]))
     min=scan;
temp=list[min];
list[min]=list[index];
 list[index]=temp;
repaint();
}    
}
private class swapper implements ActionListener 
   {
      public void actionPerformed(ActionEvent event)
      {   
         selectionSort(list);
      }
   }
 }
V_Stack
  • 147
  • 2
  • 7
  • 15
  • 2
    I would start by trying to format your code to make it easier to read. Your IDE should be able to do that for you. – Peter Lawrey Dec 09 '11 at 08:24
  • Start with formatting the code to make it easier to read, then look at what you do with your timer. You have a delay of 100 ms, but after that you sort the whole array and then repaint, and I don't think that is your goal. Don't you want to delay after each swap so you can see the sorting progress? – Roger Lindsjö Dec 09 '11 at 09:15
  • @RogerLindsjö yes that was my goal. but i have no idea how to create a loop for the timer to execute the swapping each time so that the swapping is visible for user and additiion of colors for the bar that is being swapped so it make the swapping more obvious. – V_Stack Dec 09 '11 at 09:18
  • As hardly anybody solves homework problems here (how would you learn then) I'll post some hints as an answer. – Roger Lindsjö Dec 09 '11 at 09:24
  • hint will be good enough. i want to learn. but i tried various way and it just wouldnt work – V_Stack Dec 09 '11 at 09:26
  • while you are at learning: please learn java naming conventions and stick to them :-) – kleopatra Dec 09 '11 at 09:58

2 Answers2

2

javax.swing.Timer is a good choice, as it hides the thread used to wait and fires when it's time to draw. Just draw the result of one swap in the timer's actionPerformed() method. You'll have to re-organize selectionSort() so that it can run one step at a time. There's a related Timer example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Instead of using a Timer which sorts your whole array every time it is invoked and without rewriting your sort method to only sort one element on each invocation you can do this.

When you use the Timer it actually execute in the same thread as repainting the guy, so if you do any sleep here the guy will not repaint.

Instead, change your swapper to be a Runnable, and create a new Thread with your swapper and start the thread.

Then, after swapping and calling repaint you can tell the swapping thread to sleep for a while Thread.sleep(delay).

JPanel does not clear itself between repaints, so you will only see columns grow, never get shorter. Either fix the paintComponent to clear the graphics or extend JComponent instead.

Hope that helps your progress.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • is that the only approach? Runnable is still out of my capability as my lecturer havent thought about the whole thrread – V_Stack Dec 09 '11 at 09:40