import java.awt.Color; import java.awt.event.ActionEvent; import javax.swing.JFrame; public class GnomeSortAnimation extends SortAnimation { protected int n = 1; public void actionPerformed(ActionEvent evt) { g.setColor(Color.YELLOW); if (a[n - 1] > a[n]) { int temp = a[n]; a[n] = a[n - 1]; a[n - 1] = temp; if (n > 1) drawIndex(n--); else drawIndex(n++ - 1); } else drawIndex(n++ - 1); g.setColor(Color.RED); try { drawIndices(n - 1, n); } catch (IndexOutOfBoundsException e) { finish(); } } public static void main(String... args) { JFrame frame = new JFrame("Gnome sort animation"); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new GnomeSortAnimation()); frame.setVisible(true); } }