3

我正在尝试绘制平滑的贝塞尔曲线以适合我拥有的一组数据,以便将其绘制在 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);
            }
        }

这是它正在绘制的图片:
在此处输入图像描述

//2012 年 6 月 26 日上午 7:34 //这是我添加渲染提示后的更新代码

    // 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);
        }
    }

这是最新的图片。我不确定为什么它会让我的背景网格线消失。在绘制彩色轮廓后绘制网格线。 在此处输入图像描述

4

2 回答 2

8

您需要为 Graphics2D 对象启用抗锯齿。

像这样做

Graphics graphics = ...
Graphics2D g2d = (Graphics2D) graphics;

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

// You can also enable antialiasing for text:

g2d.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

graphics在使用对象绘制任何东西之前执行此操作。

另请参阅RenderingHints javadoc

于 2012-06-25T16:25:56.987 回答
3

与抗锯齿一起使用Graphics2D

 Graphics2D g2 = (Graphics2D)g;
 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
     RenderingHints.VALUE_ANTIALIAS_ON);
 //...
 g2.draw(gp);
于 2012-06-25T16:23:45.653 回答