Answered step by step
Verified Expert Solution
Question
1 Approved Answer
BasicGraph.java import acm.graphics.GLine; import acm.graphics.GOval; import acm.graphics.GPoint; import acm.program.GraphicsProgram; import javax.swing. * ; import java.awt.event.ActionEvent; public class BasicGraph extends GraphicsProgram { private static final int
BasicGraph.java
import acm.graphics.GLine;
import acm.graphics.GOval;
import acm.graphics.GPoint;
import acm.program.GraphicsProgram;
import javax.swing.;
import java.awt.event.ActionEvent;
public class BasicGraph extends GraphicsProgram
private static final int APPLICATIONWIDTH ;
private static final int APPLICATIONHEIGHT ;
private GPoint origin;
public void init
setSizeAPPLICATIONWIDTH, APPLICATIONHEIGHT;
JButton pointButton new JButtonDraw Points;
JButton lineButton new JButtonDraw Line";
JButton circleButton new JButtonDraw Circle";
JButton clearButton new JButtonClear;
addpointButton SOUTH;
addlineButton SOUTH;
addcircleButton SOUTH;
addclearButton SOUTH;
addActionListeners;
origin new GPointdouble APPLICATIONWIDTH double APPLICATIONHEIGHT ;
public void actionPerformedActionEvent e
String cmd egetActionCommand;
switch cmd
case "Draw Points:
drawPoints;
break;
case "Draw Line":
drawLine;
break;
case "Draw Circle":
drawCircle;
break;
case "Clear":
clearCanvas;
break;
private void drawPoints
int x readIntEnter xcoordinate: ;
int y readIntEnter ycoordinate: ;
Point p new Pointx y;
double drawX origin.getX x;
double drawY origin.getY y;
GOval point new GOvaldrawX drawY ;
point.setFilledtrue;
addpoint;
private void drawLine
int x readIntEnter xcoordinate: ;
int y readIntEnter ycoordinate: ;
int x readIntEnter xcoordinate: ;
int y readIntEnter ycoordinate: ;
double startX origin.getX x;
double startY origin.getY y;
double endX origin.getX x;
double endY origin.getY y;
MyLine line new MyLinestartX startY, endX, endY;
addline;
private void drawCircle
int centerX readIntEnter center xcoordinate: ;
int centerY readIntEnter center ycoordinate: ;
int radius readIntEnter radius: ;
double drawX origin.getX centerX radius;
double drawY origin.getY centerY radius;
GOval circle new GOvaldrawX drawY, radius, radius;
addcircle;
public void clearCanvas
removeAll;
origin new GPointdouble APPLICATIONWIDTH double APPLICATIONHEIGHT ;
addnew GLine origin.getY APPLICATIONWIDTH, origin.getY; xaxis
addnew GLineorigingetX origin.getX APPLICATIONHEIGHT; yaxis
public void run
clearCanvas;
public static void mainString args
new BasicGraphstartargs;
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 MyLinedouble x double y double x double y
superx y x y;
this.dx x x;
this.dy y y;
This method should not override the final paint method from GObject
public void drawGraphics g
GraphicsD gd GraphicsD g;
gdsetStrokenew BasicStroke;
gddrawLineGMathroundgetStartPointgetX GMath.roundgetStartPointgetY
GMath.roundgetEndPointgetX GMath.roundgetEndPointgetY;
Point.java
public class Point
private int x;
private int y;
public Pointint initX, int initY
this.x initX;
this.y initY;
public boolean equalsPoint other
return this.x other.x && this.y other.y;
@Override
public String toString
return this.x this.y ;
public double distanceFromOrigin
return Math.sqrtthisx this.x this.y this.y;
public int getX
return x;
public int getY
return y;
public void setXint x
this.x x;
public void setYint y
this.y y;
Code must have similar output in the image, where the user can select through the main menu whether to draw a points line, or circle.
The user must also be able to input x and y coordinates directly inside the graphics program. Half of the window must be like a console program and half must be the graph output which can be seen in the image.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started