2012-06-25 105 views
3

我正在嘗試繪製平滑的貝塞爾曲線以適合我繪製的一組數據,以便在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); 
      } 
     } 

這裏是什麼它畫圖:
enter image description here

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

這是最新的圖片。我不知道爲什麼它使我的背景網格線消失。繪製彩色輪廓後,繪製網格線。 enter image description here

+0

我該怎麼做?我對Stack Overflow很新,所以我不太清楚它是如何工作的。 –

回答

6

您需要啓用antaliasing的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

+0

嗯。我嘗試了渲染提示代碼,它仍然繪製相同的圖片。 –

+1

發佈更新後的代碼,或許您的提示太遲,或錯誤的對象。併發布您的Java版本。 – npe

+0

好的,只需在第一次嘗試下面的代碼下發布更新後的版本。 –

2

使用Graphics2D與antialising:

Graphics2D g2 = (Graphics2D)g; 
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    RenderingHints.VALUE_ANTIALIAS_ON); 
//... 
g2.draw(gp);