import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.lang.Math.*; /* <applet code="ex18_3.class" width=800 height=400 > </applet> */ public class ex18_3 extends Applet implements ActionListener Panel pane=new Panel(); drawWnd dw; Label 11 = new Label("峰值"); TextField tf1 = new TextField(15); Button btn = new Button("OK"); private float a=0; public void init() pane.setLayout(new FlowLayout(FlowLayout.CENTER,25,5)); pane.add(11); pane.add(tf1); pane.add(btn); btn.addActionListener(this); add("North",pane); dw=new drawWnd(); add("South",dw);
class drawWnd extends Canvas drawWnd() setSize(300,100); setBackground(Color. GRAY);
public void paint(Graphics g) g.setColor(Color.GREEN); for(int i=0;i<getSize().width-1;i++) int y=0; int y1=y+(int) (Math.sin(i)*a); int y2=y1+(int) (a*Math.sin(i+1)); g.drawLine(i,y1,i+1,y2);
public void actionPerformed(ActionEvent ae) try a=Integer.parseInt(tf1.getText()); dw.repaint();