// Copyright 1996, David Neto /* MergeSortApp.java PURPOSE: Demonstrates MergeSort, with comparison counts. AUTHOR: David Neto neto@cs.utoronto.ca Department of Computer Science 10 King's College Road University of Toronto Toronto, Ontario Canada M5S 1A4 VERSION DATE: March 24, 1996. DEVELOPED UNDER: Linux 1.2.13, the 1.0 JDK as ported as of March 1, 1996. BUGS (bugs in Netscape 2.01, really): 1) Disappearing inputChoice. Under Netscape, if the current input choice is not "Custom" and change the inputChoice selection by some means other than direct manipulation (i.e. changing it via the "Shuffle now" button, or by direct manipulation of the values in the text field) then the inputChoice button disappears. It can be restored by making the windowing system repaint the Netscape window, or by clicking once on the area where the inputChoice is hidden. The applet works fine in this regard under the JDK applet viewer. The event is properly being sent to the ActiveChoice button, and the paint() method is properly being called, but nothing is being repainted. Perhaps there is something wrong with the peers implementation within Netscape? WORKAROUND: I am indebted to jabecker@mit.edu who gave me a workaround to this problem. He said that adding a hide() and show() pair fixes the problem. It does. 2) Hardcoded parameters. (Not really a bug...) I have put in support to get runtime parameters from the applet tag. However, Netscape does not always properly pass on all these values. For example, sometimes null is passed in place of the actual value of the second parameter. The JDK applet viewer does not have this problem. To get around this problem with Netscape, I have hardcoded the constants for the n=16 case. It works just as well with suitable parameters for the n=21 case. The downside is that the code needs to be replicated in order to get the n=21 case. I don't think we can get around this code replication via subclassing because of the strict filename-class correspondence. */ import java.awt.*; import java.applet.Applet; public class MergeSortApp extends Applet { GridBagLayout gridBag; GridBagConstraints c; Ticker ticker; int numberParam; // number of elements in the array. String ascendingParam; String descendingParam; String strangeParam; int barWidthParam, barHeightMultipleParam; LoadButton loadButton; RewindButton rewindButton; GoButton goButton; StopButton stopButton; StepButton stepButton; Scrollbar speedScrollbar; SortInput inputs[]; final int numInputs = 5; int currentInputIndex; SortInput currentInput; PreviousSortInput previousInput; CustomSortInput customInput; MergeSortActivation rootActivation = null; ActiveChoice inputChoice; InputRepField inputRepresentation; public void init() { // System.out.println("MergeSortApp init"); doRealConstruction(); } public void start() { // System.out.println("MergeSortApp start"); loadButton.click(); rootActivation.start(); } public void stop() { // System.out.println("MergeSortApp stop"); ticker.killSortNow(); rootActivation.stop(); } public void destroy() { // System.out.println("MergeSortApp destroy"); removeAll(); rootActivation = null; } private void doRealConstruction() { setLayout(gridBag = new GridBagLayout()); c = new GridBagConstraints(); c.weighty = -1; c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.WEST; /* This is the only place where the n=21 and n=16 applets differ. */ numberParam = 16; ascendingParam = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16"; descendingParam = "16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1"; strangeParam = "1 12 6 10 7 2 3 9 14 15 8 11 4 16 13 5"; barWidthParam = 10; barHeightMultipleParam = 3; // Input specification panel. Panel inputPanel = new Panel(); inputPanel.add(new Label("Input:")); inputChoice = new ActiveChoice(); inputs = new SortInput[numInputs]; inputs[0] = new FixedSortInput("Ascending", numberParam, ascendingParam, this, 0); inputs[1] = new FixedSortInput("Descending", numberParam, descendingParam,this,1); inputs[2] = new FixedSortInput("Strange", numberParam, strangeParam, this, 2); inputs[3] = customInput = new CustomSortInput("Custom", numberParam, strangeParam, this, 3); inputs[4] = previousInput = new PreviousSortInput("Previous", numberParam, strangeParam, this, 4); for (int i=0;i 0 ; i-- ) { // Switch i with j j = randint(i+1); t = reps[i]; reps[i]=reps[j]; reps[j]=t; } for ( i=0;i