import java.awt.*; import java.applet.*; public class CrossLine extends Applet { DrawCrossLine drawing_CrossLine; Panel InputPanel; Panel LinePanel, PointPanel; TextField TFslope, TFintcpt, TFAx, TFAy; double slope=1.0, intcpt=-3.0, Ax=2.0, Ay=2.0; public void init() { setLayout(new BorderLayout()); LinePanel = new Panel(); LinePanel.add(new Label("Line l: slope =", Label.RIGHT)); LinePanel.add(TFslope = new TextField(String.valueOf(slope),4)); LinePanel.add(new Label(" intercept =", Label.RIGHT)); LinePanel.add(TFintcpt = new TextField(String.valueOf(intcpt),4)); PointPanel = new Panel(); PointPanel.add(new Label("Point: A(x,y)=(", Label.RIGHT)); PointPanel.add(TFAx = new TextField(String.valueOf(Ax),4)); PointPanel.add(new Label(",", Label.CENTER)); PointPanel.add(TFAy = new TextField(String.valueOf(Ay),4)); PointPanel.add(new Label(")", Label.LEFT)); InputPanel = new Panel(); InputPanel.setLayout(new GridLayout(2,1)); InputPanel.add(LinePanel); InputPanel.add(PointPanel); drawing_CrossLine = new DrawCrossLine(slope, intcpt, Ax, Ay); add("Center", drawing_CrossLine); add("South", InputPanel); } public boolean action(Event ev, Object arg) { if (ev.target instanceof TextField) { slope = Float.valueOf(TFslope.getText().trim()).floatValue(); intcpt = Float.valueOf(TFintcpt.getText().trim()).floatValue(); Ax = Float.valueOf(TFAx.getText().trim()).floatValue(); Ay = Float.valueOf(TFAy.getText().trim()).floatValue(); drawing_CrossLine.redraw(slope, intcpt, Ax, Ay); return true; } else if (ev.target instanceof Button) { return true; } else return true; } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { stop(); destroy(); System.exit(0); return true; } else { return super.handleEvent(e); } } public static void main(String args[]) { PFrame f = new PFrame("A Line which crossing with right angle with line l"); CrossLine CrossLine_function = new CrossLine(); CrossLine_function.init(); CrossLine_function.start(); f.add("Center", CrossLine_function); f.resize(420, 500); f.show(); } } class PFrame extends Frame { PFrame(String title) { super(title); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { dispose(); System.exit(0); } return false; } } class DrawCrossLine extends Canvas { Font font; double slope = 0.0, intcpt = -3.0, Ax = -3.0, Ay = 2.0; Point A = new Point(0,0), B = new Point(0,0); Point O = new Point(0,0); Point U = new Point(20,20); int Height, Width; DrawCrossLine(double slope, double intcpt, double Ax, double Ay){ this.slope = slope; this.intcpt = intcpt; this.Ax = Ax; this.Ay = Ay; repaint(); } public void aw2px(Point t, int xx, int yy){ t.x = ( xx * U.x) + O.x; t.y = ( - yy * U.y) + O.y; } public void aw2px(Point t, double x, double y){ t.x = (int)( x * (double)U.x + (double)O.x ); t.y = (int)( - y * (double)U.y + (double)O.y ); } public void draw_coordinate(Graphics g) { g.setColor(Color.pink); for (int i = -10; i <= 10; i++) { aw2px(A, -10,i); aw2px(B, 10,i); g.drawLine(A.x, A.y, B.x, B.y); } for (int i = -10; i <= 10; i++) { aw2px(A, i,-10); aw2px(B, i, 10); g.drawLine(A.x, A.y, B.x, B.y); } g.setColor(Color.black); g.setFont(font); aw2px(A,0,-10); aw2px(B,0, 10); g.drawLine(A.x, A.y, B.x, B.y); aw2px(A,-10,0); aw2px(B,10,0); g.drawLine(A.x, A.y, B.x, B.y); } public void paint(Graphics g) { Height = size().height; Width = size().width; O.x = Height / 2; O.y = Width / 2; draw_coordinate(g); g.setColor(Color.red); aw2px(A, -10.0, slope * -10.0 + intcpt); aw2px(B, 10.0, slope * 10.0 + intcpt); g.drawLine(A.x, A.y, B.x, B.y); aw2px(A, Ax, Ay); g.fillOval(A.x -5, A.y -5, 10, 10); double Sslope, Sintcpt; if ( slope != 0.0 ) { Sslope = -1.0 / slope; } else { g.drawString("Invalid!! Because of division by zero", 10, Height - 14); return; } Sintcpt = Ay - Sslope * Ax; aw2px(A, -10.0, Sslope * -10.0 + Sintcpt); aw2px(B, 10.0, Sslope * 10.0 + Sintcpt); g.drawLine(A.x, A.y, B.x, B.y); g.drawString("line: y = " + Sslope + "X + " + Sintcpt, 10, Height - 14); } public void redraw(double slope, double intcpt, double Ax, double Ay){ this.slope = slope; this.intcpt = intcpt; this.Ax = Ax; this.Ay = Ay; repaint(); } }