// // Student Name: Aleman Meza, Boanerges import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; /** * Applet for Binary Tree */ public class Dsv1 extends JApplet { private JPanel northPanel, westPanel, eastPanel, southPanel; private JPanel subSouthPanel; private DrawPanel drawArea; protected JLabel statusLabel; protected JTextField preTextField, inTextField, postTextField; protected JButton clearButton, enterButton, findButton, deleteButton; private int width = 620, height = 450; protected boolean amIanApplication; protected String dsvInfo, llInfo; public void setWidth(int w) { width = (w >= 0 ? w : width); } public void setHeight(int h) { height = (h >= 0 ? h : height); } public void init(boolean isApplication) { amIanApplication = isApplication; init(); } public void checkButtons() { boolean enabled = drawArea.hasElements(); findButton.setEnabled(enabled); deleteButton.setEnabled(enabled); preTextField.setText(drawArea.getPreOrder()); inTextField.setText(drawArea.getInOrder()); postTextField.setText(drawArea.getPostOrder()); } public void init() { statusLabel = new JLabel("Welcome to DSV - Binary Tree Made Easy"); northPanel = new JPanel(); westPanel = new JPanel(); eastPanel = new JPanel(); southPanel = new JPanel(); drawArea = new DrawPanel(); preTextField = new JTextField(); preTextField.setEditable(false); inTextField = new JTextField(); inTextField.setEditable(false); postTextField = new JTextField(); postTextField.setEditable(false); dsvInfo = "The Data Structures Visualization\n"+ "(DSV) Group is comprised of three\n"+ "Graduate Students at the University\n"+ "of Georgia under the supervision of\n"+ "Dr. Eileen Kraemer. DSV's purpose\n"+ "is to provide interactive learning\n"+ "for the CSCI 2720 Data Structures\n"+ "Course at the University of Georgia.\n"+ "\n\n\n\n\n"+ "DSV Group:\n"+ " Boanerges Aleman\n"+ " Navdeep Latawa\n"+ " James Skinner\n"; llInfo = "Binary Tree info. at \n" + "www.cs.uga.edu/~boanerg/hci2/partc/bintree"; JMenuBar menuBar = new JMenuBar(); setJMenuBar (menuBar); JMenu optionMenu = new JMenu("Options"); optionMenu.setMnemonic('O'); menuBar.add(optionMenu); JCheckBoxMenuItem step = new JCheckBoxMenuItem("Step by Step"); JCheckBoxMenuItem code = new JCheckBoxMenuItem("Sample Code"); step.setEnabled(false); code.setEnabled(false); optionMenu.add(step); optionMenu.add(code); JMenu infoMenu = new JMenu("Information"); infoMenu.setMnemonic('I'); menuBar.add(infoMenu); JMenuItem linkInfo = new JMenuItem("Binary Tree Info."); linkInfo.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Dsv1.this, llInfo, "The Linked List Information Box", JOptionPane.INFORMATION_MESSAGE); } } ); infoMenu.add(linkInfo); JMenu aboutMenu = new JMenu("About Demo"); aboutMenu.setMnemonic('A'); menuBar.add(aboutMenu); JMenuItem aboutItem = new JMenuItem("About DSV"); aboutItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Dsv1.this, dsvInfo, "Data Structures Visualization (DSV)", JOptionPane.INFORMATION_MESSAGE); } } ); aboutMenu.add(aboutItem); clearButton = new JButton("Clear"); clearButton.setMnemonic('C'); clearButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Dsv1.this, "Tree is cleared", "Clear", JOptionPane.PLAIN_MESSAGE); drawArea.clear(); Dsv1.this.checkButtons(); } } ); enterButton = new JButton("Add..."); enterButton.setMnemonic('A'); enterButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String value = JOptionPane.showInputDialog(Dsv1.this, "Add Element"); if(value != null && !value.trim().equals("")) { drawArea.addElement(value); } Dsv1.this.checkButtons(); doLayout(); } } ); findButton = new JButton("Find..."); findButton.setMnemonic('F'); findButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String value = JOptionPane.showInputDialog(Dsv1.this, "Find Element"); if(value != null && !value.trim().equals("")) { if(drawArea.findElement(value)) JOptionPane.showMessageDialog(Dsv1.this, "Element " + value.trim() + " Found", "Delete Element", JOptionPane.PLAIN_MESSAGE); else JOptionPane.showMessageDialog(Dsv1.this, "Element Not Fount ", "Find Element", JOptionPane.PLAIN_MESSAGE); } } } ); findButton.setEnabled(false); deleteButton = new JButton("Delete..."); deleteButton.setMnemonic('D'); deleteButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String value = JOptionPane.showInputDialog(Dsv1.this, "Delete Element"); if(value != null && !value.trim().equals("")) { if(!drawArea.deleteElement(value)) JOptionPane.showMessageDialog(Dsv1.this, "Element Not Fount ", "Delete Element", JOptionPane.PLAIN_MESSAGE); } Dsv1.this.checkButtons(); } } ); deleteButton.setEnabled(false); subSouthPanel = new JPanel(); subSouthPanel.setLayout(new FlowLayout()); subSouthPanel.add(clearButton); subSouthPanel.add(enterButton); subSouthPanel.add(findButton); subSouthPanel.add(deleteButton); southPanel.setLayout(new BorderLayout()); southPanel.add(subSouthPanel, BorderLayout.CENTER); southPanel.add(statusLabel, BorderLayout.SOUTH); westPanel.add(new JLabel(" ")); eastPanel.setLayout(new GridLayout(10, 1)); eastPanel.add(new JLabel("Traversals:")); eastPanel.add(new JLabel("pre-order:")); eastPanel.add(preTextField); eastPanel.add(new JLabel("in-order:")); eastPanel.add(inTextField); eastPanel.add(new JLabel("post-order:")); eastPanel.add(postTextField); Container myContainer = getContentPane(); myContainer.setLayout(new BorderLayout()); myContainer.add(new JScrollPane( drawArea), BorderLayout.CENTER); myContainer.add(westPanel, BorderLayout.WEST); myContainer.add(southPanel, BorderLayout.SOUTH); myContainer.add(eastPanel, BorderLayout.EAST); addKeyListener( new KeyAdapter() { public void keyTyped( KeyEvent e ) { String value = "" + e.getKeyChar(); if(!value.trim().equals("")) { drawArea.addElement(value); Dsv1.this.checkButtons(); doLayout(); } } } ); setSize(width, height); setVisible(true); } public static void main(String args[]) { int width = 620, height = 450; if(args.length == 2) { try { width = Integer.parseInt(args[0]); height = Integer.parseInt(args[1]); } catch(Exception ex) { width = 620; height = 450; } } JFrame myFrame = new JFrame("Application DSV"); myFrame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); Dsv1 appletObject = new Dsv1(); appletObject.setWidth(width); appletObject.setHeight(height); appletObject.init(true); appletObject.start(); myFrame.getContentPane().add(appletObject); myFrame.setSize(width, height); myFrame.setVisible(true); } }