// Hello.java // // Becky's first program. // Created March 30, 2002, by Becky Wagner with asssistance from Rick Wagner. // Last updated October 28, 2012 by Rick Wagner. // Version history: // Version 1.03, updated March 31, 2002. // Version 1.04, October 28, 2012, adds a few more comments. // Version 1.05, November 8, 2012, replaces deprecated action events with ActionListener. // Copyright 2002-2012, all rights reserved. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Hello extends Applet { private Dimension dApplet = null; // The applet panel size (read from the html file) private Label lblMessage = null; private TextArea taOutput = null; private int iNumDates = 20; // The number of dates for sorting private int iArray[] = new int[iNumDates]; // Array of integers private Button btnScrambleArray = null; private Button btnSortArray = null; private ActionListener al = new alButtonEvents(); public void init() { // Set the background color: setBackground(Color.lightGray); // Get the applet dimensions: dApplet = getSize(); // Add the message label: lblMessage = new Label("Hello. Welcome to Becky's World!"); lblMessage.setAlignment(Label.CENTER); add(lblMessage); // Add the text area: taOutput = new TextArea(20, 40); taOutput.setEditable(true); add(taOutput); // Add the scramble button: btnScrambleArray = new Button("Scramble Dates"); add(btnScrambleArray); btnScrambleArray.setActionCommand("Scramble Dates"); // Defaults to the button name. btnScrambleArray.addActionListener(al); // Add the sort button: btnSortArray = new Button("Sort Dates"); add(btnSortArray); btnSortArray.setActionCommand("Sort Dates"); // Defaults to the button name. btnSortArray.addActionListener(al); } // End of init() public void start() { int i = 0; int iRand = 0; for (i = 0; i < iNumDates; i++) // Set random dates over a 200 year range. { iRand = 1800 + (int) (Math.random() * 200); iArray[i] = iRand; } } // End of start() // Code for displaying images or drawing in the applet frame public void paint(Graphics g) { // Draw a recessed frame around the applet border. Designed for gray-on-gray browser background. g.setColor(Color.black); g.drawLine(0, 0, dApplet.width - 1, 0); g.drawLine(0, 0, 0, dApplet.height - 1); g.setColor(Color.white); g.drawLine(0, dApplet.height - 1, dApplet.width - 1, dApplet.height - 1); g.drawLine(dApplet.width - 1, 1, dApplet.width - 1, dApplet.height - 1); } // End of paint() class alButtonEvents implements ActionListener { public void actionPerformed(ActionEvent ae) { String s = ae.getActionCommand(); int i = 0; int j = 0; int iTemp = 0; // Swap space. int iRand = 0; if (s.equals("Scramble Dates")) { taOutput.setText(""); for (i = 0; i < iNumDates - 1; i++) { iRand = i + 1 + (int) (Math.random() * (iNumDates - 1 - i)); System.out.println("Rand = " + iRand); iTemp = iArray[i]; iArray[i] = iArray[iRand]; iArray[iRand] = iTemp; } for (i = 0; i < iNumDates; i++) { taOutput.append(Integer.toString(iArray[i]) + "\n"); } } if (s.equals("Sort Dates")) { for (i = 0; i < iNumDates - 1; i++) { for (j = i + 1; j < iNumDates; j++) { if (iArray[j] < iArray[i]) { // Swap! iTemp = iArray[j]; iArray[j] = iArray[i]; iArray[i] = iTemp; } } } taOutput.setText(""); for (i = 0; i < iNumDates; i++) { taOutput.append(Integer.toString(iArray[i]) + "\n"); } } } // End of actionPerformed() } // End of class alButtonEvents } // End of Applet class