日期:2014-05-20  浏览次数:20944 次

关于swing绘制余弦函数图形
这几天做题目,要用黄金搜索算法计算最大值,但我绘制cos函数图形时,却发现图形偏移了。
Java code

class GJpanel extends JPanel {
        private int w;
        private int h;
        public GJpanel(){

        }
        

        public void paintComponent(final Graphics g){
            w = getWidth();
            h = getHeight();
            g.setColor(Color.green);
            g.drawLine(0, 0, 0,getHeight());            
            g.setColor(Color.red);
            g.drawLine(0,h/2,w,h/2);  //x
            g.drawLine(w, h/2, w-10, h/2-10);
            g.drawLine(w, h/2, w-10, h/2+10);
            g.drawLine(w/2, 0,w/2, h); //y
            g.drawLine(w/2, 0, w/2-10, 10);
            g.drawLine(w/2, 0, w/2+10, 10);
            g.drawString("Y", w/2-20, 20);
            g.drawString("X", w-20, h/2+20);
            for(int x =0;x<w; x++){
                 int y =(int) (Math.cos (x*Math. PI/180)*h/3+h/2);
                g.drawString("·", x, y);
            }
}


图形:http://my.csdn.net/hqjma/album/detail/1105954
网上搜到的,很多都是加了偏移地址的。。请问要怎样才能绘制出正确的图形。
另外,cos()中的值到底是怎么回事啊,api文档说是角度和弧度都可以。但为什么都*Math.PI/180,就算是转换也应该是*180/Math.PI啊。。。



------解决方案--------------------
探讨
用这种方法试了,画出的结果还是一样的,就是和坐标轴有偏移。。我想画出来的是在坐标轴上标准的余弦曲线。。