java - Java 中的贝塞尔曲线与点

标签 java graphics drawing bezier

我正在尝试制作一个简单的程序,通过随机生成的点绘制一条曲线。但是出了问题,编译器给出了错误。我刚刚开始学习 java,这是首要任务之一。希望您能指出我的错误。这是代码

public class Framey {

private static int fact(int n) {
    int fact = 1;
    for(int i = 1; i <= n;i++) {
        fact *= i;
    }
    return fact;
}

private static double bernstein(float t,int n,int i) {
    return (fact(n)/(fact(i)*fact(n-i)))*Math.pow(1-t, n-i)*Math.pow(t, i);
}


private JFrame frame;



/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Framey window = new Framey();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Framey() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBackground(Color.LIGHT_GRAY);
    panel.setBounds(23, 35, 250, 200);
    frame.getContentPane().add(panel);


    JSpinner spinner = new JSpinner();
    spinner.setBounds(291, 113, 30, 20);
    frame.getContentPane().add(spinner);

    JLabel warning = new JLabel("Value cannot be larger than 8!");
    warning.setFont(new Font("Tahoma", Font.BOLD, 9));
    warning.setBounds(283, 144, 141, 53);
    frame.getContentPane().add(warning);
    warning.setVisible(false);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int pointCount;

            pointCount = (int) spinner.getValue();
            if (pointCount>8) {
                warning.setVisible(true);
            }else {warning.setVisible(false);}




            Point[] points = getPoints(pointCount); 

             drawScene(points);
        }
        private  Point[] getPoints(int pointCount) {
            Random val = new Random();
            Point[] points = new Point[pointCount];
            for(int i=0;i<pointCount;i++) {
                points[i].x = val.nextInt(panel.getWidth());
                points[i].y =val.nextInt(panel.getHeight());
            }
            return points;
        }
        private void drawScene(Point[] points) {
            int pWidth = panel.getWidth();
            int pHeight = panel.getHeight();
            Graphics g = panel.getGraphics();
            int pointCount = points.length;
            g.setColor(Color.WHITE);
            g.fillRect(0,0, pWidth, pHeight);

            if(pointCount>1) {
                float t = 0;
                while( t<=1) {
                    g.setColor(Color.DARK_GRAY);
                    besierCurve(t, points);
                    t+=0.001;
                }
            }
        };


        private void besierCurve(float t , Point[] points) {

            Graphics g = panel.getGraphics();
            int pointCount = points.length; 
            double bPoly[] = new double[pointCount];
            for(int i =0;i<pointCount;i++) {
                bPoly[i] = bernstein(t,pointCount-1,i+1);
            }

            double sumX = 0;
            double sumY = 0;
            for(int i=0;i<pointCount;i++) {
                sumX += bPoly[i] * points[i].x;
                sumY += bPoly[i] * points[i].y;
            }

            int x , y;
            x=(int) Math.round(sumX);
            y=(int) Math.round(sumY);
            g.drawLine(x, y, x, y);

        }






    });

线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在 bezjeLiikne.Framey$2.getPoints(Framey.java:106) 在 bezjeLiikne.Framey$2.actionPerformed(Framey.java:98)

最佳答案

问题出在您的 getPoints 方法中。创建点数组不会创建任何点,它只是创建可以存储点引用的空间。但这些引用最初都是 null,这就是为什么您会收到 NullPointerException

因此,您需要在尝试分配 xy 字段之前创建 Point 实例。

更改:

points[i].x = val.nextInt(panel.getWidth());
points[i].y =val.nextInt(panel.getHeight());

致:

points[i] = new Point(val.nextInt(panel.getWidth()), val.nextInt(panel.getHeight());

关于java - Java 中的贝塞尔曲线与点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930047/

相关文章:

java - 在 Java 列表中存储不同类型的元素

c# - .net onpaint 垂直同步

c# - 非多行左对齐选项卡控件

java - 是什么导致 AWT-EventQueue-0 线程中的 NullPointerException

c# - 在没有表格的情况下在屏幕上绘图

java - Java 中的动态绑定(bind)

Java正则表达式匹配优先级模式

java - JFreeChart CategoryPlot 覆盖类别

c# - 如何更改可选面板的边框颜色?

wpf - OpenTK、SharpGL 和 WPF