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;
}
线程的正弦函数
最新推荐文章于 2023-12-11 23:57:49 发布