import java.awt.*; import java.awt.event.*; import javax.swing.*; // NOTHING to change in this class, only finish methods in BST class. public class DisplayWindow extends javax.swing.JFrame implements MouseListener { BinaryTreeNode tree; static int window_size = 700; int moveCounter = 0; public void mouseClicked(MouseEvent mouseEvent) { Point clickPoint = mouseEvent.getPoint(); BinaryTreeNode clickNode = BinaryTreeNode.getNodeMouseClick(clickPoint); if ( clickNode == null ) { if( clickPoint.y > this.getHeight() - 40 ) { this.tree = new BinaryTreeNode(5, null, 0, 100); moveCounter = 0; repaint(); } return; } if( mouseEvent.getButton() == mouseEvent.BUTTON1 ) { if( clickNode.rotateAlternate() ) moveCounter++; } else { if( clickNode.rotate() ) moveCounter++; } repaint(); } public void paint(Graphics g) { super.paint(g); tree.addGraphics(g); g.drawString( "Left click node rotates clockwise (your task in the lab)", 20, this.getHeight() - 70); g.drawString( "Right click node rotates counterClockwise (provided to you)", 20, this.getHeight() - 50 ); g.drawString( "Click here to generate new random BST", 20, this.getHeight() - 30 ); g.setColor(Color.BLUE); g.setFont(new Font("Verdana",Font.BOLD,20)); g.drawString( "Moves: "+moveCounter, this.getWidth() - 130, this.getHeight() - 15 ); } public DisplayWindow() { this.tree = new BinaryTreeNode(5, null, 0, 100); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setSize(window_size,window_size); setTitle("Balance the BST game"); this.addMouseListener(this); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DisplayWindow().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) { } }