CPTR 124 Fundamentals of Programming
In this lab you will use your Point and Line classes in a graphical application.
- Housekeeping. Create a project named Lab8.
Copy your Geometry package from your Lab7 project
into this new project.
- Teams. You should work with a partner for this lab.
Submit one lab check sheet with both names on it.
- How the program behaves.
The program listens for mouse events (specifically
mouse released events). The user presses and releases the
the mouse button at four separate locations on the screen:
- The first click specifies the first point of line one. A little box appears where the user clicks the mouse.
- The second click specifies the second point of line one.
- A small box appears where the user clicks.
- After the second click, the first line should be drawn on the screen.
- The first line should be blue.
- The line's equation is displayed near the bottom of the window in blue text.
- The third click specifies the first point of line two. (Draw the little box.)
- The fourth click specifies the second point of line two.
(Draw the little box.)
- After the fourth click, the second line should be drawn on the screen.
- The second line should be green.
- The second line's equation is displayed near a the bottom of the window (but not overlapping the first line's equation) in green text.
- After the fourth click, the point of intersection
(if any) can be drawn on the screen.
- The point of intersection should be red.
- The location of the point should be displayed as an ordered pair (x,y).
- The fifth mouse click is the same as the first click, as the process described above continues until the program is terminated. The fifth click clears the screen and then draws the little box where the mouse was clicked.
- The following figure shows how the window might look after
the user has created the lines:
- What to do. You will reuse your Geometry classes
from before and simply complete one method in a class I provide.
- You should be able to use your Geometry.Point and Geometry.Line classes as is. You may find it convenient to add a new method or so, but any changes should be minor. Please fix any errors in them that may be exposed by the new code you write for this lab.
- The VisualGeometry class is provided.
You should not modify this
class. Place it in the project's default directory.
// VisualGeometry.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import Geometry.*; public class VisualGeometry { public static void main(String[] args) { JFrame window = new JFrame("Visual Geometry"); window.getContentPane().add(new VisualGeometryPanel()); window.setLocation(100, 100); window.setSize(600, 400); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } }
- In the VisualGeometryPanel
class, you should complete
the paintComponent() method only. This class should appear
in the default package also.
// VisualGeometryPanel.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import Geometry.*; public class VisualGeometryPanel extends JPanel { /** The first point selected by the user. */ private Geometry.Point pt1; /** The second point selected by the user. */ private Geometry.Point pt2; /** The third point selected by the user. */ private Geometry.Point pt3; /** The fourth point selected by the user. */ private Geometry.Point pt4; /** The first line created by the user. */ private Line line1; /** The second line created by the user. */ private Line line2; /** The point of intersection of the two lines. */ private Geometry.Point intersection; /** Number of mouse button releases. */ private int mouseReleases; /** * Initializes the number of points, adds the mouse listener, * and gets the window ready to show. */ public VisualGeometryPanel() { // No initial points pt1 = pt2 = pt3 = pt4 = null; addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent ev) { switch ( mouseReleases++ ) { case 0: pt1 = new Geometry.Point(ev.getX(), ev.getY()); pt2 = pt3 = pt4 = null; repaint(); break; case 1: pt2 = new Geometry.Point(ev.getX(), ev.getY()); line1 = new Line(pt1, pt2); repaint(); break; case 2: pt3 = new Geometry.Point(ev.getX(), ev.getY()); repaint(); break; case 3: pt4 = new Geometry.Point(ev.getX(), ev.getY()); line2 = new Line(pt3, pt4); repaint(); mouseReleases = 0; break; } } }); setBackground(Color.white); } public void paintComponent(Graphics g) { super.paintComponent(g); /* . . . */ /* Complete this method */ /* . . . */ } }
You should complete the paintComponent() method of the VisualGeometryPanel class. If you wish, you may add a helper method that paintComponent() uses to do its work (see below). In order to implement paintComponent(), you must exercise several methods of the Graphics class:
- setColor() Sets the current painting color
- drawRect() Draws a rectangle
- drawLine() Draws a line
- drawString() Draws a piece of graphical text
For more information about how to use these methods, see the Java documentation (there is a local link on my homepage).
- Remarks:
- The feedback points are small unfilled rectangles.
- The slopes are "backwards" since the y-axis is
inverted (y = 0 at the top, y increases going
down).
- Be sure your toString() methods in
Point and Line report
only two decimal places (otherwise the equations printed
are too big!). The disadvantage of this rounding is
that some very small numbers will be rounded to zero;
for example -0.0002x + 10 becomes -0x + 10.
- Initially, just draw the line segment between the
endpoints entered by the user. Extend them to true
lines once you have everything else working.
- You may find it useful to add a yIntercept()
method to the Line class that returns the
y-intercept of the line. This allows you to
extend the line past its "endpoints."
- Here is the pseudocode for my paintComponent()
method:
public void paintComponent(Graphics g) { Let w be the width of the drawing surface Let h be the height of the drawing surface if pt1 is non-null, then Set the drawing color to blue Draw a little box centered on pt1's (x,y) coordinates if pt2 is non-null, then Draw a little box centered on pt2's (x,y) coordinates drawLine(line1, w, h, g) Write line1's equation near the bottom left of the screen if pt3 is non-null, then Set the drawing color to green Draw a little box centered on pt3's (x,y) coordinates if pt4 is non-null, then Draw a little box centered on pt4's (x,y) coordinates drawLine(line2, w, h, g) Write line2's equation near the bottom left of the screen Let intersect be the point of intersection of line1 and line2 if intersect is non-null, then Set the drawing color to red Draw a little box centered on intersect's (x,y) coordinates Write intersect's coordinates near intersect }
- My paintComponent() method uses a method
named drawLine() to draw the line (Note:
this is different from the drawLine() method in
the Graphics class!):
private void drawLine(Line line, int width, int height, Graphics g) { Let m be the slope of line Let b be the y-intercept of line (or, if the line is vertical, let b = any x value on the line) if line is vertical, then Let x1 = b Let y1 = 0 Let x2 = x1 Let y2 = height else if m < -1 or m > 1, then Let y1 = 0 Let x1 = (y1 - b) / m Let y2 = height; Let x2 = (y2 - b) / m else Let x1 = 0 Let y1 = b x2 = width Let y2 = m*x2 + b Draw the line connecting points (x1, y1) and (x2, y2) }
- I handle b above for vertical lines in my
Line constructor. If the line is vertical, I
set its slope to Double.POSITIVE_INFINITY and its
y-intercept (that really does not exist) to be the
x value of the points passed to it.
- My paintComponent() method also uses a method
named drawPoint() to draw my little boxes,
but you may not want to be that obsessive!.
- You do not need to understand inheritance or anything from Chapter 8 to do this lab. You do need to know how to do some basic algebra (most is done for you already) and be able to invoke methods on a Graphics object to draw the necessary images in the window.
- The feedback points are small unfilled rectangles.
- Coding Conventions. As always, use the standard Java
coding style conventions.
- Submission. When you believe the program works correctly:
- recheck your style to make sure it is similar to the Sun guidelines,
- let me check and review your lab with you,
- get a Lab Report (one per team),
- close your project, exit Eclipse, and log out from your workstation.