The Line Length application should draw a straight black line on a JFrame and calculate the length of the line. The line should begin at the coordinates where the mouse button is pressed and should stop at the point where the mouse button is released. The application should display the line’s length (that is, the distance between the two endpoints) in the Length= JTextField. Use the following formula to calculate the line’s length, where (x1 , y1 ) is the first endpoint (the coordinates where the mouse button is pressed) and (x2 , y2 ) is the second endpoint (the coordinates where the mouse button is released). To calculate the distance (or length) between the two points, use the following equation:
To draw a straight line, you need to use the drawLine method on a Graphics object. Use the following method call to draw a black line between the two points using a Graphics object reference g:
a) Copying the template to your working directory. Copy the C:Examples Tutorial21ExercisesLineLength directory to your C:SimplyJava directory.
b) Opening the template file. Open the LineLength.java file in your text editor.
c) Coding the lineLengthMousePressed method. Find the lineLengthMousePressed method, which starts at line 97. Inside the lineLengthMousePressed method, add code to clear lengthJTextField, store the x- coordinate in instance variable point1x (declared for you in the template) using the statement event.getX()and store the y- coordinate in instance variable point1y (declared for you in the tem-
plat
```
1 // LineLength.java
2 // This application allows the user to draw a line and then displays
3 // the length of the line.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.geom.*;
7 import java.text.DecimalFormat;
8 import java.util.*;
9 import javax.swing.*;
10
11 public class LineLength extends JFrame
12 {
13 // JLabel and JTextField for length of line
14 private JLabel lengthJLabel;
15 private JTextField lengthJTextField;
16
17 // variables to hold coordinates of endpoints
18 private int point1x;
19 private int point1y;
20 private int point2x;
21 private int point2y;
22
23 DecimalFormat value = new DecimalFormat( "0.00" );
24
25 // no-argument constructor
26 public LineLength()
27 {
28 createUserInterface();
29 }
30
31 // create and position GUI components; register event handlers
32 public void createUserInterface()
33 {
34 // get content pane for attaching GUI components
35 Container contentPane = getContentPane();
36
37 // enable explicit positioning of GUI components
38 contentPane.setLayout( null );
39
40 // set up lengthJLabel
41 lengthJLabel = new JLabel();
42 lengthJLabel.setBounds( 8, 240, 64, 24 );
43 lengthJLabel.setText( "Length =" );
44 contentPane.add( lengthJLabel );
45
46 // set up lengthJTextField
47 lengthJTextField = new JTextField();
48 lengthJTextField.setBounds( 72, 240, 48, 24 );
49 lengthJTextField.setHorizontalAlignment( JTextField.CENTER );
50 lengthJTextField.setEditable( false );
51 contentPane.add( lengthJTextField );
52
53 // add mouse listener to JFrame
54 addMouseListener(
55
56 new MouseListener() // anonymous inner class
57 {
58 // event handler called when the mouse button is pressed
59 public void mousePressed( MouseEvent event )
60 {
61 lineLengthMousePressed( event );
62 }
63
64 // event handler called when the mouse button is released
65 public void mouseReleased( MouseEvent event )
66 {
67 lineLengthMouseReleased( event );
68 }
69
70 // event handler must exist to implement interface
71 public void mouseClicked( MouseEvent event )
72 {
73 }
74
75 // event handler must exist to implement interface
76 public void mouseEntered( MouseEvent event )
77 {
78 }
79
80 // event handler must exist to implement interface
81 public void mouseExited( MouseEvent event )
82 {
83 }
84
85 } // end anonymous inner class
86
87 ); // end call to addMouseListener
88
89 // set properties of application’s window
90 setTitle( "Line Length" ); // set title bar text
91 setSize( 302, 298 ); // set window size
92 setVisible( true ); // display window
93
94 } // end method createUserInterface
95
96 // mouse is pressed
97 public void lineLengthMousePressed( MouseEvent event )
98 {
99 lengthJTextField.setText( "" ); // clear lengthJTextField
100
101 // get x- and y- coordinates of mouse click
102 point1x = event.getX();
103 point1y = event.getY();
104
105 } // end method lineLengthMousePressed
106
107 //
108 public void lineLengthMouseReleased( MouseEvent event )
109 {
110 // final point
111 point2x = event.getX();
112 point2y = event.getY();
113
114 // find distance between two points
115 double distance = lineLength();
116
117 // display distance in lengthJTextField
118 lengthJTextField.setText( value.format( distance ) );
119
120 repaint(); // draw line connecting the two points
121
122 } // end method lineLengthMouseReleased
123
124 //
125 private double lineLength()
126 {
127 double xDistance = point1x - point2x; // horizontal distance
128 double yDistance = point1y - point2y; // vertical distance
129
130 return Math.sqrt( ( xDistance * xDistance )
131 yDistance ) );
132
133 } // end method lineLength
134
135 //
136 public void paint( Graphics g )
137 {
138 super.paint( g ); // call superclass's paint method
139
140 // draw each line
141 g.drawLine( point1x, point1y, point2x, point2y );
142
143 } // end method paint
144
145 //
146 public static void main( String [] args )
147 {
148 LineLength application = new LineLength();
149 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
150
151 } // end method main
152
153 } // end class LineLength
```
You might also like to view...
It is NOT recommended to use the standard formatting techniques found on the ________ tab with PivotTables
A) Home B) Insert C) Design D) Layout
To add explanatory text to a form control, you can click the ____ option in the Toolbox, as shown in the accompanying figure.
A. Input (Text) B. Label C. Image D. Paragraph
A DBMS feature known as __________ ensures that transactions are updated in an orderly manner by establishing update rules.
A. serializability B. optimization C. prioritization D. normalization
The ____ class contains methods for manipulating numbers and properties that contain static values representing some of the numeric limitations in the JavaScript language.
A. Integer B. Number C. Math D. Limits