import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; // NOTHING to change in this class public class lab5Client extends javax.swing.JFrame implements MouseListener { BinaryHeap binaryHeap; String operationStr = ""; public void mouseClicked(MouseEvent mouseEvent) { Point clickPoint = mouseEvent.getPoint(); int clickNodeIndex = binaryHeap.getNodeIndexMouseClick(clickPoint); if ( clickNodeIndex < 0 ) { if( clickPoint.y < this.getHeight() - 40 ) return; if( clickPoint.x < this.getWidth()/2 ){ binaryHeap = new BinaryHeap(); operationStr = ""; } else if( clickPoint.x > this.getWidth()/2 && clickPoint.x < 0.75 * this.getWidth() ){ String str = JOptionPane.showInputDialog(null, "Insert value: ", "Insert value: ", 1); binaryHeap.insert(Integer.parseInt(str)); operationStr = "Insert value ( " + Integer.parseInt(str)+ " )"; } else if( clickPoint.x > 0.75 * this.getWidth() ) { operationStr = "deleteMin() = " + binaryHeap.deleteMin(); } repaint(); return; } if( mouseEvent.getButton() == mouseEvent.BUTTON1 ) { operationStr = "percolateDown( "+binaryHeap.dataArray[clickNodeIndex]+" )"; binaryHeap.percolateDown(clickNodeIndex); } else { operationStr = "percolateUp( "+binaryHeap.dataArray[clickNodeIndex]+" )"; binaryHeap.percolateUp(clickNodeIndex); } repaint(); } public void paint(Graphics g) { super.paint(g); binaryHeap.addGraphics(g, this.getWidth(), this.getHeight()); g.setColor(Color.BLUE); g.drawString( operationStr, 20, this.getHeight() - 100); g.setColor(Color.MAGENTA); g.drawString( "Left click node => percolateDown(node value)", 20, this.getHeight() - 60); g.drawString( "Right click node => percolateUp(node value)", 20, this.getHeight() - 40 ); g.setColor(Color.BLUE); g.drawString( "Click for random BinaryHeap", 20, this.getHeight() - 20 ); g.drawString( "Insert", (int)(this.getWidth()*0.5), this.getHeight() - 20); g.drawString( "DeleteMin", (int)(this.getWidth()*0.75), this.getHeight() - 20); } public lab5Client() { binaryHeap = new BinaryHeap(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setSize(900, 500); setTitle("Binary heap display"); this.addMouseListener(this); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new lab5Client().setVisible(true); } }); } // Other mouse listener event handlers (NOT needed for our purposes, just leave blank) public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } }