Java Reference
In-Depth Information
As a first step, type this class into your IDE and create and show an instance
of it. Then, draw it on graph paper so that you can see where its parts are.
The problem with this class is that the numbers are all hard-coded. Rewrite
the class so that it has the following fields, which are declared with initializing
declarations: (x, y) gives the origin of the rectangle that contains the face, and
r gives radius of the face. When making the change, declare the fields first and
recompile. Then, change the constants in one statement at a time, testing after
each one.
import java.awt.*;
import javax.swing.*;
public class FaceFrame extends JFrame {
* Draw a face in the rectangle whose origin is (10, 15) and whose
* width and height are both 40 .
public void paint(Graphics g) {
g.drawOval(10, 15, 40, 40);
// Draw the mouth
g.drawLine(10 + 20 - 5, 15 + 20 + 10,
10 + 20 + 5, 15 + 20 + 10);
// Draw the left and right eyes, green
g.drawOval(10 + 20 - 6, 15 + 20 - 6, 3, 3);
g.drawOval(10 + 20 + 6, 15 + 20 - 6, 3, 3);
E10 . Add to the class of the previous exercise a method moveFace( int
originX, int originY, int radius) that changes the origin of the face and
its radius. The last statement in the method body should be a call to repaint the
frame: repaint(); .
E11. In the previous exercise, look at a face with radius 60 so you can see it well.
Notice that the eyes seem to be looking to the right. Why is that? Why aren't the
eyes centered? Fix the eyes—but do not hack! That is, do not simply try many
different things, hoping that one of them will fix the eyes. Instead, draw the face
on paper, putting in the bounding boxes of all ovals that are drawn and labeling
the various parts, distances, etc. If you do this carefully, you should be able to
see the problem.
E12. Write (and test) a class that customizes JFrame with another method that
Search WWH ::

Custom Search