swoop

ZoomListener

Apr 12th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. class ZoomListener extends FractalListener implements MouseInputListener{
  2.     private double startX, startY, finalX, finalY;
  3.     private Rectangle selection;
  4.     private Point anchor;
  5.    
  6.     @Override
  7.     public void mousePressed(MouseEvent e){
  8.         updateDetails();
  9.         startX = e.getX();
  10.         startY= e.getY();
  11.         anchor = e.getPoint();
  12.         selection = new Rectangle(anchor);
  13.         drawingPanel.setIsZooming(true);
  14.         drawingPanel.setZoomRect(selection);
  15.     }
  16.    
  17.     @Override
  18.     public void mouseReleased(MouseEvent e){
  19.         finalX = e.getX();
  20.         finalY = e.getY();
  21.         double xScale = (xMaxVal - xMinVal) / size.getWidth();
  22.         double yScale = (yMaxVal - yMinVal) / size.getHeight();
  23.         startX = xMinVal + (startX * xScale);
  24.         finalX = xMinVal + (finalX * xScale);
  25.         startY = yMaxVal - (startY * yScale);
  26.         finalY = yMaxVal - (finalY * yScale);
  27.         if(startX != finalX && startY != finalY){
  28. imageDetails[0] = Math.min(startX, finalX);
  29. imageDetails[1] = Math.max(startX, finalX);
  30. imageDetails[2] = Math.min(startY, finalY);
  31. imageDetails[3] = Math.max(startY, finalY);
  32. String text = new String();
  33. text += imageDetails[0];
  34. realMin.setText(text);
  35. text = "";
  36. text += imageDetails[1];
  37. realMax.setText(text);
  38. text = "";
  39. text += imageDetails[2];
  40. imgMin.setText(text);
  41. text = "";
  42. text += imageDetails[3];
  43. imgMax.setText(text);
  44. drawingPanel.setIsZooming(false);
  45. selection = null;
  46. drawingPanel.setZoomRect(selection);
  47. repaint();
  48.         }
  49.     }
  50.    
  51.     @Override
  52.     public void mouseDragged(MouseEvent e){
  53.         selection.setBounds( (int)Math.min(anchor.x,e.getX()), (int)Math.min(anchor.y,e.getY()),
  54.                 (int)Math.abs(e.getX()-anchor.x), (int)Math.abs(e.getY()-anchor.y));
  55.         drawingPanel.repaint();
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment