画平滑样条java(那个老板栗)

时间:2013-10-03 19:45:26

标签: java swing awt jfreechart

我复制了一个用Java绘制样条曲线的程序,但它并不像我想的那样顺畅。这是程序(它实际上合并了我刚才提到的程序和我从别处复制的另一个程序)。

import java.applet.Applet;
import java.awt.BorderLayout;       // Using AWT containers and components
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;

import edu.umd.cs.piccolo.nodes.PPath;
// Using AWT events and listener interfaces
// Using Swing components and containers

/**
 *
 * @author User
 */
public class SplinePanel extends Applet{



    public JPanel createContentPane(){
        // This is where we'll put all our widgets
        // in the next tutorials!
        JPanel panel = new JPanel();

        /*

        SplineFactory s = new SplineFactory(); 

        double[] c = new double[12];
    c[0]  =   0.0;  // x0
    c[1]  =   0.0;  // y0
    c[2]  =   0.0;  // z0      

    c[3]  =   1.0;  // x1
    c[4]  =   1.0;  // y1
    c[5]  =   0.0;  // z1      

    c[6]  =   2.0;  // x2
    c[7]  =  -1.0;  // y2
    c[8]  =   0.0;  // z2      

    c[9]  =  10.0;  // x3
    c[10] =   0.0;  // y3
    c[11] =   0.0;  // z3      

    double[] spline1 = SplineFactory.createBezier (c,     20);
    double[] spline2 = SplineFactory.createCubic (c,      20);
    double[] spline3 = SplineFactory.createCatmullRom (c, 20); 

    /*

    System.out.println ("-- Bezier");
    for (int i = 0; i < spline1.length; i+=3)
      System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);

    System.out.println ("-- Cubic");
    for (int i = 0; i < spline2.length; i+=3)
      System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

    System.out.println ("-- Catmull-Rom");
    for (int i = 0; i < spline3.length; i+=3)
      System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]);

    */



        panel.setLayout(new BorderLayout());

        XYSeries series = new XYSeries("MyGraph");

        /*

        System.out.println ("-- Bezier");
        for (int i = 0; i < spline1.length; i+=3)
         System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);
        // series.add(spline1[i])

        System.out.println ("-- Cubic");
        for (int i = 0; i < spline2.length; i+=3)
         System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

        System.out.println ("-- Catmull-Rom");
        for (int i = 0; i < spline3.length; i+=3)
         System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]); 

         */

        /* 
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);
        */

        /*

        double[] x = new double[5];
        double[] y = new double[5];
        x[0] = 0;
        x[1] = 2;
        x[2] = 2;
        x[3] = 7;
        x[4] = 9;
        y[0] = 1;
        y[1] = 2;
        y[2] = 5;
        y[3] = 8;
        y[4] = 10;

        SplineInterpolator s = new SplineInterpolator();



        CubicCurve2D c = new CubicCurve2D.Double();
     // draw CubicCurve2D.Double with set coordinates
      * 
      */


        /*
     c.setCurve(x1, y1, ctrlx1,
                ctrly1, ctrlx2, ctrly2, x2, y2);


        */

        /*
        c.setCurve(x[0], x[0], x[2],
                y[2], x[3], y[3], x[4], y[4]);

        Graphics2D g2 = (Graphics2D)g;

        g2.draw(c);

        */

        /*
        List<Point2D> points = new ArrayList<Point2D>(); 
     // Collect points 
     Shape shape = new CurveCreator().getShape(points); 
     PPath p = new PPath(); 
     p.setPathTo(shape); 
     */


         series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);




        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "XY Chart",
                "x-axis",
                "y-axis",
                dataset, 
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );
        ChartPanel chartPanel = new ChartPanel(chart);

        chart.getXYPlot().setRenderer(new XYSplineRenderer());





        panel.add(chartPanel);
        //panel.setSize(800, 500);

        //content panes must be opaque



        panel.setOpaque(true); 
        return panel;  
    }

    public void paint(Graphics g) {

        Graphics2D g2 = (Graphics2D)(g);
        CubicCurve2D c = new CubicCurve2D.Double();
        // draw CubicCurve2D.Double with set coordinates

           /*
        c.setCurve(x1, y1, ctrlx1,
                   ctrly1, ctrlx2, ctrly2, x2, y2);
                   */

        double[] x = new double[5];
        double[] y = new double[5];
        x[0] = 0;
        x[1] = 2;
        x[2] = 2;
        x[3] = 7;
        x[4] = 9;
        y[0] = 1;
        y[1] = 2;
        y[2] = 5;
        y[3] = 8;
        y[4] = 10;

           c.setCurve(x[0], x[0], x[2],
                   y[2], x[3], y[3], x[4], y[4]);

           g2.scale(5.0, 5.0);
           g2.draw(c);
    }


    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");



        //Create and set up the content pane.
        SplinePanel demo = new SplinePanel();
        frame.setContentPane(demo.createContentPane());

        // The other bits and pieces that make our program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 650);
        frame.setVisible(true);
        /*
        Graphics g = demo.getGraphics(); 
        demo.paint(g);
        */
        JFrame jp1 = new JFrame();

        jp1.getContentPane().add(demo, BorderLayout.CENTER);
        jp1.setSize(new Dimension(500,500));
        jp1.setVisible(true);
        jp1.getContentPane().add(demo, BorderLayout.CENTER);
        jp1.setSize(new Dimension(500,500));
        jp1.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:4)

将XYSplineRenderer的“精度”设置为更大的值将增加平滑度。玩10,15,20,100等。

XYSplineRenderer xySplineRenderer = new XYSplineRenderer();
xySplineRenderer.setPrecision(15);

答案 1 :(得分:3)

您也可以使用此字符串:

XYSplineRenderer xySplineRenderer = new XYSplineRenderer(15);