This homework is designed to give you some practice with different types of geometric transformations.

  1. Write a translation matrix that will translate an object by 50 pixels in the X direction and 100 pixels in the Y direction.

  2. Write a scaling matrix that will scale an object by a factor of 2 in the X direction and a factor of 0.5 in the Y direction.

  3. Write a rotation matrix that will rotate an object by 45 degrees. Do not worry about which point the rotation is being performed.

  4. Write a set of matrices that will rotate an object about its center. Assume that the object has a width of wd, a height of ht, and that its upper left corner is located at the point (x,y). Your solution must show the order in which the matrices are multiplied, as shown in the Olsen text.

  5. Suppose you want to both rotate and scale a rectangle, and that you want to preserve the parallel and perpendicular lines of the rectangle. In which order should you perform the transformations (i.e., should you rotate first, and then scale, or scale first and then rotate)?

  6. Why is it important to restore the original graphics transform when you exit the paintComponent method in Java? Your answer should specify what will happen if you forget to restore the original graphics transform (i.e., what will happen to the border around the component).

  7. When applying a series of transformations in paintComponent, why is it important to augment the transformation matrix that is passed into paintComponent, rather than building a new transformation matrix from scratch? Specifically, your answer should explain what will happen to your graphics if you build your transformation matrix from scratch, rather than augmenting the transformation matrix that is passed into paintComponent.

  8. Recall that the coordinate axes in a JPanel starts at the upper left corner and that the y-axis is pointed down, rather than up. This arrangment can be unnatural for specifying the coordinates of many objects, since we normally think of the origin as being at the lower left corner of a window and the y-axis as moving up. Assuming that you have a Graphics2D object g2, show the Java transforms you would call in order to move the origin to the lower left corner and make the y-axis point up. You do not have to place your code in a java file. I just want to see the code fragment you would write to transform g2 to change the coordinate axes using a combination of the Graphic2D class's translate and scale methods. As an example, if you draw a rectangle at coordinates (20, 20), I would expect the rectangle to be drawn at an offset of (20, 20) from the bottom left corner of the JPanel, rather than the upper left corner, as shown below:
    If you re-sized the JPanel, the rectangle would stay attached to the lower left hand corner of the JPanel.

    To provide context, you would insert your code into the following code fragment:

        public void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	Rectangle r = new Rectangle(20, 20, 200, 100);
    	Graphics2D g2 = (Graphics2D) g;
    	AffineTransform saveTransform = g2.getTransform();
            // YOUR CODE FOR MODIFYING g2's TRANSFORM MATRIX GOES HERE
    
    	g2.draw(r); // draws rectangle in the lower left corner of the JPanel
    	g2.setTransform(saveTransform);
        }
    
  9. Modify the interface that you developed in homework assignment 5 so that it allows panning and zooming.

    1. Panning should be accomplished by clicking the mouse button anywhere in the drawing window and moving the mouse.
    2. Zooming should be accomplished by scaling from the center of the window and using a zoom slider in a new control window. The zooming should be anywhere from 0-300%, and, as shown in class and in the class notes, you should ensure that the scaling factor never actually goes to absolute zero.
    3. Make sure that when you pan the display after zooming, that you transform the mouse point to the new coordinate system, so that panning is done in the zoomed coordinate system, not the original coordinate system. The way to tell whether or not you are panning in the zoomed coordinate system is to place the mouse cursor inside one of the labeled boxes and pan the display. If you are panning correctly, the mouse cursor should stay pinned to the same location in the box. If you have not accounted for the zooming, the mouse cursor will drift outside the box.
    4. To make the panning more realistic, you should try creating some boxes that exceed the dimensions of the window, and watching how you can make them appear by panning the window toward them.
    5. You should work by yourself on this question.
    6. If you worked in a group on the previous homework assignment, modify your group's files.
    7. If you could not make homework assignment 5 work, then you can use my solution source files from that assignment. You should be able to write your own zoom control window for controlling the zoom slider and you should be able to modify my DrawPanel.java class. You will need to modify and/or add the following elements to DrawPanel:

      1. paintComponent must be modified to handle panning and zooming.
      2. the selection handling code must be modified so that it transforms the mouse point to the current coordinate system before trying to find the selected object. If you fail to do so, you will find that you cannot select objects after you have panned them.
      3. you need to add event handling code to manage panning. When you pan, remember to transform the mouse coordinates to the current coordinate system, or else you may find that if your mouse cursor is over an object, it does not stay attached to the object.


What to Submit

  1. A hw8.txt or hw8.pdf file that contains your answers to questions 1-8. If you are using an ascii text file, it is okay to specify matrices using the following ascii art:
      | a   b   c |
      | d   e   f |
      | 0   0   1 |
      
    where a-f represents your coefficients.
  2. For problem 9, a jar file named LabeledBox.jar with the following files:

    We will expect to be able to execute your jar file by typing:
    java -jar LabeledBox.jar