Homework 2, due Wednesday, Sept 18.

When you have finished this assignment, post the working applet onto the web. The grader and I will be able to look at it there; there is no need to send any email.

  1. Create an interesting picture that uses only the setColor, drawLine, drawPolygon and fillPolygon methods from class java.awt.Graphics.

  2. Provide a way for a user to interact with this picture through mouse movements, so as to translate, rotate and scale it. Try to come up with an interesting way for the user to use the mouse to translate, rotate and scale the picture. If you want, you can use my Slider class from http://mrl.nyu.edu/~perlin/car/Car.html, or you can come up with something else, such as designating different portions of the applet window as "translate", "rotate" and "scale" zones that respond differently when the user drags the mouse, or else creating "handles" superimposed over the picture, which the the user can drag on. Feel free to use any methods from java.awt.Graphics to draw your interaction widgets.

Internally, you should keep the original, untransformed version of the picture, as well as a 3×3 transformation matrix. Make changes only to this matrix, and then apply the matrix to your original picture to create the transformed picture that the user sees.

To effect these transformations, implement a Matrix_3x3 class. You'll need to implement an initializing identity() method, as well as translate(double tx, double ty), rotate(double theta) and scale(double sx, double sy) methods that modify your matrix. You'll also need to implement transformX(double x, double y) and transformY(double x, double y) methods, to apply the matrix to the coordinates of the vertices in your picture.

Here is a very simple example of valid code that would use such a matrix class:

   // COORDINATES OF A SQUARE SHAPE

   int X[] = {100, 200, 200, 100};
   int Y[] = {100, 100, 200, 200};

   ////// FIRST CREATE A TRANSFORMATION

   Matrix_3x3 M = new Matrix_3x3();
   M.identity();
   M.scale(2, 2);
   M.rotate(Math.PI/2);
   M.translate(100, 100);

   ////// THEN APPLY TRANSFORMATION TO THE SHAPE

   // LOOP THROUGH ALL POINTS

   for (int i = 0 ; i < X.length ; i++) {

      // INDEX OF THE NEXT POINT AROUND THE SHAPE

      int j = (i+1) % X.length;

      // DRAW LINE BETWEEN SUCCESSIVE TRANSFORMED POINTS

      g.drawLine((int)M.transformX(X[i],Y[i]), (int)M.transformY(X[i],Y[i]),
                 (int)M.transformX(X[j],Y[i]), (int)M.transformY(X[i],Y[j]));
   }