Homework 6


This homework assignment has two purposes:

  1. 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.
  2. to give you some experience with using and writing interactors.

Display Management

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 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.
    3. You can check out my example by typing:
      	       java /home/bvz/gui/hw/hw7/XorRedraw.jar
      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 with the old and new coordinates of the rectangle (call repaint twice for simplicity since the two calls will merge the bounding boxes and create a single paint event). Note that there will be almost a total overlap between the old and new locations of the rectangle because java reports every single pixel change in the mouse so the rectangle will only move one pixel at a time in either the horizontal or vertical direction. Hence merging the two bounding boxes is quite appropriate for this problem.

    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.


Now you are going to get some practice with interactors. Start by compiling and running the MoveApplication.java file in ~bvz/gui/hw/hw6. To compile it, you will need to copy both the MoveApplication.java file and the interactors directory to your directory. Then type:

javac -cp .:.. MoveApplication.java
and try running it. It should display a window with 10 rectangles. If you mouse down over any of the rectangles and drag, the rectangles should follow the mouse.

  1. In MoveApplication.java change the MoveInteractor so that instead of moving the rectangles during the mouse dragging, it displays and moves a dashed line feedback rectangle that represents the rectangle to be moved. Only when you release the mouse button should the rectangle jump to its new location. To effect this change, you will need to:

    1. Convert the "new MoveInteractor" statement to a new statement that creates an anonymous MoveInteractor class that overrides the startAction, runningAction, and stopAction methods.
    2. Declare a RectShape object in MoveApplication that represents a feedback object.
    3. Have your start, running, and stop actions manipulate the location of this feedback object and set a visible flag that indicates whether or not the feedback object is visible.
    4. Modify the paintComponent method to draw the feedback rectangle as a dashed line rectangle when it is visible.

    You can run my ~bvz/gui/hw/hw6/MoveApplication.jar application to see what your revised application should do.

  2. Copy my original MoveApplication.java file to a new file called NewObjectApplication.java and copy my ~bvz/gui/hw/hw6/interactors/MoveInteractor.java file to a new file called NewObjectInteractor.java (this file will need to be in your interactors directory).

    1. Edit the NewObjectInteractor.java file so that it calculates a bounding box for a new object. When it calls the start action method, it should provide the top/left of the object as parameters, and when it calls the running and stop action methods, it should provide the top/left and size (width/height) of the object as parameters. All three methods should also take the mouse event as a parameter. You will need to rewrite the mousePressed, mouseDragged, and mouseReleased methods so that the mousePressed method captures the initial (x,y) coordinates of the object and the mouseDragged and mouseReleased methods calculate the width and height of the object, based on the assumption that the current mouse event is the lower right corner of the new object.

    2. Edit the NewObjectApplication.java file so that it creates a NewObjectInteractor and uses it to create new rectangles whenever the user drags out a rectangle on the screen. Your instance of the NewObjectInteractor should override the start, running, and stop actions so that it displays a dashed line feedback rectangle as the user is dragging, and then displays the new rectangle in blue when the users releases the mouse button. The NewObjectInteractor should operate anywhere within the JPanel, so you will need to create a single rectangle that is the same size as the JPanel and register it with the NewObjectInteractor (this rectangle will not actually be displayed in the JPanel). If the JPanel is resized, you should take care to resize this rectangle as well (you can do it whenever paintComponent gets called).

    You can run my ~bvz/gui/hw/hw6/NewObjectApplication.jar application to see what your application should do. Press and drag with the mouse button to create new rectangles.

What To Submit

You should submit the following files using the submit script:

  1. XorRedraw.java/Xor.jar
  2. ClipRedraw.java/ClipRedraw.jar
  3. MoveApplication.java/MoveApplication.jar
  4. NewObjectApplication.java/NewApplication.jar
  5. NewObjectInteractor.java
  6. hw6.pdf or hw6.txt: A pdf or ascii text file with your answer to question 3 in the display management section.
The jar files should all execute the corresponding program. The .java files should not be bundled with the .jar files but instead be separate standalone files that we can inspect.