Computer Science 225
Advanced Programming
Spring 2022, Siena College
Lecture 11: Event-Driven Programming Practice
Date: Friday, March 11, 2022
Agenda
- Announcements
- Grading updates
- Lab 6: Quadtrees and More Java Graphics (in Canvas and GitHub) due the day
before your lab meeting next week
- Problem Set 5 group and repository deadline has passed, whole
thing is due in a week
- Discussion of the last part of Lab 6: Quadtrees and More
Java Graphics (in Canvas and GitHub), the class hierarchy
- A quick look at Hilbert SFC ordering
- In-class exercise: Triangles (5 points)
In the repository for this exercise, which you can create from the
link in Canvas under "In-Class 11", is the skeleton of a program
that creates an empty window. However, it also contains a class
Triangle that represents a triangle that can be constructed based
on its corner points, and drawn on a Graphics component using its
paint method.
Today's task is to enhance this program so it allows you to draw
triangles in the window using the following mechanism.
- When the mouse is first pressed in the window, this selects the
first corner point of the triangle to be drawn.
- Subsequent mouse moves should display a "rubber-banding" line
from that press point to the current mouse position.
- The second mouse press selects the second corner of the
triangle.
- Subsequent mouse moves should display a "rubber-banding"
triangle using the first two press points as two corners of the
triangle, and the current mouse position as the third.
- The third mouse press selects the third corner of the triangle,
which should be displayed for the remainder of the program's
execution. (So, you'd better construct a Triangle and add it
to a list, so it and all others that have been specified using this
mechanism are redrawn on each paintComponent event.)
- The next mouse press is treated the same as the first overall,
and selects the first corner point of the next triangle to be drawn,
and the remaining steps repeat. (For those who've seen the idea in
Theory of Computation: we're building a state machine!)
- A memory diagram for the triangles program
- we will draw a diagram after one triangle has been specified
and is drawn, and the next triangle has had 2 points selected so
far and the mouseMoved method is about to return