博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

画函数图像

Posted on 2012-08-08 14:37  紫冰龙  阅读(477)  评论(0编辑  收藏  举报
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DrawDemo extends JPanel{

    /*public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g = this.getGraphics();
        g.setColor(Color.BLUE);
        g.drawString("HELLO", 100, 100);
        g.fill3DRect(50, 100, 100, 100, true);
    }*/
    public void paintComponent(Graphics g) {
        double x0,y0,x1,y1,x2,y2,scale;
        x0 = 100;y0 = 80;
        scale = 30.0;
        g.setColor(Color.BLUE);
        for(x1=-3.1415926d;x1<=3.1415926d;x1+=0.01d) {
            y1 = Math.sin(x1)+Math.cos(x1);
            x2 = x0+x1*scale;
            y2 = y0+y1*scale;
            System.out.println(""+x2+"-"+y2);
            g.fillOval((int)x2,(int)y2,3,3);
        }
        
    }
    
    
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(500,500);
        f.setTitle("Draw Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(new DrawDemo());
        f.setVisible(true);
    }
    
    
}