This homework is designed to give you some practice with different types of geometric
transformations.
- Write a translation matrix that will translate an object by 50 pixels in the X
direction and 100 pixels in the Y direction.
- 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.
- Write a rotation matrix that will rotate an object by 45 degrees. Do not worry about
which point the rotation is being performed.
- Write a set of matrices that will rotate an object by an angle A
about its center. Assume that
the object has a width of w, a height of h, 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.
- 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)?
- 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 and other components being laid out after this component).
- 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.
- 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);
}
- Modify the interface that you developed in homework assignment 5 so that it
allows panning and zooming. Panning should be accomplished by clicking the
mouse button anywhere in the drawing window and moving the mouse. 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. 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. You should work by yourself on this question. It is okay to use my homework
solutions to homework 5. You can find the source code for my solution
in the directory /home/bvz/gui/hw/hw5/solutions/src.
If you worked in a group on
the previous homework assignment, modify either your group's files or my files.
Here are a couple more points about the problem:
- If you have questions about how your interface should work, you can
execute my jar file:
java -jar /home/bvz/gui/hw/hw6/LabeledBoxApplication.jar
Should you copy this jar file to your directory, make sure you also
copy the lib directory, or else your jar file will not work.
- Make sure that you can still select labeled boxes after panning
and zooming. You will need to convert the mouse coordinates
to the panning/zoomed coordinates in order to check whether
the mouse point is contained in one of the boxes. You can
invert the pan/zoom transform in order to convert the mouse
coordinates.
What to Submit
Submit a jar file named LabeledBoxApplication.jar with the following files:
- A hw6.txt or hw6.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.
- A set of appropriately named .java files, one for each of your
classes and interfaces (if you have any interfaces), for question 9.
- A file named LabeledBoxApplication.java that contains the code for setting up
your interface for question 9.
- A manifest.txt file that indicates that LabeledBoxApplication is your main class.
- All your class files.
We will expect to be able to execute your jar file by typing:
java -jar LabeledBoxApplication.jar