//***************************************************************************** // // Beenish Chaudry, Geetika Tewari, Elif Tosun and // Ileana Streinu // // Summer 1999, Fall 1999, Spring 2000 // //***************************************************************************** // // CanvasLine.java // //***************************************************************************** import java.awt.*; import java.awt.event.*; import java.lang.*; import java.io.*; import java.util.*; class CanvasLine extends Canvas implements PointSetObserver{ FramePoint bf; public CanvasLine(FramePoint bff) { super(); setSize(Constants.wFP, Constants.hFP); // setVisible(true); setBackground(Constants.backgroundL); bf = bff; bf.ps.registerAsObserver(this); }// end CanvasLine constructor //**************************** // // Reset CanvasLine // //**************************** public void Reset() { setBackground(Constants.backgroundL); repaint(); }// end reset //************************* // // paint // //************************* public void paint(Graphics g) { int width = bf.lf.getSize().width; int height = bf.lf.getSize().height; if(bf.lf.state.stateGrid == StateGrid.yes) { g.setColor(bf.lf.state.grid_line_color); drawGrid(g, width, height); } if (bf.lf.state.stateParabola == StateParabola.yes) { g.setColor(bf.lf.state.parabola_color); drawParabola(g, width, height); } if (bf.lf.state.stateAxes == StateAxes.yes) { g.setColor(bf.lf.state.axes_color); drawAxes(g, width, height); } if(bf.lf.state.stateFrameRefer == StateFrameRefer.yes) { g.setColor(bf.lf.state.frame_of_ref_color); drawFrameOfReference(g); } if(bf.lf.state.stateOrCoord == StateOrCoord.yes) { g.setColor(bf.lf.state.or_coord_color); drawOriginCoord(g); } // draw the lines, under the current duality bf.ps.DrawLines(g, bf.lf.duality); } //**************************************** // // PointSetHasChanged // //**************************************** public void PointSetHasChanged(PointSet ps){ repaint(); }// end PointSetHasChanged //********************** // Draw Parabola // //********************** void drawParabola(Graphics g, int width, int height) { int ox, oy; int i; int scale = bf.ps.unitL.d; ox = bf.ps.or.x; oy = bf.ps.or.y; for (i = 0; (i*i)/scale < height - oy ; i++) { int b = (oy - ((i*i)/scale)); g.drawLine(ox+(i*(int)(bf.ps.unitL.n/bf.ps.unitL.d)), b, ox+((i+1)*(int)(bf.ps.unitL.n/bf.ps.unitL.d)), oy - (((i+1)*(i+1))/scale)); g.drawLine(ox-(i*((int)(bf.ps.unitL.n/bf.ps.unitL.d))), b, ox-((i+1)*((int)(bf.ps.unitL.n/bf.ps.unitL.d))), bf.ps.or.y-(((i+1)*(i+1))/scale)); } } //********************* // Draw Grid // //********************* void drawGrid (Graphics g, int width, int height) { int i; int v = width/Constants.gridGap; //nr of vertical lines int h = height/Constants.gridGap; //nr of horizontal lines for (i=0; i<= v; i++) { g.drawLine((bf.ps.or.x - i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), 0, (bf.ps.or.x - i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), height); g.drawLine((bf.ps.or.x + i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), 0, (bf.ps.or.x + i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), height); } //PROBLEM: Should the gridgap be equal to the length of the frameofreference? //horizontal for (i=0; i<= h; i++) { g.drawLine(0,(bf.ps.or.y-i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), bf.lf.getSize().width, (bf.ps.or.y - i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d))); g.drawLine(0, (bf.ps.or.y + i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d)), bf.lf.getSize().width, (bf.ps.or.y + i*Constants.gridGap*((int)bf.ps.unitL.n/bf.ps.unitL.d))); } } //***************************************************** // // drawAxes // draw the axes of coordinates // //******************************************************* void drawAxes(Graphics g, int width, int height) { Point or = bf.ps.or; Rational unitL = bf.ps.unitL; boolean wOrient = bf.ps.wOrient; g.drawLine(0, or.y, width, or.y); g.drawLine(or.x, 0, or.x, height); }//end of drawAxes //***************************************************** // // drawFrameOfReference // draw the frame of reference // //******************************************************* public void drawFrameOfReference(Graphics g) { Point or = bf.ps.or; Rational unitL = bf.ps.unitL; boolean wOrient = bf.ps.wOrient; PointObj p = bf.state.point; // point is an arbitrary point, // to allow to call a method on a PointObj //vertical line if(wOrient) p.DrawThickLine(or.x, or.y, or.x, (int)(((or.y*unitL.d)-unitL.n)/unitL.d), 3, StateStyle.solid,g); else p.DrawThickLine(or.x, or.y, or.x, (int)(((or.y*unitL.d)+unitL.n)/unitL.d), 3, StateStyle.solid,g); //horizontal line p.DrawThickHorizLine(or.x, or.y, (int)(((or.x*unitL.d)+unitL.n)/unitL.d), 3, StateStyle.solid,g); }// end drawFrameOfReference //***************************************************** // // drawOriginCoord // draw coordinates of the origin // //******************************************************* public void drawOriginCoord(Graphics g) { Point or = bf.ps.or; g.drawString("(0,0)", or.x+5, or.y); }// end DrawOriginCoord }//end class CanvasLine