/* PrimeTriangleApplet.java Generate various forms of Prime Triangles, using a GUI to monitor progress and control. Rich Acuff */ import java.awt.*; import java.applet.*; public class PrimeTriangleApplet extends Applet { PrimeTriangleThread ptt; boolean stats = false; boolean running = false; boolean output = true; boolean verify = false; void verifyCheckbox_Action(Event event) { verify = verifyCheckbox.getState(); } void noOutputCheckbox_Action(Event event) { output = !noOutputCheckbox.getState(); } void statsCheckbox_Action(Event event) { stats = statsCheckbox.getState(); } void intervalField_KeyRelease(Event event) { if (running) {ptt.updateInterval();} } void keepGoingButton_Clicked(Event event) { if (running) { ptt.keepGoing = true; ptt.resume(); } } void stopButton_Clicked(Event event) { ptt.goAway(); ptt = null; goButton.show(); } void goOnButton_Clicked(Event event) { if (running) {ptt.resume();} } synchronized void goButton_Clicked(Event event) { if (!running) { goButton.hide(); ptt = new PrimeTriangleThread (this); //ptt.setDaemon(true); //seems to break Macs and not work right on PCs (Netscape) running = true; ptt.start(); } } public void init() { super.init(); //{{INIT_CONTROLS setLayout(null); addNotify(); resize(580,688); setBackground(new Color(12632256)); out = new java.awt.TextArea(); out.reshape(4,253,574,427); add(out); instructions = new java.awt.Label("Enter the number of rows you want and press GO!"); instructions.reshape(7,5,451,30); instructions.setFont(new Font("Helvetica", Font.BOLD, 18)); add(instructions); rowsLabel = new java.awt.Label("Up to what row?"); rowsLabel.reshape(51,39,130,24); rowsLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowsLabel); rowField = new java.awt.TextField(); rowField.reshape(183,37,106,26); rowField.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowField); goButton = new java.awt.Button("GO!"); goButton.reshape(304,35,48,30); goButton.setFont(new Font("Dialog", Font.BOLD, 16)); add(goButton); goOnButton = new java.awt.Button("Next Row"); goOnButton.hide(); goOnButton.reshape(428,67,105,30); goOnButton.setFont(new Font("Dialog", Font.BOLD, 16)); add(goOnButton); stopButton = new java.awt.Button("Stop"); stopButton.hide(); stopButton.reshape(302,35,105,30); stopButton.setFont(new Font("Dialog", Font.BOLD, 16)); stopButton.setBackground(new Color(16711680)); add(stopButton); keepGoingButton = new java.awt.Button("Keep Going"); keepGoingButton.hide(); keepGoingButton.reshape(427,35,105,30); keepGoingButton.setFont(new Font("Dialog", Font.BOLD, 16)); add(keepGoingButton); intervalField = new java.awt.TextField(); intervalField.setText("10"); intervalField.reshape(183,63,106,26); intervalField.setFont(new Font("Dialog", Font.BOLD, 14)); add(intervalField); intervalLabel = new java.awt.Label("Display update interval:"); intervalLabel.reshape(2,65,179,24); intervalLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(intervalLabel); label1 = new java.awt.Label("(1/10 sec)"); label1.reshape(292,65,130,24); label1.setFont(new Font("Dialog", Font.BOLD, 14)); add(label1); isPrimeLabel = new java.awt.Label("# calls to isPrime:",Label.RIGHT); isPrimeLabel.reshape(0,123,157,24); isPrimeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(isPrimeLabel); rearrangeLabel = new java.awt.Label("# calls to rearrange:",Label.RIGHT); rearrangeLabel.reshape(-3,149,160,24); rearrangeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rearrangeLabel); swapLabel = new java.awt.Label("# calls to swap:",Label.RIGHT); swapLabel.reshape(2,175,155,24); swapLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(swapLabel); totalIsPrimeLabel = new java.awt.Label("",Label.RIGHT); totalIsPrimeLabel.reshape(265,126,105,19); totalIsPrimeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalIsPrimeLabel); totalRearrangeLabel = new java.awt.Label("",Label.RIGHT); totalRearrangeLabel.reshape(265,152,105,19); totalRearrangeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalRearrangeLabel); totalSwapLabel = new java.awt.Label("",Label.RIGHT); totalSwapLabel.reshape(265,178,105,19); totalSwapLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalSwapLabel); totalTimeLabel = new java.awt.Label("",Label.RIGHT); totalTimeLabel.reshape(265,230,105,19); totalTimeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalTimeLabel); totalLabel = new java.awt.Label("",Label.RIGHT); totalLabel.reshape(265,204,105,19); totalLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalLabel); rowTimeLabel = new java.awt.Label("",Label.RIGHT); rowTimeLabel.reshape(159,229,105,19); rowTimeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowTimeLabel); timeLabel = new java.awt.Label(" Run Time:",Label.RIGHT); timeLabel.reshape(62,227,94,24); timeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(timeLabel); rowIsPrimeLabel = new java.awt.Label("",Label.RIGHT); rowIsPrimeLabel.reshape(159,125,105,19); rowIsPrimeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowIsPrimeLabel); rowRearrangeLabel = new java.awt.Label("",Label.RIGHT); rowRearrangeLabel.reshape(159,151,105,19); rowRearrangeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowRearrangeLabel); rowSwapLabel = new java.awt.Label("",Label.RIGHT); rowSwapLabel.reshape(159,177,105,19); rowSwapLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowSwapLabel); freeLabel = new java.awt.Label("",Label.RIGHT); freeLabel.reshape(159,203,105,19); freeLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(freeLabel); memLabel = new java.awt.Label("Memory (free/total)",Label.RIGHT); memLabel.reshape(6,201,153,24); memLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(memLabel); thisRowLabel = new java.awt.Label(""); thisRowLabel.reshape(204,106,72,19); thisRowLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(thisRowLabel); rowLabel = new java.awt.Label("Row ",Label.RIGHT); rowLabel.reshape(154,107,50,18); rowLabel.setFont(new Font("Dialog", Font.BOLD, 14)); add(rowLabel); modeGroup = new CheckboxGroup(); oneButton = new java.awt.Checkbox("Only One Row", modeGroup, false); oneButton.reshape(393,221,175,22); oneButton.setFont(new Font("Dialog", Font.BOLD, 14)); add(oneButton); stepButton = new java.awt.Checkbox("One Row at a Time", modeGroup, true); stepButton.reshape(393,173,175,22); stepButton.setFont(new Font("Dialog", Font.BOLD, 14)); add(stepButton); completeButton = new java.awt.Checkbox("Complete Triangle", modeGroup, false); completeButton.reshape(393,197,175,22); completeButton.setFont(new Font("Dialog", Font.BOLD, 14)); add(completeButton); totalTag = new java.awt.Label("Total",Label.CENTER); totalTag.reshape(283,104,92,21); totalTag.setFont(new Font("Dialog", Font.BOLD, 14)); add(totalTag); statsCheckbox = new java.awt.Checkbox("Stats instead of rows"); statsCheckbox.reshape(391,148,183,24); statsCheckbox.setFont(new Font("Dialog", Font.BOLD, 14)); add(statsCheckbox); noOutputCheckbox = new java.awt.Checkbox("No output"); noOutputCheckbox.reshape(391,123,183,25); noOutputCheckbox.setFont(new Font("Dialog", Font.BOLD, 14)); add(noOutputCheckbox); verifyCheckbox = new java.awt.Checkbox("Verify"); verifyCheckbox.reshape(391,98,183,25); verifyCheckbox.setFont(new Font("Dialog", Font.BOLD, 14)); add(verifyCheckbox); //}} PrimeTriangle.preComputePrimes(PrimeTriangle.pCacheSize); } public boolean handleEvent(Event event) { if (event.target == goButton && event.id == Event.ACTION_EVENT) { goButton_Clicked(event); return true; } if (event.target == goOnButton && event.id == Event.ACTION_EVENT) { goOnButton_Clicked(event); return true; } if (event.target == stopButton && event.id == Event.ACTION_EVENT) { stopButton_Clicked(event); return true; } if (event.target == keepGoingButton && event.id == Event.ACTION_EVENT) { keepGoingButton_Clicked(event); return true; } if (event.target == intervalField && event.id == Event.KEY_RELEASE) { intervalField_KeyRelease(event); return true; } if (event.target == statsCheckbox && event.id == Event.ACTION_EVENT) { statsCheckbox_Action(event); return true; } if (event.target == noOutputCheckbox && event.id == Event.ACTION_EVENT) { noOutputCheckbox_Action(event); return true; } if (event.target == verifyCheckbox && event.id == Event.ACTION_EVENT) { verifyCheckbox_Action(event); return true; } return super.handleEvent(event); } //This isn't really a good place for these methods, which should be more generic... void printRagged(int array[][]) { if (!output) {return;} String pad = new String(), sample = "" + array.length; int width = sample.length(); for (int i = 0; i < width; i++) pad += " "; for (int j = 0; j < array.length; j++) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < ((array.length - 1) - j); i++) { buffer.append(pad); } for (int i = 0; i < array[j].length; i++, buffer.append(pad)) { String padded = pad + array[j][i]; buffer.append(padded.substring(padded.length() - width)); } buffer.append('\n'); out.appendText(buffer.toString()); } } void printRow (int[] array) { if (!output) {return;} out.appendText(rowToString(array)); } static String rowToString (int[] array) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length; i++, buffer.append(" ")) { buffer.append(Integer.toString(array[i])); } buffer.append('\n'); return buffer.toString(); } void statsHeader () { if (!output) {return;} out.appendText ("Row\tisPrime\tRearrange\tSwap\tStack\tMax Stack\tTime\tTotal Time\n"); } void printStats (Monitor[] m) { if (!output) {return;} out.appendText (m[0] + "\t" + m[1] + "\t" + m[2] + "\t" + m[3] + "\t" + m[4] + "\t" + m[5] + "\t" + m[6] + "\t" + m[7] + "\n"); } //{{DECLARE_CONTROLS java.awt.TextArea out; java.awt.Label instructions; java.awt.Label rowsLabel; java.awt.TextField rowField; java.awt.Button goButton; java.awt.Button goOnButton; java.awt.Button stopButton; java.awt.Button keepGoingButton; java.awt.TextField intervalField; java.awt.Label intervalLabel; java.awt.Label label1; java.awt.Label isPrimeLabel; java.awt.Label rearrangeLabel; java.awt.Label swapLabel; java.awt.Label totalIsPrimeLabel; java.awt.Label totalRearrangeLabel; java.awt.Label totalSwapLabel; java.awt.Label totalTimeLabel; java.awt.Label totalLabel; java.awt.Label rowTimeLabel; java.awt.Label timeLabel; java.awt.Label rowIsPrimeLabel; java.awt.Label rowRearrangeLabel; java.awt.Label rowSwapLabel; java.awt.Label freeLabel; java.awt.Label memLabel; java.awt.Label thisRowLabel; java.awt.Label rowLabel; java.awt.Checkbox oneButton; CheckboxGroup modeGroup; java.awt.Checkbox stepButton; java.awt.Checkbox completeButton; java.awt.Label totalTag; java.awt.Checkbox statsCheckbox; java.awt.Checkbox noOutputCheckbox; java.awt.Checkbox verifyCheckbox; //}} }