CIT 594 Help on assignment 8 (Backtracking)
Spring 2011, David Matuszek

You should never do a significant amount of work in a Listener; for example, solve a complete backtracking problem. The Listener is in the GUI thread, and it keeps that thread from doing anything else (like, updating the display) while it is running its method.

If you have made this mistake, I've tried to find a way to modify my supplied ColorBar class to work around it. I have not found a way. You'll just have to fix your code. (If your program works, you can ignore the rest of this page.)

Here's what you need to do. Your Listener (probably on a JButton) that runs the backtracking algorithm, should instead start a new Thread that runs the backtracking algorithm. The the Listener can quit and let the new Thread do its job.


ColorBar class: Not changed.

ColorBarExample class:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Program to provide an example of using the ColorBar class.
 * 
 * @author Dave Matuszek
 */
public class ColorBarExample extends JFrame {
    ColorBar[] bars;
    Color[] colors1 = { Color.RED, Color.ORANGE, Color.YELLOW,
                        Color.GREEN, Color.BLUE };
    Color[] colors2 = { Color.RED, Color.ORANGE, Color.YELLOW,
                        Color.GREEN, Color.BLUE };
    JButton runButton = new JButton("Run");

    /**
     * Displays a color bar, then changes it several times.
     * 
     * @param args Not used.
     */
    public static void main(String[] args) {
        new ColorBarExample().run();
    }

    /**
     * Displays a color bar, then changes it several times.
     */
    private void run() {
        bars = new ColorBar[2];
        bars[0] = new ColorBar(colors1);
        bars[1] = new ColorBar(colors2);
        setLayout(new GridLayout(3, 1));
        add(bars[0]);
        add(bars[1]);
        add(runButton);
    
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(getParent());
        pack();
        setVisible(true);
    
        runButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Backtracking bt = new Backtracking(bars);
                bt.start();
            }
    
        });
    }
}

New Backtracking class (extending Thread):

import java.util.Random;
    
public class Backtracking extends Thread {
    ColorBar[] bars;
    static Random random = new Random();
    
    public Backtracking(ColorBar[] bars) {
        this.bars = bars;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            shuffle(bars[0].colors);
            shuffle(bars[1].colors);
            bars[0].resetColors();
            bars[1].resetColors();
        }
    }

    /**
     * Shuffles (randomizes) the Objects in the given array.
     * 
     * @param array The array of objects to be shuffled.
     */
    void shuffle(Object[] array) {
        for (int i = array.length; i > 0; i--)  {
            int j = random.nextInt(i);
            Object temp = array[i - 1];
            array[i - 1] = array[j];
            array[j] = temp;
        }
    }
}