//PlotPanel.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class PlotPanel extends JPanel { double [] X, Y; double a, b, c; double x1, x2, vertexX, vertexY, focusX, focusY, directrix; double xLo, xHi, yLo, yHi, tickInterval; boolean newplot; int spx, spy; boolean newMouseDrag; public void paintComponent(Graphics g) { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged( MouseEvent event) { spx = event.getX(); spy = event.getY(); newMouseDrag = true; repaint(); } } ); g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); // if (newplot) super.paintComponent(g); //otherwise add to existing grid //seems to have no effect??? i.e. always cleared int w=getWidth(); //panel size int h=getHeight(); double xWidth = xHi - xLo; //user grid size double yHeight = yHi - yLo; double xScale = w / xWidth; //scale user to panel double yScale = h / yHeight; double xOffset = xLo * xScale; double yOffset = yLo * yScale; DecimalFormat d3; d3 = new DecimalFormat ("0.000"); g.setColor(Color.WHITE); g.fillRect(0,0,w,h); g.setColor(Color.BLACK); g.drawString("Drag point on curve to see tangent", 0,20); int xAxis = (int)(h+yOffset); int yAxis = (int)(-xOffset); g.drawLine(0,xAxis,w,xAxis ); //x axis g.drawLine(yAxis,0,yAxis,h); //y axis for (double tick=tickInterval; tick<=xHi; tick+=tickInterval) { g.setColor(Color.LIGHT_GRAY); g.drawLine((int)(tick * xScale - xOffset),0,(int)(tick * xScale - xOffset),h); g.setColor(Color.BLACK); g.drawLine((int)(tick * xScale - xOffset),xAxis-3,(int)(tick * xScale - xOffset),xAxis+3); } for (double tick=-tickInterval; tick>=xLo; tick-=tickInterval) { g.setColor(Color.LIGHT_GRAY); g.drawLine((int)(tick * xScale - xOffset),0,(int)(tick * xScale - xOffset),h); g.setColor(Color.BLACK); g.drawLine((int)(tick * xScale - xOffset),xAxis-3,(int)(tick * xScale - xOffset),xAxis+3); } for (double tick=tickInterval; tick<=yHi; tick+=tickInterval) { g.setColor(Color.LIGHT_GRAY); g.drawLine(0,h-(int)(tick * yScale - yOffset), w,h-(int)(tick * yScale - yOffset)); g.setColor(Color.BLACK); g.drawLine(yAxis-3,h-(int)(tick * yScale - yOffset),yAxis+3,h-(int)(tick * yScale - yOffset)); } for (double tick=-tickInterval; tick>=yLo; tick-=tickInterval) { g.setColor(Color.LIGHT_GRAY); g.drawLine(0,h-(int)(tick * yScale - yOffset), w,h-(int)(tick * yScale - yOffset)); g.setColor(Color.BLACK); g.drawLine(yAxis-3,h-(int)(tick * yScale - yOffset),yAxis+3,h-(int)(tick * yScale - yOffset)); } int x, y; //if (X != null) { //won't happen now double distanceFV = Math.abs(focusY-vertexY); g.setColor(Color.PINK); //latus rectum g.drawLine((int)(focusX * xScale - xOffset), h - (int)(focusY * yScale - yOffset), (int)((focusX-2*distanceFV) * xScale - xOffset), h - (int)(focusY * yScale - yOffset)); g.drawLine((int)(focusX * xScale - xOffset), h - (int)(focusY * yScale - yOffset), (int)((focusX+2*distanceFV) * xScale - xOffset), h - (int)(focusY * yScale - yOffset)); g.setColor(Color.RED); for (int i=0; i