//***************************************************************************** // // Beenish Chaudry, Geetika Tewari, Elif Tosun and // Ileana Streinu // // Summer 1999, Fall 1999, Spring 2000 // //***************************************************************************** // // CanvasPoint.java // //***************************************************************************** // // called in: FramePoint // //***************************************************************************** import java.awt.*; import java.awt.event.*; import java.lang.*; import java.io.*; import java.util.*; class CanvasPoint extends Canvas implements PointSetObserver{ FramePoint bf; // parent window //**************************** // // CanvasPoint constructor // //**************************** public CanvasPoint(FramePoint bff) { super(); setSize(Constants.wFP, Constants.hFP); // setVisible(true); setBackground(Constants.backgroundP); bf = bff; addMouseListener(new HandlerMousePoint(bf)); addMouseMotionListener(new HandlerMouseMotionPoint(bf)); addKeyListener(new HandlerKeyPoint(bf)); bf.ps.registerAsObserver(this); }// end constructor CanvasPoint //**************************** // // Reset CanvasPoint // //**************************** public void Reset() { setBackground(Color.white); repaint(); }// end reset //**************************** // // paint // //**************************** public void paint(Graphics g) { int width = getSize().width; int height = getSize().height; if(bf.state.stateGrid == StateGrid.yes) { g.setColor(bf.state.grid_line_color); drawGrid(g, width, height); } if (bf.state.stateParabola == StateParabola.yes) { g.setColor(bf.state.parabola_color); drawParabola(g, width, height); } if (bf.state.stateAxes == StateAxes.yes) { g.setColor(bf.state.axes_color); drawAxes( g, width, height); } if(bf.state.stateFrameRefer == StateFrameRefer.yes) { g.setColor(bf.state.frame_of_ref_color); drawFrameOfReference(g); } if(bf.state.stateOrCoord == StateOrCoord.yes) { g.setColor(bf.state.or_coord_color); drawOriginCoord(g); } // draw the points bf.ps.Draw(g); // if in seelct with rectangle, draw rectangle if(bf.state.stateMouse == StateMouse.selectR){ int luCornerX, luCornerY, widthRect, heightRect ; widthRect = bf.p2.x - bf.p1.x; heightRect = bf.p2.y - bf.p1.y; g.setColor(Constants.selectC); if (widthRect < 0 && heightRect >= 0) // move left and down { widthRect = - widthRect; luCornerX = bf.p2.x; luCornerY = bf.p1.y; } else if (widthRect >= 0 && heightRect < 0) // move right and up { heightRect = - heightRect; luCornerX = bf.p1.x; luCornerY = bf.p2.y; } else if (widthRect < 0 && heightRect < 0) // move left and up { widthRect = - widthRect; heightRect = - heightRect; luCornerX = bf.p2.x; luCornerY = bf.p2.y; } else // (widthRect > 0 && heightRect > 0) // move right and down { luCornerX = bf.p1.x; luCornerY = bf.p1.y; } g.drawRect(luCornerX, luCornerY, widthRect, heightRect); } } //paint //************************************************* // // PointSetHasChanged (implements PointSet observer) // //************************************************* public void PointSetHasChanged(PointSet sp) { repaint(); }// end PointSetHasChanged //********************** // Draw Parabola // //********************** void drawParabola(Graphics g, int width, int height) { // g.setColor(bf.state.parabola_color); 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) { // g.setColor(bf.state.grid_line_color); 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.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.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; //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 CanvasPoint