Question: Fix code so that the output graph is similar to the picture attached: MyLine.java import acm.graphics.GLine; import acm.graphics.GMath; import java.awt. * ; public class MyLine

Fix code so that the output graph is similar to the picture attached:
MyLine.java
import acm.graphics.GLine;
import acm.graphics.GMath;
import java.awt.*;
public class MyLine extends GLine {
private final double dx;
private final double dy;
public MyLine(double x1, double y1, double x2, double y2){
super(x1, y1, x2, y2);
this.dx = x2- x1;
this.dy = y2- y1;
}
// This method should not override the final paint method from GObject
public void draw(Graphics g){
Graphics2D g2d =(Graphics2D) g;
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(GMath.round(getStartPoint().getX()), GMath.round(getStartPoint().getY()),
GMath.round(getEndPoint().getX()), GMath.round(getEndPoint().getY()));
}
}
Point.java
public class Point {
private int x;
private int y;
public Point(int initX, int initY){
this.x = initX;
this.y = initY;
}
public boolean equals(Point other){
return this.x == other.x && this.y == other.y;
}
@Override
public String toString(){
return "("+ this.x +","+ this.y +")";
}
public double distanceFromOrigin(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int quadrant(){
if (x >0 && y >0) return 1;
if (x 0 && y >0) return 2;
if (x 0 && y 0) return 3;
if (x >0 && y 0) return 4;
return 0;
}
public static Point closestToOrigin(Point p1, Point p2){
return p1.distanceFromOrigin() p2.distanceFromOrigin()? p1 : p2;
}
}
BasicGraph2.java
import acm.graphics.GCanvas;
import acm.graphics.GLine;
import acm.graphics.GOval;
import acm.program.ConsoleProgram;
import java.awt.*;
public class BasicGraph2 extends ConsoleProgram {
private static final int APPLICATION_WIDTH =800;
private static final int APPLICATION_HEIGHT =600;
// private fields for drawing functionality
private GCanvas canvas;
private Point origin;
public void init(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
canvas = new DrawingCanvas(); // Use a custom DrawingCanvas class
add(canvas);
origin = new Point(APPLICATION_WIDTH /2, APPLICATION_HEIGHT /2);
drawAxes();
}
public void run(){
int choice =0;
do {
println("
================================");
println(" Main Menu ");
println("================================");
println("1. Draw Point(s)");
println("2. Draw Line");
println("3. Draw Circle");
println("4. Clear Drawing");
println("5. Exit");
println("================================
");
choice = readInt(">>");
switch (choice){
case 1:
drawPoints();
break;
case 2:
drawLine();
break;
case 3:
drawCircle();
break;
case 4:
clearDrawing();
break;
case 5:
println("Exiting ....");
pause(1000);
exit();
break;
default:
println("Invalid choice. Please enter a valid option.");
}
} while (choice !=5);
}
private void drawPoints(){
int numPoints = readInt("Enter the number of points to draw: ");
for (int i =0; i numPoints; i++){
int x = readInt("Enter x coordinate for point "+(i +1)+": ");
int y = readInt("Enter y coordinate for point "+(i +1)+": ");
((DrawingCanvas) canvas).drawPoint(new Point(x, y), Color.RED);
}
}
private void drawLine(){
int x0= readInt("Enter x coordinate for start point: ");
int y0= readInt("Enter y coordinate for start point: ");
int x1= readInt("Enter x coordinate for end point: ");
int y1= readInt("Enter y coordinate for end point: ");
double width = readDouble("Enter line width: ");
((DrawingCanvas) canvas).drawLine(new MyLine(x0, y0, x1, y1), width);
}
private void drawCircle(){
int x = readInt("Enter x coordinate for center: ");
int y = readInt("Enter y coordinate for center: ");
double radius = readDouble("Enter radius: ");
double width = readDouble("Enter line width: ");
((DrawingCanvas) canvas).drawCircle(new Point(x, y), radius, width);
}
 Fix code so that the output graph is similar to the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!