Write several JFrame
subclasses (customizations) in order
to practice with writing classes, methods, method calls (including the
use of arguments and parameters), and expressions. We will have taught
all the material required for this assignment by the end of week 2.
You may work with one partner.
This is (very, very roughly) a 10-hour assignment. Plan accordingly.
Plan to spend as much as an hour reading this handout so that you thoroughly understand what we are asking for. Do this with your partner before you start programming!
We strongly, strongly suggest that you and your partner alternate writing the classes. You will both benefit from this, even if you find it feels like it is taking longer.
Here are some very specific rules that you must follow. If you break any of these, you will automatically get a zero on part or all this assignment.
static
anywhere in
assignment 1. if
statements. public
,
and every instance variable must be marked private
. System.out.println
or System.out.print
in any class that you write.JFrame
You will write several subclasses of JFrame
. Here are the
sources of help that you can use, in no particular order: the course
textbooks, your lecture notes, your programming partner, the course website,
the Java APIs, the consultants in Carpenter, the TAs, and the instructor.
You should write a short javadoc comment at the top of each class that describes what the class is for. You should also write a comment for each method. As a reminder, javadoc comments look like these:
/** this is a javadoc comment for class X */
public class X {
/** this is a javadoc comment for method m. Put a blank line before me. */
public void m() {
}
}
Here are the five subclasses that you should write:
JFrame
that can double and halve its height and width. Has these additional
methods:
doubleDimensions()
: double this window's height
and width.halveDimensions()
: cut this window's height and width
in half. Hints:Write and check out one method at a time. In the Interactions
pane, initialize a JFrame
and figure out a statement
that doubles the size of that window. Then write the subclass outline,
copy the statement into the method, and change the variable name to
"this". Also, check your lecture notes.
Note: doubling and halving may stop working properly if the windows
become too large for your screen. Don't worry about this; your class
need work only when the window is of a reasonable size.
JFrame
that can
be resized in several different ways. Has these additional methods:
setSizeToLoc()
: set this window's width to this
window's X coordinate and set this window's height to this window's
Y coordinate. enlarge(int dW, int dH)
: add dW
to
this window's width, and add dH
to this window's height.
switchOrientation()
: set this window's width to
the height and the height to the width. Hints: write and test one method at a time. For example, after you
write class ResizingFrame
and method setSizeToLoc
,
try this in the Interactions pane:
ResizingFrame rw= new ResizingFrame();
rw.show();
rw.setLocation(400, 400);
rw.setSizeToLoc();
You should see the window change shape.
JFrame
that can get
its size and location from a Date
object. Has these additional
methods:
setSizeAndLocToDate(Date d)
: Set this window's width to
112 + (d
's year modulo 400), set this window's height to
d
's month times 10, set this window's X coordinate to d
's
hour times 10, set this window's Y coordinate to d
's seconds
times 5, and set this window's title to d.toString()
.
b modulo c
using b % c
. JFrame
that
can create a partner frame. Has these additional methods:
createPartner()
: create a JFrame
that is the same size as this frame and is 25 pixels to the right
and 25 pixels down from this frame. resetPartner()
: resize the partner frame so that
it is the same size as this fram, and move it so that it is again
25 pixels to the right and 25 pixels down from this frame. This
method is called only after createPartner
has been
called. Note: The first method creates a frame; the second one doesn't. It
just moves and resizes the four frames. For example, you, the user,
can drag and resize the frame; then you can call resetPartner to
fix its partner.
Hint: save the partner frame as an instance variable. Draw a picture
to work out the coordinates before you program it.
You have to show the frame in the two methods; otherwise, it won't
become visible.
JFrame
that can create
a "star" of frames around it. Has these additional methods:
createStar()
: create 4 JFrame
s that
are all half the size of this fram, and form a star: each of the
four (exactly) touches a corner of this frame. resetStar()
: move and resize the 4 frames so that
they are all half the size of this fram, and form a star: each
of the four (exactly) touches a corner of this frame. This method
is called only after createStar
has been called.Hint: save the partner framesas instance variables. Draw pictures to work out the coordinates.
j.setSize(0, 0);
, the width is 112. You don't need to fix
this, and you will not be penalized for this.
DoublingHalvingFrame.java
,ResizingFrame.java
,DateFrame.java
,DoubleVisionFrame.java
,StarFrame.java
.