/** * Josephus -- * A class to graphically depict a Josephus selection * * @author rpj * @version 28ii08 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Josephus extends Applet { // defaults private static final int SIZE = 8; // how many people private static final int NTH = 3; // eliminate every Nth person // instance variables int size = SIZE; int nth = NTH; TextField setSize = new TextField("Enter number of soldiers here"); TextField setNth = new TextField("I will kill every nth soldier. Enter n here"); TextField person; CircularList people; /** button to control the applet: */ Button pickSurvivorBtn = new Button("pickSurvivor"); public void init() { people = new CircularList(); setLayout(new FlowLayout()); add(pickSurvivorBtn); setSize.setEditable(true); setNth.setEditable(true); add(setSize); add(setNth); pickSurvivorBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent buttonPress) { try { size = new Integer(setSize.getText()); nth = new Integer(setNth.getText()); } catch (NumberFormatException nfe) { // revert to defaults } for (int i=0; i(); pickSurvivorBtn.setEnabled(true); } public void pause(int time) { try { Thread.sleep(time); } catch(InterruptedException ignored) {} } }