Answered step by step
Verified Expert Solution
Link Copied!

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 APPLICATION_WIDTH =800;
private static final int APPLICATION_HEIGHT =600;
private GPoint origin;
public void init(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
JButton pointButton = new JButton("Draw Point(s)");
JButton lineButton = new JButton("Draw Line");
JButton circleButton = new JButton("Draw Circle");
JButton clearButton = new JButton("Clear");
add(pointButton, SOUTH);
add(lineButton, SOUTH);
add(circleButton, SOUTH);
add(clearButton, SOUTH);
addActionListeners();
origin = new GPoint((double) APPLICATION_WIDTH /2,(double) APPLICATION_HEIGHT /2);
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
switch (cmd){
case "Draw Point(s)":
drawPoints();
break;
case "Draw Line":
drawLine();
break;
case "Draw Circle":
drawCircle();
break;
case "Clear":
clearCanvas();
break;
}
}
private void drawPoints(){
int x = readInt("Enter x-coordinate: ");
int y = readInt("Enter y-coordinate: ");
Point p = new Point(x, y);
double drawX = origin.getX()+ x;
double drawY = origin.getY()- y;
GOval point = new GOval(drawX -2.5, drawY -2.5,5,5);
point.setFilled(true);
add(point);
}
private void drawLine(){
int x1= readInt("Enter x1-coordinate: ");
int y1= readInt("Enter y1-coordinate: ");
int x2= readInt("Enter x2-coordinate: ");
int y2= readInt("Enter y2-coordinate: ");
double startX = origin.getX()+ x1;
double startY = origin.getY()- y1;
double endX = origin.getX()+ x2;
double endY = origin.getY()- y2;
MyLine line = new MyLine(startX, startY, endX, endY);
add(line);
}
private void drawCircle(){
int centerX = readInt("Enter center x-coordinate: ");
int centerY = readInt("Enter center y-coordinate: ");
int radius = readInt("Enter radius: ");
double drawX = origin.getX()+ centerX - radius;
double drawY = origin.getY()- centerY - radius;
GOval circle = new GOval(drawX, drawY, 2* radius, 2* radius);
add(circle);
}
public void clearCanvas(){
removeAll();
origin = new GPoint((double) APPLICATION_WIDTH /2,(double) APPLICATION_HEIGHT /2);
add(new GLine(0, origin.getY(), APPLICATION_WIDTH, origin.getY())); // x-axis
add(new GLine(origin.getX(),0, origin.getX(), APPLICATION_HEIGHT)); // y-axis
}
public void run(){
clearCanvas();
}
public static void main(String[] args){
new BasicGraph().start(args);
}
}
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;
}
}
Code must have similar output in the image, where the user can select through the main menu whether to draw a point/s, 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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions