我正在尝试绘制平滑的贝塞尔曲线以适合我绘制的一组数据,以便在Java图形绘制中绘制它。 下面是我目前用来绘制的代码。这是绘制点很好,除了曲线有尖锐的边缘,有时有小骨折。有没有更好的方法可以使用java图形来制作平滑的拟合曲线?在Java图形中绘制平滑曲线
int numProfiles = speedList.size();
int lenOfList;
System.out.println();
System.out.println("Creating a new general path");
//BasicStroke boldStroke = new BasicStroke(.3f);
//((Graphics2D)g).setStroke(boldStroke);
for (int i=0; i<numProfiles; i++){
GeneralPath gp = new GeneralPath();
g.setColor(colors[i]);
lenOfList = speedList.get(i).length;
if (lenOfList < 3) {
double xPlotVal1 = xMarginLeft + (((speedList.get(i)[0].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal1 = yMarginTopAxisTop + (((depthList.get(i)[0].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal2 = xMarginLeft + (((speedList.get(i)[1].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal2 = yMarginTopAxisTop + (((depthList.get(i)[1].getVal() - yMin)/(yMax - yMin)) * height);
g.drawLine((int) xPlotVal1, (int) yPlotVal1, (int) xPlotVal2, (int) yPlotVal2);
} else {
System.out.println("More than 2 pts");
for (int j = 0; j < (lenOfList - 2); j++) {
double xPlotVal1 = xMarginLeft + (((speedList.get(i)[j].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal1 = yMarginTopAxisTop + (((depthList.get(i)[j].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal2 = xMarginLeft + (((speedList.get(i)[j + 1].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal2 = yMarginTopAxisTop + (((depthList.get(i)[j + 1].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal3 = xMarginLeft + (((speedList.get(i)[j + 2].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal3 = yMarginTopAxisTop + (((depthList.get(i)[j + 2].getVal() - yMin)/(yMax - yMin)) * height);
gp.moveTo(xPlotVal1, yPlotVal1);
if (j==0) gp.moveTo(xPlotVal1, yPlotVal1);
// gp.moveTo(xPlotVal1, yPlotVal1);
gp.curveTo(xPlotVal1, yPlotVal1, xPlotVal2, yPlotVal2,
xPlotVal3, yPlotVal3);
}
((Graphics2D) g).draw(gp);
}
}
这里是什么它画图:
在7:34 // 2012年6月26日,//这里是我后添加呈现提示更新的代码
// the profiles
Graphics2D g2d = (Graphics2D)g;
int numProfiles = speedList.size();
int lenOfList;
for (int i=0; i<numProfiles; i++){
GeneralPath gp = new GeneralPath();
g2d.setColor(colors[i]);
lenOfList = speedList.get(i).length;
if (lenOfList < 3) {
double xPlotVal1 = xMarginLeft + (((speedList.get(i)[0].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal1 = yMarginTopAxisTop + (((depthList.get(i)[0].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal2 = xMarginLeft + (((speedList.get(i)[1].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal2 = yMarginTopAxisTop + (((depthList.get(i)[1].getVal() - yMin)/(yMax - yMin)) * height);
g2d.drawLine((int) xPlotVal1, (int) yPlotVal1, (int) xPlotVal2, (int) yPlotVal2);
} else {
for (int j = 0; j < (lenOfList - 2); j++) {
double xPlotVal1 = xMarginLeft + (((speedList.get(i)[j].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal1 = yMarginTopAxisTop + (((depthList.get(i)[j].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal2 = xMarginLeft + (((speedList.get(i)[j + 1].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal2 = yMarginTopAxisTop + (((depthList.get(i)[j + 1].getVal() - yMin)/(yMax - yMin)) * height);
double xPlotVal3 = xMarginLeft + (((speedList.get(i)[j + 2].getVal() - xMin)/(xMax - xMin)) * width);
double yPlotVal3 = yMarginTopAxisTop + (((depthList.get(i)[j + 2].getVal() - yMin)/(yMax - yMin)) * height);
gp.moveTo(xPlotVal1, yPlotVal1);
if (j==0) gp.moveTo(xPlotVal1, yPlotVal1); //only move at the begining of the curve drawing
// gp.moveTo(xPlotVal1, yPlotVal1);
gp.curveTo(xPlotVal1, yPlotVal1, xPlotVal2, yPlotVal2,
xPlotVal3, yPlotVal3);
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.draw(gp);
}
}
这是最新的图片。我不知道为什么它使我的背景网格线消失。绘制彩色轮廓后,绘制网格线。
我该怎么做?我对Stack Overflow很新,所以我不太清楚它是如何工作的。 –