Ok so I am working with the following project with the following 7 classes (files) and need help with getting the last two to work. Please explain how you implemented the code. Thank you. PLEASE DON'T...

Java programming


Ok so I am working with the following project with the following 7 classes (files) and need help with getting the last two to work. Please explain how you implemented the code. Thank you. PLEASE DON'T MODIFY THE FIRST 5 CLASSES Point.Java /** * Represents a geometric point */ public class Point { public double x; public double y; /** * Creates the point (x, y) * @param x the x compoent of the point * @param y the y compoent of the point */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Returns the a string representation of the point, (x, y) */ @Override public String toString() { return String.format("(%.2f, %.2f)", x, y); } } ApplicationWindow.java import javax.swing.*; /** * A convenience superclass for creating simple graphical applications. */ @SuppressWarnings("serial") public class ApplicationWindow extends JFrame { /** * Creates a new application window. The client provides application-specific * information. * * @param title the title of the window; appears in the window's title bar * @param x the
x
coordinate of the window's top-left corner * @param y the
y
coordinate of the window's top-left corner * @param width the window's width * @param height the window's height * @param panel the application's drawing area and application-specific * functionality */ public ApplicationWindow(String title, int x, int y, int width, int height, JComponent panel) { super(title); setLocation(x, y); setSize(width, height); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); } } GeoPoint.java import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; @SuppressWarnings("serial") public class GeoPoint extends JPanel { private static final int RADIUS = 4; public int x; public int y; public GeoPoint(int x, int y) { setBounds(x, y, RADIUS, RADIUS); } @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.BLUE); g2.fillOval(x, y, RADIUS, RADIUS); } @Override public String toString() { return "(" + x + ", " + y + ")"; } } GraphicalPanel.java // GraphicalPanel.java import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.AffineTransform; import javax.swing.*; @SuppressWarnings("serial") public class GraphicalPanel extends JPanel { protected static final int NORMAL_OFFSET = 4; protected static final int ACTIVE_OFFSET = 8; protected static final int NORMAL_SIZE = 8; protected static final int ACTIVE_SIZE = 16; protected static final int CLOSENESS = 8; protected static final Color LIGHT_GRAY = new Color(220, 220, 220); protected Point point1 = null; protected Point point2 = null; protected Point point3 = null; protected Point point4 = null; protected Point intersectionPoint = null; protected Point activePoint = null; protected boolean isDragging = false; private static final Color DARK_GREEN = new Color(0, 150, 0); MouseAdapter mouseAdapter = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (activePoint != null) { isDragging = true; repaint(); } } @Override public void mouseReleased(MouseEvent e) { int x = e.getX() - getWidth() / 2, y = getHeight() / 2 - e.getY(); if (point1 == null) { point1 = new Point(x, y); } else if (point2 == null) { point2 = new Point(x, y); } else if (point3 == null) { point3 = new Point(x, y); } else if (point4 == null) { point4 = new Point(x, y); } isDragging = false; repaint(); } @Override public void mouseMoved(MouseEvent e) { if (!isDragging) { checkActive(e.getX() - getWidth() / 2, getHeight() / 2 - e.getY()); // checkActive(e.getX(), e.getY()); } repaint(); } @Override public void mouseDragged(MouseEvent e) { // checkActive(e.getX(), e.getY()); if (isDragging && activePoint != null) { activePoint.x = e.getX() - getWidth() / 2; activePoint.y = getHeight() / 2 - e.getY(); } repaint(); } }; public GraphicalPanel() { setBackground(Color.WHITE); // setLayout(new BorderLayout()); setLayout(null); addMouseListener(mouseAdapter); addMouseMotionListener(mouseAdapter); setFocusable(true); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { super.keyTyped(e); switch (e.getKeyCode()) { case KeyEvent.VK_UP: System.out.println("You pressed the UP key"); break; case KeyEvent.VK_DOWN: System.out.println("You pressed the DOWN key"); break; case KeyEvent.VK_ESCAPE: point1 = point2 = point3 = point4 = activePoint = null; break; case KeyEvent.VK_V: if (point1 != null && point2 == null) { point2 = new Point(point1.x, getHeight() / 2); } else if (point3 != null && point4 == null) { point4 = new Point(point3.x, getHeight() / 2); } break; case KeyEvent.VK_H: if (point1 != null && point2 == null) { point2 = new Point(getWidth() / 2, point1.y); } else if (point3 != null && point4 == null) { point4 = new Point(getWidth() / 2, point3.y); } break; } repaint(); } }); } protected void checkActive(int x, int y) { if (point1 != null && near(point1, x, y)) { activePoint = point1; } else if (point2 != null && near(point2, x, y)) { activePoint = point2; } else if (point3 != null && near(point3, x, y)) { activePoint = point3; } else if (point4 != null && near(point4, x, y)) { activePoint = point4; } else { activePoint = null; } } private boolean near(Point p, double x, double y) { double dx = p.x - x, dy = p.y - y; dx = (dx > 0) ? dx : -dx; dy = (dy > 0) ? dy : -dy; return dx < closeness="" &&="" dy="">< closeness;="" }="" protected="" void="" drawpoint(graphics2d="" g,="" point="" p)="" {="" int="" x="(int)" math.round(p.x),="" y="(int)" math.round(p.y);="" g.filloval(x="" -="" normal_offset,="" y="" -="" normal_offset,="" normal_size,="" normal_size);="" if="" (p="=" activepoint)="" {="" g.drawoval(x="" -="" active_offset,="" y="" -="" active_offset,="" active_size,="" active_size);="" }="" g.scale(1,="" -1);="" g.drawstring(p.tostring(),="" x="" +="" 10,="" -y="" -="" 10);="" g.scale(1,="" -1);="" }="" protected="" void="" drawextendedline(graphics2d="" g2,="" point="" pt1,="" point="" pt2)="" {="" double="" m="Geometry.slope(pt1," pt2),="" b="Geometry.intercept(pt1," pt2),="" new_x1,="" new_y1,="" new_x2,="" new_y2;="" if="" (m="=" double.positive_infinity)="" {="" new_x1="new_x2" =="" pt1.x;="" new_y1="-getHeight()" 2;="" new_y2="getHeight()" 2;="" }="" else="" if="" (m="">= -1.0 && m <= 1.0)="" {="" point="" i1="Geometry.intersection(new" line(m,="" b),="" new="" line(double.positive_infinity,="" -getwidth()="" 2)),="" i2="Geometry.intersection(new" line(m,="" b),="" new="" line(double.positive_infinity,="" getwidth()="" 2));="" new_x1="i1.x;" new_y1="i1.y;" new_x2="i2.x;" new_y2="i2.y;" }="" else="" {="" point="" i1="Geometry.intersection(new" line(m,="" b),="" new="" line(0,="" -getheight()="" 2)),="" i2="Geometry.intersection(new" line(m,="" b),="" new="" line(0,="" getheight()="" 2));="" new_x1="i1.x;" new_y1="i1.y;" new_x2="i2.x;" new_y2="i2.y;" }="" var="" strokesaved="g2.getStroke();" g2.setstroke(new="" basicstroke(2));="" g2.drawline((int)="" math.round(new_x1),="" (int)="" math.round(new_y1),="" (int)="" math.round(new_x2),="" (int)="" math.round(new_y2));="" g2.setstroke(strokesaved);="" }="" private="" void="" drawgrid(graphics2d="" g,="" int="" interval)="" {="" int="" w="getWidth()," h="getHeight()," maxx="w" 2,="" maxy="h" 2;="" g.setcolor(light_gray);="" draw="" the="" minor="" grid="" lines="" for="" (int="" i="0;" i="">< w="" 2;="" i="" +="interval)" {="" g.drawline(-maxx,="" i,="" maxx,="" i);="" g.drawline(-maxx,="" -i,="" maxx,="" -i);="" g.drawline(i,="" -maxy,="" i,="" maxy);="" g.drawline(-i,="" -maxy,="" -i,="" maxy);="" }="" draw="" the="" axes="" g.setcolor(color.black);="" var="" strokesaved="g.getStroke();" g.setstroke(new="" basicstroke(2));="" g.drawline(-maxx,="" 0,="" maxx,="" 0);="" g.drawline(0,="" -maxy,="" 0,="" maxy);="" draw="" the="" x-axis="" arrow="" heads="" g.drawline(-maxx,="" 0,="" -maxx="" +="" 10,="" -7);="" g.drawline(-maxx,="" 0,="" -maxx="" +="" 10,="" 7);="" g.drawline(maxx,="" 0,="" maxx="" -="" 10,="" -7);="" g.drawline(maxx,="" 0,="" maxx="" -="" 10,="" 7);="" draw="" the="" y-axis="" arrow="" heads="" g.drawline(0,="" -maxy,="" -7,="" -maxy="" +="" 10);="" g.drawline(0,="" -maxy,="" 7,="" -maxy="" +="" 10);="" g.drawline(0,="" maxy,="" -7,="" maxy="" -="" 10);="" g.drawline(0,="" maxy,="" 7,="" maxy="" -="" 10);="" g.setstroke(strokesaved);="" }="" @override="" protected="" void="" paintcomponent(graphics="" g)="" {="" super.paintcomponent(g);="" graphics2d="" g2="(Graphics2D)" g;="" affinetransform="" at="g2.getTransform();" g2.translate(getwidth()="" 2,="" getheight()="" 2);="" g2.scale(1,="" -1);="" invert="" y-axis="" drawgrid(g2,="" 20);="" if="" (point1="" !="null)" {="" g.setcolor(color.blue);="" drawpoint(g2,="" point1);="" if="" (point2="" !="null)" {="" drawpoint(g2,="" point2);="" drawextendedline(g2,="" point1,="" point2);="" if="" (point3="" !="null)" {="" g.setcolor(dark_green);="" drawpoint(g2,="" point3);="" if="" (point4="" !="null)" {="" drawpoint(g2,="" point4);="" drawextendedline(g2,="" point3,="" point4);="" intersectionpoint="Geometry.intersection(new" line(geometry.slope(point1,="" point2),="" geometry.intercept(point1,="" point2)),="" new="" line(geometry.slope(point3,="" point4),="" geometry.intercept(point3,="" point4)));="" if="" (intersectionpoint="" !="null)" {="" g.setcolor(color.red);="" drawpoint(g2,="" intersectionpoint);="" }="" }="" }="" }="" }="" g2.scale(1,="" -1);="" re-invert="" the="" y-axis="" to="" print="" the="" text="" right-side="" up="" if="" (point2="" !="null)" {="" g.setcolor(color.blue);="" int="" x="-getWidth()" 2;="" g.drawstring(geometry.lineequation(point1,="" point2),="" x="" +="" 10,="" -x="" -="" 40);="" if="" (point4="" !="null)" {="" g.setcolor(dark_green);="" g.drawstring(geometry.lineequation(point3,="" point4),="" x="" +="" 10,="" -x="" -="" 20);="" }="" }="" g2.settransform(at);="" }="" }="" visualgeometry.java="" import="" javax.swing.swingutilities;="" public="" class="" visualgeometry="" {="" public="" static="" void="" main(string[]="" args)="" {="" swingutilities.invokelater(()="" -=""> { new ApplicationWindow("Visual Geometry", 100, 100, 600, 600, new GraphicalPanel()); }); } } Please advise on code for the following two files (classes) which need to function with the above classes in same project. Line.java /** * Represents a geometric line */ public class Line { // Add your implementation details here /** * Creates a Line object with the given slope and intercept * @param m the slope of the line * @param b the intercept of the line */ public Line(double m, double b) { // Add code here } /** * Creates a Line object that passes through the two points given * @param p1 one of the points on the line * @param p2 another point of the line */ public Line(Point p1, Point p2) { // Add code here } /** * Returns the slope of the line * @return the
Apr 02, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here