CSC 172
Homework assignment 5
Assigned 8 Apr, 1999
Due 20 Apr, 1999
An Applet with GUI Components and Fonts
Version 1
Write an applet that shows a TextField big enough for 40 characters (on
the top line); four buttons labelled "print", "println", "clear", and "quit"
(in a row below the TextField), and a TextCanvas occupying the rest of
the window. "TextCanvas" is not a predefined class; it's a class
you need to define, as a subclass of Canvas. For this version, you only
need to get it to show up on the screen, which requires that you write
a constructor for it containing the line
setBackground (Color.white);
For the moment, it doesn't need to do anything else. The buttons, likewise,
don't need to do anything yet except show up on the screen.
At right is my solution.
Hint: If you start working on this and don't see
a white rectangle occupying the bottom
part of your window, it's probably because your TextCanvas has no size.
If it's added to the North, South, or Center part of a BorderLayout, it'll
automatically be stretched horizontally to the full width of the window.
If it's added to the East, West, or Center part, it'll automatically be
stretched vertically to whatever space is left between the North and South
parts. So if you want it to be stretched in both directions, you need to
figure out how to make it the Center part of a BorderLayout.
Version 2
Add a public "paint()" method to your TextCanvas class that draws several
strings in several different places.
Version 3
Add three public methods named "print", "println", and "clear" to your
TextCanvas class. "print" and "println" should each take a String parameter
and print it within the TypingCanvas. The catch is, the TypingCanvas
needs to remember where each string ended, so it can start the
next one just to the right of it (or, if it was printed using "println",
at the left-hand margin on the next line).
To test this, add the following lines (or something similar) to the
"paint()" method you wrote in Version 2.
print ("Hello ");
println ("there. This should be on the same line.");
println ("But this should be below it,");
print ("while this is on yet a third line.");
Note: When I first wrote this assignment, I suggested
putting these lines into the "init()" method of the Applet. That
doesn't work, because during the "init()" method, the TypingCanvas
doesn't have a Graphics context yet so it can't draw any strings in its
Graphics context. This version, however, works.
Version 4
The obvious Version 4 is to make the buttons work. To wit, when the
user presses the "print" button, whatever text is in the TextField
should be "print"ed. When the user presses the "println" button,
whatever text is in the TextField should be "println"ed (i.e. the
next thing to be printed will be at the left edge of the next
line). When the user presses the "clear" button, the TypingCanvas will be
cleared, and the next thing to be printed will be at the upper left hand
corner of the TypingCanvas. And when the user presses the "quit"
button, the applet ends.
Since I've only posted version 4 on April 15,
it's obviously not due April 15.
Work on it as soon as you've finished the previous versions.
My version should be in the right-hand margin, but it doesn't seem to
run; I'm still trying to figure out why.
Last modified:
Thu Apr 15 10:16:02 EDT 1999
Stephen Bloch / sbloch@adelphi.edu