JAVA   25
CrossHairs
Guest on 9th February 2023 03:03:08 AM


  1. import objectdraw.*;
  2. import java.awt.*;
  3.  
  4. /* A program that draws perpendicular lines that move
  5.  so that they always intersect at the mouse's current
  6.  position */
  7. public class CrossHairs extends WindowController {
  8.  
  9.         private Line vert, horiz; // lines forming crosshairs
  10.  
  11.         private int canvasWidth, canvasHeight;
  12.  
  13.         // Initially center the lines
  14.         public void begin() {
  15.                 canvasWidth = canvas.getWidth();
  16.                 canvasHeight = canvas.getHeight();
  17.  
  18.                 vert = new Line(canvasWidth / 2, 0, canvasWidth / 2, canvasHeight,
  19.                                 canvas);
  20.                 horiz = new Line(0, canvasHeight / 2, canvasWidth, canvasHeight / 2,
  21.                                 canvas);
  22.         }
  23.  
  24.         // move the lines so that they intersect at the mouse
  25.         public void onMouseDrag(Location point) {
  26.                 vert.setEndPoints(point.getX(), 0, point.getX(), canvasHeight);
  27.  
  28.                 horiz.setEndPoints(0, point.getY(), canvasWidth, point.getY());
  29.         }
  30.  
  31. }

Raw Paste

Login or Register to edit or fork this paste. It's free.