import java.awt.*; import java.applet.*; import java.awt.event.*; public class M extends Applet implements ActionListener { int raskus_a=0, raskus_b=0, vahe, vana_vahe, vahe_vahe, i; TextField tf_a = new TextField(10); TextField tf_b = new TextField(10); public void init() { setLayout(new BorderLayout()); Panel p = new Panel(new BorderLayout()); p.add(tf_a, BorderLayout.WEST); p.add(tf_b, BorderLayout.EAST); add(p, BorderLayout.SOUTH); tf_a.addActionListener(this); tf_b.addActionListener(this); } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,getSize().width,getSize().height); g.setColor(Color.black); joonistaSkaala(g, 10, 10, true); joonistaSkaala(g, 180, 10, false); g.drawLine(52,80+vahe,198,80-vahe); g.drawString(""+raskus_a, 50,150+vahe); g.drawString(""+raskus_b, 185,150-vahe); g.drawLine(55, 77+vahe, 55, 135+vahe); g.drawLine(195, 77-vahe, 195, 135-vahe); } public void joonistaSkaala(Graphics o, int a, int b, boolean ab) { int t; if(ab == true) { t = 0; } else { t = 55; } o.drawLine (a+30, b+0, a+30, b+120); o.drawLine (a+20, b+20, a+40, b+20); // 50 o.drawString("-50", t+a-10, b+20); o.drawLine (a+20, b+45, a+40, b+45); // 25 o.drawString("-25", t+a-10, b+45); o.drawLine (a+20, b+70, a+40, b+70); // 0 o.drawString("0", t+a-10, b+70); o.drawLine (a+20, b+95, a+40, b+95); // -25 o.drawString("25", t+a-10, b+95); o.drawLine (a+20, b+120, a+40, b+120); // -50 o.drawString("50", t+a-10, b+120); } public void arvutaKalle() { vana_vahe = vahe; vahe = raskus_a - raskus_b; if(vahe > 50) { vahe=50; } if(vahe < -50) { vahe=-50; } } public void actionPerformed(ActionEvent e) { raskus_a = Integer.parseInt(tf_a.getText().trim()); raskus_b = Integer.parseInt(tf_b.getText().trim()); arvutaKalle(); Graphics g = getGraphics(); vahe_vahe = vahe; i=0; if(vahe != vana_vahe) { while((i > vahe_vahe-vana_vahe) || (i < vahe_vahe-vana_vahe)) { try { Thread.sleep(50); } catch(Exception ex) {} vahe = vana_vahe+i; paint(g); if((vahe_vahe-vana_vahe) > 0) { i++; } else { i--; } } } } }