import java.awt.*; import java.applet.*; public class TwoLines extends Applet { DrawTwoLines drawing_twolines; Panel InputPanel; Panel LineAPanel, LineBPanel; TextField TFslopeA, TFslopeB, TFintA, TFintB; double SlopeA=1.0, SlopeB=-3.0, IntA=-3.0, IntB=2.0; public void init() { setLayout(new BorderLayout()); LineAPanel = new Panel(); LineAPanel.add(new Label("A: Slope =", Label.RIGHT)); LineAPanel.add(TFslopeA = new TextField(String.valueOf(SlopeA),4)); LineAPanel.add(new Label(" Intercept =", Label.RIGHT)); LineAPanel.add(TFintA = new TextField(String.valueOf(IntA),4)); LineBPanel = new Panel(); LineBPanel.add(new Label("B: Slope =", Label.RIGHT)); LineBPanel.add(TFslopeB = new TextField(String.valueOf(SlopeB),4)); LineBPanel.add(new Label(" Intercept =", Label.RIGHT)); LineBPanel.add(TFintB = new TextField(String.valueOf(IntB),4)); InputPanel = new Panel(); InputPanel.setLayout(new GridLayout(2,1)); InputPanel.add(LineAPanel); InputPanel.add(LineBPanel); drawing_twolines = new DrawTwoLines(SlopeA, SlopeB, IntA, IntB); add("Center", drawing_twolines); add("South", InputPanel); } public boolean action(Event ev, Object arg) { if (ev.target instanceof TextField) { SlopeA = Float.valueOf(TFslopeA.getText().trim()).floatValue(); IntA = Float.valueOf(TFintA.getText().trim()).floatValue(); SlopeB = Float.valueOf(TFslopeB.getText().trim()).floatValue(); IntB = Float.valueOf(TFintB.getText().trim()).floatValue(); drawing_twolines.redraw(SlopeA, IntA, SlopeB, IntB); 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("Draw Two Lines and Display Cross Point"); TwoLines TwoLines_function = new TwoLines(); TwoLines_function.init(); TwoLines_function.start(); f.add("Center", TwoLines_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 DrawTwoLines extends Canvas { Font font; double slopeA = 0.0, intcptA = -3.0, slopeB = -3.0, intcptB = 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; DrawTwoLines(double slopeA, double intcptA, double slopeB, double intcptB){ this.slopeA = slopeA; this.intcptA = intcptA; this.slopeB = slopeB; this.intcptB = intcptB; 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, -10.0, slopeA * -10.0 + intcptA); aw2px(B, 10.0, slopeA * 10.0 + intcptA); g.drawLine(A.x, A.y, B.x, B.y); aw2px(A, -10.0, slopeB * -10.0 + intcptB); aw2px(B, 10.0, slopeB * 10.0 + intcptB); g.drawLine(A.x, A.y, B.x, B.y); g.setColor(Color.black); g.setFont(font); double crssX, crssY; crssX = - (intcptA - intcptB)/(slopeA - slopeB); crssY = slopeA * crssX + intcptA; aw2px(A, crssX, crssY); g.fillOval(A.x -5, A.y -5, 10, 10); g.drawString("Crossing Point = (" + crssX + "," + crssY + ")", 10, Height - 14); } public void redraw(double slopeA, double intcptA, double slopeB, double intcptB){ this.slopeA = slopeA; this.intcptA = intcptA; this.slopeB = slopeB; this.intcptB = intcptB; repaint(); } }