...

View Full Version : Need help with Selection Sort



demmos
01-08-2008, 09:48 PM
Hello, I'm new to the forum, although I've been reading it some time, it has helped me alot. Thank you.
My problem is with a swing java program. I'm trying to make an animation applet for the Selection Sort algorithm.
I have problem with the timer or the algorithm. It only completes the first step of sorting then nothing happens. If I put timer.stop() after else { in the ActionPerformed it says that cannot recognize this variable, I don't know how to fix this either. Please help, I am a begginer in Java :) Thank you.
Here's the code, I have underlined the timer.stop() problem:

import java.awt.*;
import java.util.Arrays.*;
import java.util.Collections.*;

public class Sort extends javax.swing.JFrame {
public int i=0;
public int ssc=-1;

public Sort() {
initComponents();
}

// Generated code is here.

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sort().setVisible(true);
Sort f = new Sort();
final Panel p = new Panel();
p.setSize(400,400);
javax.swing.JButton jButton2 = new javax.swing.JButton();
p.add(jButton2);
jButton2.setText("Start");
f.add(p);
f.setVisible(true);
final javax.swing.Timer timer = new javax.swing.Timer(500, p);

jButton2.addActionListener(new Panel() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
timer.start();
}
});

}
});
}

// Variables declaration - do not modify
// End of variables declaration
Panel p;
}

class Panel extends javax.swing.JPanel implements java.awt.event.ActionListener {
public int i=0;
int[] x = { 60, 80, 50, 30, 110, 100, 5};

public void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
g.setColor(java.awt.Color.WHITE);
g.fillRect(1,1,399,399);
g.setColor(java.awt.Color.RED);
g.fillRect(20,61,20,x[0]);
g.fillRect(45,61,20,x[1]);
g.fillRect(70,61,20,x[2]);
g.fillRect(95,61,20,x[3]);
g.fillRect(120,61,20,x[4]);
g.fillRect(145,61,20,x[5]);
g.fillRect(170,61,20,x[6]);

g.setColor(java.awt.Color.BLACK);
g.draw3DRect(10,60,200,181,true);
g.draw3DRect(230,60,150,181,true);
g.drawString("This is a short animation",235,95);
g.drawString("of the Selection Sort",255,115);
g.drawString("algorithm of",253,135);
g.drawString("max of 7 elements.",245,155);
g.drawString("SELECTION SORT",150,49);
g.drawString("Andrej",110,265);
g.drawString("Copyright 2008",150,285);

}

public void actionPerformed(ActionEvent e) {
int ssc=-1;
if (ssc !=x.length-1) {

ssc++;
int minIndex = ssc;

for (int j=ssc+1; j<x.length; j++) {
if (x[minIndex] > x[j]) {
minIndex = j;
}
}

if (minIndex != ssc) {
int temp = x[ssc];
x[ssc] = x[minIndex];
x[minIndex] = temp;
repaint();
}


} else {
timer.stop();
ssc=-1;

}

repaint();

}
}
//--------------------------------------------------------------

Roelf
01-09-2008, 08:56 AM
the variable timer is defined and thus only known in the main() method. Define you timer as a member a level higher, in the class. That way it is known in all methods of the class

demmos
01-09-2008, 01:09 PM
Thank you, I found the error, I didn't even need the timer there, now it works perfectly. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum