6.837 Assignment 4 |
Start | Prev |
(This material can be found at
http://graphics.lcs.mit.edu/6.837/f96/problem_sets/problem_sets.html.)
Some style notes: we use typewriter font to denote class
names (for example Draw) and affix parentheses
to denote methods (for example Draw.init()).
Note that capitalization is significant, so that (for
example) the class DrawLine is distinct
from the method Graphics.drawLine.
addItem() calls in
Draw.init() to add
colors to the Choice component. Second, update the event
handling code in Draw.action() to do the right thing when
your new items are selected. Finally, add a setColor()
call to DrawCircle.mouseDown() and
DrawText.mouseDown() to make color
selection work as it does in DrawLine.mouseDown().
Font.PLAIN, Font.BOLD, Font.ITALIC)
of text. You can use a Choice or
a TextEntry for string-valued input,
and Checkboxes for selecting the style.
Add code to Draw.init() which implements these UI
components, and add code to Draw.action() to handle the
corresponding events. Also, add fields to Options to
hold the new state values in your applet. Finally, in
DrawText.mouseDown(), you'll new a new
Font(name,style,size) object, and call
setFont() on the appropriate graphics object
(of class Graphics). Note that this approach will not
support changing fonts while in the middle of typing a string; for
extra credit, implement such a capability.
DrawObject and DrawConnection
classes; however, you must fill in the following gaps in
functionality. First, modify DrawObject.sendOptions()
and DrawObject.readOptions() to handle all supported
colors, and transmit some encoding of the fill flag
options.fill. Second, modify the methods in
DrawText to send events analogous to those sent by
DrawLine and DrawCircle.
DrawSpline (analogous to, e.g., DrawLine and
DrawCircle) that subclasses DrawObject; add
a button to Draw.init() which activates it; and add code
to Draw.action() analogous to that of
e.g. DrawLine. The simplest approach is to collect the
positions of four mouse clicks, then draw the cubic Bezier spline
defined by these four control points. You can use
graphics.drawLine() to draw the line segments.
For extra credit, make the current spline editable. In
DrawSpline.mouseDown(), check whether the corresponding
position is close to that of any control point. If so, let the user
drag the control point around, while the spline is dynamically redrawn.
Note: achieving smooth regeneration of the curve without
disturbing the rest of the image will take some thought. The xor approach used
by DrawCircle won't work well, since your spline-drawing
routine is likely to draw some pixels twice (e.g., at shared endpoints
of line segments, or where the spline self-intersects). One solution
is to save the current image before any interactive spline drawing
begins. Then, whenever you update the spline, first redraw the stored
image, then draw the spline. Here is skeleton code to do this:
class DrawSpline extends DrawObject {
Image saveImg = null;
public DrawSpline(...) {
// Allocate image buffer of proper size
saveImg = parent.createImage(parent.size().width,
parent.size().height);
// Tricky part: tell Draw to update our
// image, rather than the screen image
parent.update(saveImg.getGraphics());
}
void drawTheSpline() {
// redraw what was there before
graphics.drawImage(saveImg,0,0,null);
// render spline segments using a
// series of calls to graphics.drawLine()
// ...
}
}
drawRender class writes a rectangular block
of pixels (some of which are transparent) into the
graphics to realize a filled, multicolored circle.
For extra credit, extend the drawRender class
to do something more interesting.