func.java

来自「java语言实现的mp3播放与心电图的模拟程序」· Java 代码 · 共 56 行

JAVA
56
字号
package mp3_test2;

import java.awt.*;
import java.util.*;
import javax.swing.*; 

public class Func extends JPanel implements Runnable{ 

	int move=5;
	public Thread thread;
 Func(){
	 this.setPreferredSize(new Dimension(200,300));
   show();
   thread=new Thread(this);
   thread.start();
 }
double f(double x) {  
     double a= (4*Math.sin(5*x)-2); 
     if(a>=0)
    	 return a;
     else return 0;

 }
public void run(){
	while(true){
		try{
			repaint();
			move+=10;
			thread.sleep(500); 
		}catch (InterruptedException ex){ex.printStackTrace();} 
	} 
}
 public void paint(Graphics g) { 
		g.clearRect(0,0,getSize().width,getSize().height); 
      double  x0,x1,y0,y1,xd,yd; 
      double max=5.0; 
      int w,h,hw,hh; 
      w=getSize().width; 
      h=getSize().height;
      hw=w/2; hh=h/2;
      g.setColor(Color.red);  
      g.drawLine(0,hh,w,hh);
      xd=2*max/w;
      yd=hh/max; 

      g.setColor(Color.blue);
      for (int x=0 ; x<w-1; x++) { 
    	  x0=-max+(x+move)*xd; y0=f(x0)*yd; 
          x1=x0+xd;     y1=f(x1)*yd; 
          g.drawLine(x,(int)(hh-y0),x+1,(int)(hh-y1));
      } 
      g.setColor(Color.white);
 }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?