Big Java / Java Concepts Lab 5

Frame Windows

1.

Complete the missing lines in the following code:

import javax.swing.____;

public class TestFrameViewer
{
    public static void main(String[] args)
    {
       ____ frame = new ____();

       final int FRAME_WIDTH = 250;
       final int FRAME_HEIGHT = 250;

       frame.____(FRAME_WIDTH, FRAME_HEIGHT);
       frame.setTitle("A Test Frame");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.____(true);
    }
}

Lines

2.

Create a component (class TriangleComponent extends JComponent) that uses three Line2D.Double objects to draw a triangle that looks like this:


What is the code for your TriangleComponent class?


3.

To show the component that draws a triangle you need a viewer class. Write a viewer class that shows the component that you created in the previous problem.

Rectangles and Lines

4.

Write a class HouseComponent class whose paintComponent method uses Rectangle and Line2D.Double objects to draw a house like this one:

What is the code for your HouseComponent class?

Colors

5.

Generate five circles with the following diameteres, 40, 80, 120, 160, and 200, all tangent at a common point.


Draw each circle in a different color.

What is the code for your CirclesComponent class?

Getting Input from an Option Pane

6.

Write a program NameViewer that prompts for the user's name and draws the name inside a rectangle. The drawing will occur inside a BoxedStringComponent class. 


7.

Now implement the BoxedStringComponent class.

Comparing Visual and Numerical Information

8.

Write a program that draws three lines, as in the following figure.

When the program starts, it should ask the user for a value v, then it draws the line joining the origin (0,0) with the point (v, 200).

Line2D.Double line1 = new Line2D.Double(0, 0, v, 200);

Note that the equation of this line is

v · y = 200 x

The second (horizontal) line has the equation

x = 100

You can generate it by using the following:

Line2D.Double line2 = new Line2D.Double(100, 0, 100, getWidth());

The third (vertical) line has the equation

y = 100

Mark the intersection points with small circles and print their coordinate values. To compute the intersection points, you need to solve two sets of equations. This set gives you the first intersection point:

v · y = 200 x

x = 100

This set gives you the second intersection point:

v · y = 200 x

y = 100

Tip: Start with the IntersectionComponent.java program from the textbook. The code for drawing and labeling the intersection points is helpful. You will need to change the equations.

What is the code of your modified IntersectionComponent class?


9.

Run the program with a value of v = 160. What intersection points does your program produce?


10.

Verify the computation by calculating the values. Show your work here.

Drawing Complex Shapes

11.

Suppose you need to write a graphic application that will draw several cars and a couple of houses. Which classes would you define in order to solve this problem in an efficient and object-oriented manner?


12.

The book already provides a Car class. Write a House class that draws a house in a particular position. The top left corner of the bounding rectangle of the house should be given to the constructor. You can build up from the code you developed in problem 4.


13.

Write a component class that draws two cars and two houses, using the classes Car and House.

What is the code for your component class?