This homework assignment is designed to give you some experimental experience with different incremental display algorithms and a feel for how using an incremental display algorithm can improve the interactive feel of an application. Start this assignment by saving TotalRedraw.java to your directory. Compile it and run it. You should see a checkerboard background with a black rectangle on top. Try dragging the black rectangle around the display with your mouse. See how the rectangle struggles to keep up with the mouse? The reason is that I have created a 500x500 checkerboard, with most of the checkerboard displayed off the screen. TotalRedraw is drawing 250,000 rectangles with each mouse drag. If your computer is faster or slower at redrawing rectangles than mine is, try adjusting the NUM_ROWS variable up or down.

  1. Copy TotalRedraw.java to a new file named XorRedraw.java.

    1. Change the program so that it draws a dashed line rectangle as a feedback object when you are dragging the rectangle, and then redraws the black rectangle at its new location when you are done.
    2. Use xor mode for drawing the dashed line rectangle and do not redraw any of the objects in the checkerboard during the dragging process. Remember that you can make the xor'ed rectangle appear black over either the white checkerboards or over the red checkerboards, but that you cannot make it appear black over both of them. If you want your xor'ed rectangle to appear as black when it is over the white checkerboards, then you must set the color of the graphics context to white and the xor alternation color to black. Alternatively, if you want the xor'ed rectangle to appear as black when it is over the red checkerboards, then you must set the color of the graphics context to red and the xor alternation color to black.
    3. You can check out my example by typing:
      	       java /home/bvz/gui/hw/hw7/XorRedraw
      	     
      I used a line thickness of 2, a dash pattern of 10, and made the feedback rectangle be black over the white checkerboards, but you can use anything, so long as it is clear that you have a dashed line rectangle.
    4. The xor'ing of the dashed line rectangle using the xor'ing technique discussed in class should be sufficient to draw and erase the dashed line rectangle.

  2. Copy TotalRedraw.java to a new file named ClipRedraw.java. This time do not change the behavior of the program--it should still have the black rectangle follow the mouse around the display. However, you should make the following changes to the drawing algorithm:

    1. As the user moves the black rectangle around the display, you should call repaint both before and after you set the rectangle's location. The first time you call repaint, pass it the old coordinates of the black rectangle. Then set the black rectangle to its new coordinates and call repaint a second time, this time with the new coordinates of the black rectangle. The two calls will merge the old and new bounding boxes of the black rectangle and create a single paint event.

    2. Change paintComponent so that objects in the checkerboard are only redrawn if they intersect the clipping region

    I do not have an example executable for this one, but your rectangle should no longer struggle to keep up with the mouse.

  3. The algorithm in the previous problem redraws only the checkerboard objects that intersect the moving rectangle, but it still requires that you examine all the objects, making it an O(n) algorithm. Given the regular arrangement of the checkerboard, describe in 5 sentences or less a more efficient redraw algorithm that only examines O(1) checkerboard objects.

  4. Suppose that you had a move interactor available to you for implementing the problem which uses the dashed line rectangle as feedback. Assume that you are given a very stripped down move interactor that only provides the following functionality:

    1. The start, stop, and running actions take a single parameter. The parameter is a MoveEvent object with the following interface:
      1. int getLeft(): the new left
      2. int getTop(): the new top
      3. Object getSource(): the object being dragged
      Note that MoveEvent is a pretend class for this problem so you cannot find any documentation on it, other than what you see above.

    Unlike in class there is no property for a feedback object, so you will have to make your actions handle the following items:

    1. make the feedback object visible/invisible (do not worry about whether or not it is drawn using Xor--assume that making it visible or invisible is enough to get the feedback object drawn properly)
    2. make the feedback object track the mouse
    3. make the rectangle being moved jump to its new location when the move behavior terminates.
    4. call repaint to get the canvas to repaint (do not worry about passing the damaged area as an argument to repaint).

    Assume that you are moving Java Rectangle objects, and that the following variables have been declared for you:

    	Rectangle feedbackRect; // the feedback rectangle
            // whether or not the feedback rectangle is visible
    	boolean feedbackVisible; 
          
    Write Java code to complete the following template. Assume that the addInteractor method replaces the addListener methods in the existing code.
    	 addInteractor(new MoveInteractor() {
    	   public void startAction(MoveEvent e) { // YOUR CODE }
    	   public void runningAction(MoveEvent e) { // YOUR CODE }
    	   public void stopAction(MoveEvent e) { // YOUR CODE }
    	 });
           
    You will put this code in either an ascii text file or in your word processor. You cannot implement it because you do not have the underlying interactor runtime environment with which to test it.