CSCI 1320 (Principles of Computer Science I), Fall 2014:
Homework 8
- Credit:
- 10 points.
Be sure you have read (or at least skimmed) chapters 11 and 12.
Do the following programming problems. You will end up with at
least one code file per problem.
Submit your program source (and any other needed files)
by sending mail to
bmassing@cs.trinity.edu,
with each file as an attachment.
Please use a subject line that mentions the course and
the assignment (e.g., ``csci 1320 homework 8'' or ``CS1 hw8'').
You can develop your programs on any system that provides the
needed functionality, but I will test them on one of the department's
Linux machines, so you should probably make sure they work
in that environment before turning them in.
- (10 points)
Your mission for this problem is to write a Scala program that
illustrates some basic GUI and graphics functionality.
Specifically, the program
should, at user request,
draw a picture consisting of concentric ellipses
of alternating contrasting colors (as shown in the sample screen
shot in Figure 1 below).
That is, initially the main display should be blank, and when
the user requests a ``draw'' operation (using one of the options
for user input described below),
the program should draw something like the picture shown,
and when the user requests a ``clear'' it should return to blank.
The program should also provide a way for the user to exit it.
Options for user input are the following:
- A menu with ``draw'', ``clear'', and ``exit'' items.
- Buttons for ``draw'', ``clear'', and ``exit''.
- A display area that responds to ``d'', ``c'' for clear,
and ``x'' for exit.
You will get full credit for implementing one of these choices,
extra credit for each additional choice.
Figure 1:
Sample screen shot of program after ``draw'' has been selected.
|
Hints:
- The graphics sample programs show how to draw solid rectangles
using the Rectangle library class.
You can draw ellipses using the Java library class
Ellipse2D.Double, which takes similar arguments
(coordinates of top left corner, dimensions)
of type Double. To use this class you need an
import for java.awt.geom.Ellipse2D._.
Within the paint method of a Scala Panel
you have access to variables size.width and
size.height giving the panel's current width
and height, in the same units needed by the library
methods that draw and fill shapes.
You can get the effect of concentric rings by drawing
several ellipses, largest first.
- Remember that in order to change the display you need to
call repaint.
In this problem you may need to explicitly fill a
rectangle the size of the display in order to clear it.
- For some reason I'm not sure I understand, you-as-user
may need to click in the main display window before it
will accept keyboard input.
- The sample screen shot shows what I think is a reasonable
layout for a program that implements all the mentioned
methods of getting user input.
Your program doesn't have to look exactly like the
figure, as long as it's reasonably clear how to use it.
Berna Massingill
2014-12-10