您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 高中教育 > 正弦曲线JAVA代码
建立一个对话框程序绘制正弦曲线,周期、幅度可以设置完曲线幅度和周期后按更新后曲线自动更新。importjava.awt.*;importjavax.swing.*;importjavax.swing.JFrame;importjava.text.DecimalFormat;importjava.awt.event.*;classSinWindowextendsJFrame{JTextFieldinputtext1,inputtext2;JLabellabel1,label2;JButtonbutton;JPanelpanel;privatedoublecx=1,cy=1;privateDecimalFormatdf=newDecimalFormat(0.00);SinWindow(Strings){setTitle(s);setSize(800,700);setLocation(120,120);setVisible(true);panel=newJPanel();inputtext1=newJTextField(10);inputtext2=newJTextField(10);label1=newJLabel(周期);label2=newJLabel(振幅);button=newJButton(更新);panel.add(label1);panel.add(inputtext1);panel.add(label2);panel.add(inputtext2);panel.add(button);add(panel,BorderLayout.NORTH);validate();button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cx=1.0/(Double.parseDouble(inputtext1.getText()));cy=Double.parseDouble(inputtext2.getText());paint(getGraphics());}});setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}privateinttranslateX(doublex){return(int)(x*getWidth()/Math.PI/4+getWidth()/2);}privateinttranslateY(doubley){return(int)(getHeight()/2-y*getWidth()/Math.PI/4);}privatedoublesin(doublex){return(cy*Math.sin(cx*x));}publicvoidpaint(Graphicsg){super.paint(g);g.setColor(Color.BLUE);g.drawString(y=+df.format(cy)+*sin(+df.format(2*cx)+*x),50,50);g.setColor(Color.LIGHT_GRAY);g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());g.setColor(Color.BLACK);for(doublei=-Math.PI/cx;iMath.PI/cx;i+=0.1)g.drawLine(translateX(i),translateY(sin(i)),translateX(i+0.1),translateY(sin(i+0.1)));}}publicclassexample{publicstaticvoidmain(Stringargs[]){newSinWindow(正弦图);}}
本文标题:正弦曲线JAVA代码
链接地址:https://www.777doc.com/doc-6349478 .html