线程的正弦函数


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

public class SinTest extends JFrame {
double a=100;
double b=100.0;
double c=10.0;
public SinTest() {
getContentPane().setLayout(new BorderLayout());
displayPanel.setBackground(Color.gray);
drawPanel=new DrawPanel(this);
getContentPane().add(drawPanel,"Center");
pack();
}
public static void main(String args[]){
SinTest st=new SinTest();

st.setLocation(200,200);
st.setVisible(true);
st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public double getA(){
return a;
}
double f(double x,double c) {
return (Math.sin(x/a+c) * b+getSize().height / 2);

}


public String getAppletInfo() {
return "绘制正弦曲线";
}

private DrawPanel drawPanel;
private JPanel displayPanel=new JPanel();

}

class DrawPanel extends JPanel implements Runnable,MouseListener{
***************************************
double c;
Thread thread;
boolean isStopped;
public DrawPanel(SinTest owner) {
graApp=owner;
//********************************
c=0.0;
this.setPreferredSize(new Dimension(800,300));
this.addMouseListener(this);
(thread=new Thread(this)).start();
}

public void paint(Graphics g) {
graApp.setTitle("C:"+c);
g.clearRect(0,0,getSize().width,getSize().height);
g.setColor(Color.red);
for (int x = 0 ; x < getSize().width ; x++) {
g.drawLine(x, (int)(graApp.f(x,c)), x + 1, (int)(graApp.f(x + 1,c)));
}
}

public void run() {
while(true){
repaint();
if(!isStopped)
if((c+=.1)>2*Math.PI*graApp.getA())c=0;

try {
thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
isStopped=!isStopped;

}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

SinTest graApp;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值