package howto; import java.awt.*; public class FigLine extends java.applet.Applet implements Figure { private TextField tfx1, tfy1, tfx2, tfy2; private NewCanvas nc; public void init() { setBackground(Color.white); setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); p.add(new Label("drawLine (")); p.add(tfx1 = new TextField(2)); p.add(new Label(",")); p.add(tfy1 = new TextField(2)); p.add(new Label(",")); p.add(tfx2 = new TextField(2)); p.add(new Label(",")); p.add(tfy2 = new TextField(2)); p.add(new Label(")")); add("North", p); nc = new NewCanvas(this); add("Center", nc); p = new Panel(); p.add(new Button("Draw")); add("South", p); tfx1.setText("10"); tfy1.setText("10"); tfx2.setText("100"); tfy2.setText("100"); } private int parseTextField(TextField tf) { int n; try { n = Integer.parseInt(tf.getText()); } catch (NumberFormatException nfe) { tf.setText("0"); n = 0; } return n; } public void paintCallback(Graphics g) { int x1 = parseTextField(tfx1); int y1 = parseTextField(tfy1); int x2 = parseTextField(tfx2); int y2 = parseTextField(tfy2); g.drawLine(x1, y1, x2, y2); } public boolean action(Event e, Object o) { if (o.equals("Draw")) { nc.repaint(); return true; } return false; } public Dimension preferredSize() { return new Dimension(500, 200); } public static void main(String [] args) { Frame f = new Frame("Line"); FigLine fl = new FigLine(); fl.init(); f.add("Center", fl); f.pack(); f.show(); } }