最近由于项目的需要,要求利用JAVA的AWT或者SWING组件,开发APPLET程序,用网页的形式显示出来。但是,现成的波形显示控件并没有,需要自己开发。目前,遇到的困难是怎样编写显示坐标和波形图的代码,请各位大虾指点一二!最好能给出源码!万分感谢!!!
由于小女子能力有限,请大家多多帮忙!!这厢有礼了!
------解决方案--------------------------------------------------------
//这是一个Applet程序,画一个正弦曲线并用JSlider控制它的振幅:
//绘图
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
class SineDraw extends JPanel
{
private static final int SCALEFACTOR=200;
private int cycles;
private int points;
private double sines[];
private int pts[];
public SineDraw(){setCycles(5);}
public void setCycles(int newCycles)
{
cycles=newCycles;
points=SCALEFACTOR*cycles*2;
sines=new double[points];
for(int i=0;i<points;i++)
{
double radians=(Math.PI/SCALEFACTOR)*i;
sines[i]=Math.sin(radians);
}
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int maxWidth=getWidth();
double hstep=(double)maxWidth/(double)points;
int maxHeight=getHeight();
pts=new int[points];
for(int i=0;i<points;i++)
pts[i]=(int)(sines[i]+maxHeight/2*0.95+maxHeight/2);
g.setColor(Color.RED);
for(int i=1;i<points;i++)
{
int x1=(int)((i-1)*hstep);
int x2=(int)(i*hstep);
int y1=pts[i-1];
int y2=pts[i];
g.drawLine(x1,y1,x2,y2);
}
}
}
public class SineWave extends JApplet
{
private SineDraw sines=new SineDraw();
private JSlider adjustCycles=new JSlider(1,30,5);
public void init()
{
Container cp=getContentPane();
cp.add(sines);
adjustCycles.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
sines.setCycles(((JSlider)e.getSource()).getValue());
}
});
cp.add(BorderLayout.SOUTH,adjustCycles);
}
public static void main(String args[])
{
new SineWave();
}
}
其它的波形大同小异,查一下波形函数即可,自己看下吧
------解决方案--------------------------------------------------------