我正在尝试制作一个简单的程序,通过随机生成的点绘制曲线。但是出现了问题,编译器给出了一个错误。我刚开始学习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 at bezjeLiikne.Framey$2.getPoints(Framey.java:106(at bezjeLiikne.Framey$2.actionPerformed(Framey.java:98(中的异常
问题出在getPoints
方法中。创建点阵列不会创建任何点,它只会创建一个空间,用于存储对点的引用。但这些引用最初都是null
,这就是为什么你会得到NullPointerException
。
因此,在尝试分配x
和y
字段之前,需要创建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());