本帖最后由 hqjma 于 2012-04-05 15:49:17 编辑

解决方案 »

  1.   

    为啥要用 drawString 呢?为了能够显眼?供你参考:
    思路很简单,以deltaT为递进。
    然后 x = fnX(t); y = fnY(t);
    最后就是画线了,从 oldX,oldY -> X,Y 
    public class DrawSin extends JFrame {    private static int SCALE_X = 20;
        private static int SCALE_Y = 50;        public void paint(Graphics g) {
            super.paint(g);        
            double ox = 0, oy = 0, x = 0, y = 0, t = 0;
            int middleY = this.getHeight()/2;
            for (t = 0; t < 50; t += 0.1) {
                x = t * SCALE_X;
                y = Math.sin(t) * SCALE_Y + middleY;
                if (t > 0) {
                    g.drawLine((int) ox, (int) oy, (int) x, (int) y);
                }
                ox = x;
                oy = y;
            }
        }    public static void main(String[] args) {
            DrawSin wnd = new DrawSin();
            wnd.setSize(600, 500);
            wnd.setVisible(true);
        }
    }
      

  2.   

    这个应该用点来描吧?送给你一个用 Java 输出控制台的余弦图像:###                                             #####                                             ### 
        ##                                         ##  |  ##                                         ##    
          #                                       #    |    #                                       #      
           #                                     #     |     #                                     #       
            #                                   #      |      #                                   #        
                                                       |                                                   
             #                                 #       |       #                                 #         
              #                               #        |        #                               #          
               #                             #         |         #                             #           
                                                       |                                                   
                #                           #          |          #                           #            
                 #                         #           |           #                         #             
     --------------------------------------------------+-------------------------------------------------- 
                  #                       #            |            #                       #              
                   #                     #             |             #                     #               
                                                       |                                                   
                    #                   #              |              #                   #                
                     #                 #               |               #                 #                 
                      #               #                |                #               #                  
                                                       |                                                   
                       #             #                 |                 #             #                   
                        #           #                  |                  #           #                    
                         #         #                   |                   #         #                     
                          ##     ##                    |                    ##     ##                      
                            #####                      |                      #####                       
      

  3.   

    看看这个吧package ex15;
    import java.awt.*;
    import javax.swing.*;public class Ex15_12 extends JFrame{
    public Ex15_12(){
    add(new DrawSine());
    }
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    Ex15_12 frame=new Ex15_12();
    frame.setTitle("Ex15_12");
    frame.setSize(400,250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }}class DrawSine extends JPanel{
    double f(double x){
    return Math.sin(x);
    }

    protected void paintComponent(Graphics g){
    super.paintComponent(g);

    Polygon p=new Polygon();
    int xCenter=getWidth()/2;
    int yCenter=getHeight()/2;

    //画水平轴
    g.drawLine(50,yCenter,xCenter+150,yCenter);
    g.drawLine(xCenter+150, yCenter, xCenter+140, yCenter-10);
    g.drawLine(xCenter+150, yCenter, xCenter+140, yCenter+10);

    //画纵轴
    g.drawLine(xCenter , yCenter+75, xCenter, yCenter-75);
    g.drawLine(xCenter, yCenter-75, xCenter-10, yCenter-65);
    g.drawLine(xCenter, yCenter-75, xCenter+10, yCenter-65);

    g.drawString("-2\u03c0", xCenter-100, yCenter+10);
    g.drawString("2\u03c0", xCenter+100, yCenter+10);

    g.drawString("Y", xCenter+10, yCenter-75);
    g.drawString("X", xCenter+140, yCenter-10);

    for(int x=-100;x<=100;x++){
    p.addPoint(x+xCenter, 
    yCenter-(int)(50*f((x/100.0)*2*Math.PI)));
    }
    g.drawPolygon(p);
    }
    }
      

  4.   

    请问能不能具体分析下cos里面的参数究竟要不要乘以Math.PI/180之类的东西?网上貌似都没搜到这方面的资料。。
      

  5.   

    能不能分析下给f传参时,为什么要先(x/100.0)*2*Math.PI),*2*PI是什么原理
    我那个加上一个偏移值就可以成为标准函数曲线了,但有没有科学严谨点的方法?
      

  6.   


    我把坐标轴加上去了,没发现有任何偏移,请问你所说的偏移,具体是指什么偏移到什么了?
    public class DrawSin extends JFrame {    private static int SCALE_X = 40; // X轴缩放倍数
        private static int SCALE_Y = 100; // Y轴缩放倍数
        private static int ORIGIN_X = 50; // 原点X 
        private static int ORIGIN_Y = 0; // 原点Y 
        private static int END_ARC = 360 * 2; // 画多长    public void paint(Graphics g) {
            double ox = 0, oy = 0, x = 0, y = 0, arc = 0;
            super.paint(g);        ORIGIN_Y = this.getHeight() / 2;        // 画坐标轴
            g.drawLine(ORIGIN_X, ORIGIN_Y, this.getWidth(), ORIGIN_Y); // 横轴
            g.drawLine(ORIGIN_X, 0, ORIGIN_X, this.getHeight()); // 纵轴
            // 每90度画个标尺
            for (int i = 0; i < END_ARC; i += 90) {
                arc = Math.PI * i * 2 / 360;
                x = ORIGIN_X + arc * SCALE_X;
                g.drawLine((int) x, ORIGIN_Y - 10, (int) x, ORIGIN_Y + 10);
            }        // 画正弦曲线
            g.setColor(Color.RED);
            for (int i = 0; i < END_ARC; i += 10) {
                arc = Math.PI * i * 2 / 360;
                x = ORIGIN_X + arc * SCALE_X;
                y = ORIGIN_Y + Math.sin(arc) * SCALE_Y;
                if (arc > 0) {
                    g.drawLine((int) ox, (int) oy, (int) x, (int) y);
                }
                ox = x;
                oy = y;
            }
        }    public static void main(String[] args) {
            DrawSin wnd = new DrawSin();
            wnd.setSize(600, 500);
            wnd.setVisible(true);
        }
    }