![amselah4955](/avatars/19998.jpg)
amselah4955
06.10.2019 •
Computers and Technology
Write a class called line that represents a line segment between two points. your line objects should have the following methods: public line(point p1, point p2) constructs a new line that contains the given two points. public point getp1() returns this line's first endpoint. public point getp2() returns this line's second endpoint. public string tostring() returns a string representation of this line, such as "[(22, 3), (4, 7)]". write a complete program to check your work. submit line, and client class .java files.
Solved
Show answers
More tips
- H Health and Medicine Which Water Are You Drinking? Is it Worth Buying Bottled Water?...
- A Animals and plants Уход за джунгариками: полезные советы и рекомендации...
- L Legal consultation What Documents Are Required for a Russian Passport?...
- S Sport How to Choose Tennis Rackets?...
- H Health and Medicine AKDS Vaccination: Ensure Your Child s Safety...
- H Health and Medicine Naskol ko Opasen Ukus Kleshcha i Kak Ego Raspoznat...
- C Computers and Internet How to Delete Your Account on Odnoklassniki...
- H Health and Medicine What to Do When Your Jaw Locks Up?...
- G Goods and services What Are the Most Popular Services?...
Answers on questions: Computers and Technology
- C Computers and Technology The customer s endpoint device on the wan is called the...
- M Mathematics 4. find mq a.) 76 b.) 104 c.) 66 d.) 114...
- M Mathematics Correct answer only factor x^2 + 2x - 15. a) (x - 5)(x + 3) b) (x - 5)(x - 3) c) (x + 5)(x + 3) d) (x + 5)(x - 3)...
- E English During his dream last night herbie imagined he was able to fly and jumped off of the roof, flying around his home town. he awoke in his bed, and clearly had not moved all night....
- M Mathematics Which algebraic rule describes the 270° counter-clockwise rotation about the origin? a) (x, y) → (−x, y) b) (x, y) → (x, −y) c) (x, y) → (y, −x) d) (x, y) → (−y, x)...
- H History What did marco polo die from? 20 points tell me the answer and not what you think he died from....
- B Biology Jaden’s mass is 50 kilograms. his weight on earth is —— newtons, and his weight on mars is—— newtons...
- M Mathematics The number of cubic feet of water in a curved container can be approximated by v = 0.95h2.9. find the amount of water in the container when h = 8 feet. round to the nearest...
- H History There was much hope that the new government would deliver political reform and social justice. however, the reality being more war, violence, and a crack down on people who...
- B Biology Annika has a friend named pedro how are similar are annikas and pedro dna...
Ответ:
We can draw lines by using Java Swing. Below is the full example.
Explanation:
Explanation of Below code is in comment, line starts with //.
import java.awt.*;
import javax.swing.*;
//Creating Point class which will hold point data
class Point {
public int x;
public int y;
}
//MyCanvas is the class used to draw the line
class MyCanvas extends JComponent {
//Declaring 2 points variable
Point p1;
Point p2;
// Creating Getter and Setter for above 2 variables
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
//When canvas load, it will call this method and draw a line
public void paint(Graphics g)
{
// draw and display the line
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
//toString method to show the points data
@Override
public String toString() {
return "[(" +
p1.x+","+p1.y+")," +
"("+p2.x+","+p2.y+")]";
}
}
//This class will start the program and initialize canvas class
//to draw new line on canvas
public class DrawLine {
public static void main(String[] a)
{
// creating object of JFrame
JFrame window = new JFrame();
// Operation when window closes
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setting size of window
window.setBounds(30, 30, 200, 200);
// Creating instance of MyCanvas class
MyCanvas canvas = new MyCanvas();
// Adding two points in MyCanvas object with its Setter method
Point p1 = new Point();
p1.x=10;
p1.y=10;
Point p2 = new Point();
p2.x=40;
p2.y=40;
canvas.setP1(p1);
canvas.setP2(p2);
// setting canvas to draw new line
window.getContentPane().add(canvas);
// visibility to true
window.setVisible(true);
//calling toString method to show two point values
System.out.println(canvas.toString());
}
}
Ответ:
nice
Explanation: