// import things for window import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.border.*; import javax.swing.*; public class BinarySearchApplet extends Applet implements ActionListener { Panel top; Panel bottom; Panel grid; Button search; Button step; TextField needle; Label needleLabel; String found = "Found at index: "; String foundStart = "Please enter a search term"; Label foundLabel; String searchText = "Search"; String contin = "Continue"; int[] haystack = {1, 2, 4, 7, 9, 10, 16, 22, 27, 32, 58}; int digits = (haystack[haystack.length - 1] + "").length(); int thisFirst = 0; int thisLast = haystack.length - 1; int thisNeedle = 0; JLabel[][] boxes = new JLabel[2][haystack.length]; int[] fontSize = {50, 30}; public void init() { this.setLayout(new BorderLayout()); grid = new Panel(); grid.setLayout(new GridLayout(2, 1)); top = new Panel(); top.setLayout(new GridLayout(boxes.length, boxes[0].length)); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { int labelText = (i == 0) ? haystack[j] : j; boxes[i][j] = new JLabel("" + labelText, JLabel.CENTER); if (i == 0) { boxes[i][j].setOpaque(true); boxes[i][j].setBorder(BorderFactory.createLineBorder(Color.black)); } else { boxes[i][j].setVerticalAlignment(JLabel.TOP); } boxes[i][j].setFont(new Font("Monospaced", Font.BOLD, fontSize[i])); top.add(boxes[i][j]); } } bottom = new Panel(); needleLabel = new Label("Needle:"); needle = new TextField("", digits); search = new Button(searchText); search.addActionListener(this); step = new Button(contin); step.setEnabled(false); step.addActionListener(this); bottom.add(needleLabel); bottom.add(needle); bottom.add(search); bottom.add(step); grid.add(top); grid.add(bottom); foundLabel = new Label(foundStart); foundLabel.setFont(new Font("Monospaced", Font.BOLD, 30)); this.add(grid, "Center"); this.add(foundLabel, "South"); repaint(); } public void binarySearch(int[] haystack, int first, int last, int needle) { if (first <= last) { int mid = (first + last) / 2; if (haystack[mid] == needle) { reset(found + mid); boxes[0][mid].setBackground(Color.PINK); repaint(); } else if (haystack[mid] > needle) { info(first, mid - 1, needle); } else { info(mid + 1, last, needle); } } else { reset(needle + " is not in the array"); } } public void reset(String s) { foundLabel.setText(s); for (int i = 0; i < haystack.length; i++) { boxes[0][i].setBackground(Color.GRAY); } step.setEnabled(false); repaint(); } public void info(int first, int last, int needle) { thisFirst = first; thisLast = last; thisNeedle = needle; for (int i = first; i <= last; i++) { boxes[0][i].setBackground(new Color(255, 255, 151)); } for (int i = 0; i < first; i++) { boxes[0][i].setBackground(Color.GRAY); } for (int i = last + 1; i < haystack.length; i++) { boxes[0][i].setBackground(Color.GRAY); } boxes[0][(first + last) / 2].setBackground(Color.YELLOW); repaint(); } public void paint( Graphics g ) { } public void actionPerformed(ActionEvent e) { String c = e.getActionCommand(); if(c.equals(searchText)) { try { thisNeedle = Integer.parseInt(needle.getText()); needle.setForeground(Color.BLACK); foundLabel.setForeground(Color.BLACK); foundLabel.setText("Searching for: " + thisNeedle); step.setEnabled(true); info(0, haystack.length - 1, thisNeedle); repaint(); } catch (Exception exc) { needle.setForeground(Color.RED); foundLabel.setForeground(Color.RED); foundLabel.setText("Please enter an integer"); repaint(); } } else if(c.equals(contin)) { binarySearch(haystack, thisFirst, thisLast, thisNeedle); } } }