public TreeViewer(String title, int ulx, int uly, int pw, int ph) { super(title, ulx, uly, pw, ph); // code to initialize instance variables before animation begins: // ------------------------------------------------------------------ state = "regular"; spread = 1; levelHeight = 10; // 10 levels for the camera region height tree = new SackBST(); String fileName = FileBrowser.chooseFile(true); try { Scanner input = new Scanner(new File(fileName)); String s; while (input.hasNext()) { s = input.nextLine(); if (s != null) { tree.add(s); } } input.close(); } catch (Exception e) { System.out.println("File load failed"); System.exit(1); } // code to finish setting up entire window: // ------------------------------------------------------------------ setBackgroundColor(new Color(128, 128, 200)); // code to set up camera(s) // ------------------------------------------------------------------ cameras.add(new Camera(10, 50, camw, camh, 0, 100, 0, new Color(255, 200, 255))); cameras.add(new Camera(10, 50 + camh + 10, camw, 20, 0, 100, 0, new Color(255, 255, 255))); // ------------------------------------------------------------------ // start up the animation: super.start(); }
public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); Camera cam = cameras.get(0); if (state.equals("regular")) { if (code == KeyEvent.VK_L) { cam.shiftRegion(0.25, 0); } else if (code == KeyEvent.VK_R) { cam.shiftRegion(-0.25, 0); } else if (code == KeyEvent.VK_U) { cam.shiftRegion(0, -0.25); } else if (code == KeyEvent.VK_D) { cam.shiftRegion(0, 0.25); } else if (code == KeyEvent.VK_S) { cam.scaleRegion(1.1, 1.1); } else if (code == KeyEvent.VK_B) { cam.scaleRegion(1 / 1.1, 1 / 1.1); } else if (code == KeyEvent.VK_W) { spread *= 1.1; } else if (code == KeyEvent.VK_N) { spread /= 1.1; } else if (code == KeyEvent.VK_H) { cam.setRegion(0, 100, 0, 100); } } // regular state else if (state.equals("add")) { if (code == KeyEvent.VK_ENTER) { state = "regular"; if (!stringToAdd.equals("")) tree.add(stringToAdd); } else if (code == KeyEvent.VK_DELETE || code == KeyEvent.VK_BACK_SPACE) { if (stringToAdd.length() > 0) stringToAdd = stringToAdd.substring(0, stringToAdd.length() - 1); } } // add state }