JAVA打印正弦图形

第一篇博客,有许多未考虑周到之处,还请指正,谢谢

package org.tarena.note.test;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 绘制正弦图f(x)=sin(x)
* @author dxkj
*
*/
public class MathTest extends JPanel {
public static final int WIDTH=1000;
public static final int HEIGHT=500;
public static int angel=5;
public static int startx=0;
public static int starty=200;
public static int count=2;
public void paint(Graphics g){

g.setColor(Color.red);
Font font=new Font(Font.SANS_SERIF, Font.BOLD, 10);
g.setFont(font);
paintCurve(g);
paintXCordination(g);
}

public void paintXCordination(Graphics g){
g.setColor(Color.white);
for(int i=0;i<startx;i++){
if(i%50==0){
g.drawLine(i, 200, i, 195);
}
}
}
public void paintCordination(Graphics g){
g.setColor(Color.green);
String str="("+startx+","+starty+")";
g.drawString(str, startx-30, starty+10);
}
public void paintCurve(Graphics g){

g.drawLine(0, 200, 1000, 200);//X轴
g.drawLine(startx, starty,angel,200-(int)(Math.sin(angel*Math.PI/100)*200));
startx=angel;
starty=200-(int)(Math.sin(angel*Math.PI/100)*200);
if(starty==0||starty==400){
paintCordination(g);//输出顶点坐标
}
angel=count*5;//x逢5的倍数画一次
count++;

}
public static void main(String[] args) {

JFrame frame=new JFrame();
final MathTest m=new MathTest();
frame.add(m);
frame.setAlwaysOnTop(true);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBackground(Color.black);
Thread t=new Thread(){
public void run(){

while(startx<=1000){

try {
m.repaint();

Thread.sleep(100);

System.out.println("x:"+startx);

} catch (InterruptedException e) {

e.printStackTrace();
}
}
}
};
t.start();


}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值