import java.awt.*; import java.applet.*; public class LBP extends Applet { DrawLBP drawingLBP; Panel InputPanel; Panel PointAPanel, PointBPanel; TextField TFPointAx, TFPointAy, TFPointBx, TFPointBy; double Ax = 4.0, Ay = 0.0, Bx=-3.0, By=2.0; public void init() { setLayout(new BorderLayout()); PointAPanel = new Panel(); PointAPanel.add(new Label("Point A(x,y)=(",Label.RIGHT)); PointAPanel.add(TFPointAx = new TextField(String.valueOf(Ax),4)); PointAPanel.add(new Label(",",Label.CENTER)); PointAPanel.add(TFPointAy = new TextField(String.valueOf(Ay),4)); PointAPanel.add(new Label(")",Label.LEFT)); PointBPanel = new Panel(); PointBPanel.add(new Label("Point B(x,y)=(",Label.RIGHT)); PointBPanel.add(TFPointBx = new TextField(String.valueOf(Bx),4)); PointBPanel.add(new Label(",",Label.CENTER)); PointBPanel.add(TFPointBy = new TextField(String.valueOf(By),4)); PointBPanel.add(new Label(")",Label.LEFT)); InputPanel = new Panel(); InputPanel.setLayout(new GridLayout(2,1)); InputPanel.add(PointAPanel); InputPanel.add(PointBPanel); drawingLBP = new DrawLBP(Ax, Ay, Bx, By); add("Center", drawingLBP); add("South", InputPanel); } public boolean action(Event ev, Object arg) { if (ev.target instanceof TextField) { Ax = Float.valueOf(TFPointAx.getText().trim()).floatValue(); Ay = Float.valueOf(TFPointAy.getText().trim()).floatValue(); Bx = Float.valueOf(TFPointBx.getText().trim()).floatValue(); By = Float.valueOf(TFPointBy.getText().trim()).floatValue(); drawingLBP.redraw(Ax, Ay, Bx, By); 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("LineBetweenPoints"); LBP LBP_function = new LBP(); LBP_function.init(); LBP_function.start(); f.add("Center", LBP_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 DrawLBP extends Canvas { Font font; double prmAx = 0.0, prmAy = -3.0, prmBx = -3.0, prmBy = 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; DrawLBP(double prmAx, double prmAy, double prmBx, double prmBy) { this.prmAx = prmAx; this.prmAy = prmAy; this.prmBx = prmBx; this.prmBy = prmBy; repaint(); } public void redraw(double prmAx, double prmAy, double prmBx, double prmBy) { this.prmAx = prmAx; this.prmAy = prmAy; this.prmBx = prmBx; this.prmBy = prmBy; 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) { Height = size().height; Width = size().width; O.x = Height / 2; O.y = Width / 2; 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) { draw_coordinate(g); g.setColor(Color.red); aw2px(A, prmAx, prmAy); g.fillOval(A.x -5, A.y -5, 10, 10); aw2px(A, prmBx, prmBy); g.fillOval(A.x -5, A.y -5, 10, 10); double slope, intercept; slope = (prmAy - prmBy) / (prmAx - prmBx); intercept = prmAy - slope * prmAx; aw2px(A, -10.0, slope * -10.0 + intercept); aw2px(B, 10.0, slope * 10.0 + intercept); g.drawLine(A.x, A.y, B.x, B.y); g.setColor(Color.black); g.setFont(font); g.drawString("Y = " + slope + "X + " + intercept, 10, Height - 14); } }