Homework Assignment 8
This homework assignment will give you practice with:
- Java's layout managers
- the MVC model
- custom drawing and event handling in Java
- Modify the BoxLayoutDemo found in /home/bvz/cs365/hw/hw8
in the following manner:
- The layout should be horizontal
- The buttons should be divided into two groups with group 1
consisting of buttons 1, 2, and 3 and group 2 consisting of buttons
long-named button 4 and 5. If the window is made bigger, group 1 should
cling to the left side of the window and group 2 should cling to the
right side of the window. You do not need to use two layout managers
to accomplish this grouping--you put "something" between buttons 3 and
- Within each group there should be 20 pixels of space between each
pair of buttons.
If you have questions about how your layout should look or behave, you
can run my executable:
java -jar BoxLayoutDemo.jar
- Rewrite the code in your SliderApp application from homework 7 so that
it fits the MVC model. If you had trouble getting your SliderApp application
to work, then use my solution files. If you are unsure of how to split
up the code, look at my sample MVC application that I covered in the videotape lecture. Here
are the additional requirements for this problem:
If you have questions about how your application should look or behave,
then execute the SliderApp.jar file in /home/bvz/cs365/hw/hw8.
A sample invocation might be:
- Put all your files in a package named SliderApp, including the
"Glue" file that will contain your main method.
- Create a third view called GaugeView that draws a custom
picture of a gauge with a needle. The view should allow the
user to drag the needle to change the value of the model. The gauge view
has the following requirements:
- it should consist of an arc that extends from 3:00 to 9:00 in a
counter clockwise direction and a line (i.e., the needle) that extends
from the center of the arc to the boundary of the arc.
The arc can be drawn using the Graphic object's fillArc command.
- the arc must be orange and the line must be blue.
- the user can drag the needle around the arc by pressing on the
mouse button while the mouse cursor is closer than
10 pixels from the endpoint of the needle and dragging the mouse cursor.
The mouse cursor may move anywhere while the cursor is being dragged--it
does not need to remain within 10 pixels of the endpoint of the needle.
The needle should follow the mouse cursor around the arc and the
needle's endpoint should stay attached to the boundary of the
- Use a MouseListener
to detect the MousePressed and MouseReleased events and
the MouseMotionListener to
detect the MouseDragged event. The coordinates
of the mouse cursor can be obtained from the MouseEvent that is passed
to your event handling methods. The distance of the cursor from the
endpoint of a line is given by the equation:
sqrt((cursor.x-line.x)2 + (cursor.y-line.y)2)
- 3:00 on the arc represents the minimum value of the gauge.
Values increase in a counter-clockwise direction to 9:00 on the arc.
You can use the angle of
the needle to determine the value of the gauge. For example, if the
minimum value of the gauge is 0, the maximum value is 100, and the
angle is 90 degrees (i.e., 12:00), then the value of the gauge is 50.
- the preferred width and height of your gauge view should be 200x100.
- the arc should be horizontally centered within its JPanel and
vertically attached to the bottom of its JPanel.
- The diameter of the arc must be the minimum of the
width or 2 times the height of its JPanel. You can get the width and height of
a JPanel using the getWidth and getHeight methods. As an example, if
the layout manager assigns the JPanel a width of 450 and a height of
100, then the arc's diameter must be 200 pixels, which is 2 times the
- Place the gauge view at the bottom of your application.
java -jar SliderApp.jar 50 100 80 5
Here are a few things to which you should pay careful attention:
- The y-axis of a window goes from the top to the
bottom of the window. Hence in a window that is 200 pixels high,
a y-value of 0 occurs at the top of the window
and a y-value of 200 occurs at the bottom of the window.
- When trying to obtain an angle using the arctan function, you should
use Java's Math.atan2 function rather than its Math.atan function. The
difference is that atan2 handles the case where x is 0 (occurs at π/2
radians) whereas atan will not work if x is 0 (y/x will yield NaN and
atan will return NaN rather than π/2 radians).
- Java's fillArc command uses degrees but its Math trigonometric functions
use radians. You can convert back and forth using Math.toDegrees
What To Submit
Submit two jar files named BoxDemoLayout.jar and SliderApp.jar. Both
should be executable and contain the source files for their problems.