Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with Selection Sort

    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();

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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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
    I am the luckiest man in the world

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, I found the error, I didn't even need the timer there, now it works perfectly.


  •  

    Posting Permissions

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