Computer Science 225
Advanced Programming
Spring 2021, Siena College
Lecture 13: More Event-Driven Programming
Date: Friday, April 9, 2021
Agenda
- Announcements
- Lab 6: Quadtrees and More Java Graphics (in Canvas and GitHub) due as
usual 24 hours before your lab next week
- Problem Set 4
- group formation and repository creation this afternoon
- whole thing due in a week
- see the problem description and introductory videos for more
- Grading update: Exam 1 and Problem Set 3
- Discussion of the last part of Lab 6: Quadtrees and More
Java Graphics (in Canvas and GitHub), the class hierarchy
- In-class exercise: Triangles (5 points)
In the repository for this exercise 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.
Your 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