java 怎么画一个曲线?

解决方案 »

  1.   

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;public class Polylines extends Applet {
      private static Color[] colors = { 
        Color.white, Color.black, Color.blue, Color.red, 
        Color.yellow, Color.orange, Color.cyan, Color.pink, 
        Color.magenta, Color.green };        public void init() {
              Button button = new Button("repaint");
              add(button);
              button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                Polylines.this.repaint();
              }
             });
            }
            public void paint(Graphics g) {
              int arraySize = ((int)(Math.random()*100));
              int[] xPoints = new int[arraySize];
              int[] yPoints = new int[arraySize];
            
              for(int i=0; i < xPoints.length; ++i) {
                xPoints[i] = ((int)(Math.random()*200)); 
                yPoints[i] = ((int)(Math.random()*200)); 
              }
              g.setColor(colors[(int)(Math.random()*10)]);
              g.drawPolyline(xPoints, yPoints, xPoints.length);
              showStatus(arraySize + " points");
            }
      
    }
      

  2.   

    原来帮 CSDN 上一个人写的,呵呵public class Test {
        public static void main(String[] args) {
            TriFunc tri = new TriFunc();
            
            // 生成一块25×100的画布
            Canvas canvas = new Canvas(25, 120);        // 画sin曲线,周期为2
            tri.drawSin(canvas, 2.0);
            canvas.printCanvas();
            
            System.out.println();
            canvas.reset();
            // 画cos曲线,周期为2
            tri.drawCos(canvas, 2.0);
            canvas.printCanvas();
        }
    }class TriFunc {    /**
         * 画sin曲线
         * @param canvas 画布
         * @param period 曲线周期
         */
        public void drawSin(Canvas canvas, double period) {        
            char[][] chars = canvas.getCanvas();
            // x 轴的比率
            double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
            // y 轴的放大倍率
            int yMulti = (canvas.getHeight() - 1) / 2;
            for(int i = 0; i < canvas.getWidth(); i++) {
                // 将数组索引映射为横坐标值
                double k = (i - canvas.getWidth() / 2) * xRatio;
                // 将sin值映射为数组索引
                int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
                chars[h][i] = Canvas.FILL_CHAR;
            }
        }
        
        /**
         * 画cos曲线
         * @param canvas 画布
         * @param period 曲线周期
         */
        public void drawCos(Canvas canvas, double period) {
            char[][] chars = canvas.getCanvas();
            double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
            int yMulti = (canvas.getHeight() - 1) / 2;
            for(int i = 0; i < canvas.getWidth(); i++) {
                double k = (i - canvas.getWidth() / 2) * xRatio;
                int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
                chars[h][i] = Canvas.FILL_CHAR;
            }
        }
    }
    class Canvas {
        
        private int height;
        private int width;
        private char[][] canvas;   
        
        // 填充字符
        public static char FILL_CHAR = '+';
        // 空白字符
        public static char BLANK_CHAR = ' ';
        
        /**
         * 构建一块画布
         * @param height
         * @param width
         */
        public Canvas(int height, int width) {
            // 由于需要画坐标轴,所以得采用奇数
            this.height = height % 2 == 0 ? height + 1 : height;
            this.width = width % 2 == 0 ? width + 1 : width;               
            init();
        }
        
        /**
         * 初始化画布
         */
        private void init() {
            this.canvas = new char[height][width];
            for(int i = 0; i < height; i++) {
                for(int j = 0; j < width; j++) {
                    canvas[i][j] = BLANK_CHAR;
                }
            }
            addAxis();
        }
        
        /**
         * 添加坐标轴
         */
        private void addAxis() {
            // 添加横坐标
            int y = height / 2;
            for(int x = 0; x < width; x++) {
                canvas[y][x] = '-';
            }
            // 添加纵坐标
            int xx = width / 2;
            for(int yy = 0; yy < height; yy++) {
                canvas[yy][xx] = '|';
            }
            // 添加原点
            canvas[y][xx] = '+';
        }
        
        /**
         * 输出画布
         */
        public void printCanvas() {
            for(int i = 0; i < height; i++) {
                for(int j = 0; j < width; j++) {
                    System.out.print(canvas[i][j]);
                }
                System.out.println();
            }
        }
        
        /**
         * 清空画布
         */
        public void reset() {
            init();
        }
        
        public int getHeight() {
            return height;
        }
        public int getWidth() {
            return width;
        }    public char[][] getCanvas() {
            return canvas;
        }    
    }
      

  3.   

    长见识了!我还不知道原来java还有这功能!
      

  4.   

    3楼的吧问题说的太专业了。尤其我这样的数学白痴根本不懂什么Sin Cos 之类的
      

  5.   

    贝塞尔曲线的算法代码:
    http://www.math.ucla.edu/~baker/java/hoefer/Beziersrc.htm我是不懂。
      

  6.   

    要在Applet中实现,而且过程很烦琐
      

  7.   

     用java 画几个函数曲线
    public class Test {
        public static void main(String[] args) {
            TriFunc tri = new TriFunc();
           
            // 生成一块25×100的画布
            Canvas canvas = new Canvas(25, 120);        // 画sin曲线,周期为2
            tri.drawSin(canvas, 2.0);
            canvas.printCanvas();
           
            System.out.println();
            canvas.reset();
            // 画cos曲线,周期为2
            tri.drawCos(canvas, 2.0);
            canvas.printCanvas();
        }
    }class TriFunc {    /**
         * 画sin曲线
         * @param canvas 画布
         * @param period 曲线周期
         */
        public void drawSin(Canvas canvas, double period) {       
            char[][] chars = canvas.getCanvas();
            // x 轴的比率
            double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
            // y 轴的放大倍率
            int yMulti = (canvas.getHeight() - 1) / 2;
            for(int i = 0; i < canvas.getWidth(); i++) {
                // 将数组索引映射为横坐标值
                double k = (i - canvas.getWidth() / 2) * xRatio;
                // 将sin值映射为数组索引
                int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
                chars[h][i] = Canvas.FILL_CHAR;
            }
        }
       
        /**
         * 画cos曲线
         * @param canvas 画布
         * @param period 曲线周期
         */
        public void drawCos(Canvas canvas, double period) {
            char[][] chars = canvas.getCanvas();
            double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
            int yMulti = (canvas.getHeight() - 1) / 2;
            for(int i = 0; i < canvas.getWidth(); i++) {
                double k = (i - canvas.getWidth() / 2) * xRatio;
                int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
                chars[h][i] = Canvas.FILL_CHAR;
            }
        }
    }
    class Canvas {
       
        private int height;
        private int width;
        private char[][] canvas;  
       
        // 填充字符
        public static char FILL_CHAR = '+';
        // 空白字符
        public static char BLANK_CHAR = ' ';
       
        /**
         * 构建一块画布
         * @param height
         * @param width
         */
        public Canvas(int height, int width) {
            // 由于需要画坐标轴,所以得采用奇数
            this.height = height % 2 == 0 ? height + 1 : height;
            this.width = width % 2 == 0 ? width + 1 : width;              
            init();
        }
       
        /**
         * 初始化画布
         */
        private void init() {
            this.canvas = new char[height][width];
            for(int i = 0; i < height; i++) {
                for(int j = 0; j < width; j++) {
                    canvas[i][j] = BLANK_CHAR;
                }
            }
            addAxis();
        }
       
        /**
         * 添加坐标轴
         */
        private void addAxis() {
            // 添加横坐标
            int y = height / 2;
            for(int x = 0; x < width; x++) {
                canvas[y][x] = '-';
            }
            // 添加纵坐标
            int xx = width / 2;
            for(int yy = 0; yy < height; yy++) {
                canvas[yy][xx] = '|';
            }
            // 添加原点
            canvas[y][xx] = '+';
        }
       
        /**
         * 输出画布
         */
        public void printCanvas() {
            for(int i = 0; i < height; i++) {
                for(int j = 0; j < width; j++) {
                    System.out.print(canvas[i][j]);
                }
                System.out.println();
            }
        }
       
        /**
         * 清空画布
         */
        public void reset() {
            init();
        }
       
        public int getHeight() {
            return height;
        }
        public int getWidth() {
            return width;
        }    public char[][] getCanvas() {
            return canvas;
        }   
    }
      

  8.   

    //获取指定的进程名
    System.Diagnostics.Process[] prProcesses =
                    System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
    //如果获取的进程名多于一个(本身也有一个嘛~)
    if (prProcesses.Length > 1)

            MessageBox.Show("已启动过了一个实例!");//这句话可以换成弹出原窗口的代码。 
            //退出 
            return; 

      

  9.   


    import java.awt.*;
    import javax.swing.*;public class MyDrawing extends JApplet
    {
    public void init()
    {
    DrawPanel p=new DrawPanel();
    setLayout(new BorderLayout());
    add(p,BorderLayout.CENTER);
    } public static void main(String[] args) {
    JFrame frame=new JFrame();
    frame.setTitle("我的曲线");
    MyDrawing dr=new MyDrawing();
    frame.add(dr);
    dr.init();
    dr.start();
    frame.setSize(350,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}class DrawPanel extends JPanel
    {
    public void paintComponent(Graphics g)
    {
    super.paintComponents(g);
    int x=getWidth()/3;
    int y=getHeight()/5;
    int x1=10,x2=30,x3=10,x4=120;//设置4个点的横坐标x
    int y1=40,y2=80,y3=120,y4=10;//设置4个点的纵坐标y
    //将四个点连起来
    g.drawLine(x1,y1,x2,y2);
    g.drawLine(x2, y2, x3, y3);
    g.drawLine(x3, y3, x4, y4); }
    }
      

  10.   

    楼主可以研究一下Arc2D这个类,是画弧线的
      

  11.   

    自己做不如用jfreechart,既方便快速,又美观。
      

  12.   

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;public class Polylines extends Applet {
      private static Color[] colors = { 
        Color.white, Color.black, Color.blue, Color.red, 
        Color.yellow, Color.orange, Color.cyan, Color.pink, 
        Color.magenta, Color.green };        public void init() {
              Button button = new Button("repaint");
              add(button);
              button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                Polylines.this.repaint();
              }
             });
            }
            public void paint(Graphics g) {
              int arraySize = ((int)(Math.random()*100));
              int[] xPoints = new int[arraySize];
              int[] yPoints = new int[arraySize];
            
              for(int i=0; i < xPoints.length; ++i) {
                xPoints[i] = ((int)(Math.random()*200)); 
                yPoints[i] = ((int)(Math.random()*200)); 
              }
              g.setColor(colors[(int)(Math.random()*10)]);
              g.drawPolyline(xPoints, yPoints, xPoints.length);
              showStatus(arraySize + " points");
            }
      
    }
      

  13.   


    这个应该才是正解。如果使用画点的方法,画sin cos还行。
    如果画tan和ctg,就会在PI/2的左右有明显的断点。
      

  14.   

    不就是graphics2D 想画什么画什么