title: Drawing bezier curve in Java
body: <p>I need to create a simple Java program, that draws a bezier curve pixel by pixel through any amount of points. At the moment, everything seems to be ok except that the curve always ends at x=0 y=0 coordinates.</p>
  
  <p>Screenshot 1</p>
  
  <p><a href="https://i.stack.imgur.com/3Wcli.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Wcli.png" alt="enter image description here"></a></p>
  
  <p>Screenshot 2</p>
  
  <p><a href="https://i.stack.imgur.com/rSpR3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rSpR3.png" alt="enter image description here"></a></p>
  
  <p>I need it to end at the last point. My brain is not quite working today, so I'm looking for some help.</p>
  
  <p>Here is what I have:</p>
  
  <pre class="lang-java prettyprint-override"><code>private void drawScene(){
  
      precision = Float.parseFloat(this.jTextField4.getText());
      //Clears the screen and draws X and Y lines
      g.setColor(Color.white);
      g.fillRect(0, 0, pWidth, pHeight);
  
      g.setColor(Color.gray);
      g.drawLine(0, offsetY, pWidth, offsetY);
      g.drawLine(offsetX, 0, offsetX, pHeight);
      //Drawing the points
      if(pointCount &gt; 0){
          for(int i = 0;i&lt;pointCount;i++){
              g.setColor(Color.red);
              g.drawString(String.valueOf(i+1), points[i].x + offsetX, points[i].y - 6 + offsetY);
              g.drawOval(points[i].x + offsetX, points[i].y - 6 + offsetY, 3, 3);
          }
      }
      //Drawing the curve
      if(pointCount &gt; 1){
          float t = 0;
          while(t &lt;= 1){
              g.setColor(Color.gray);
              this.besierCurvePixel(t);
              t += precision;
          }
      }
  }
  
  //Factorial
  private static int fact(int n) {
      int fact = 1;
      for (int i = 1; i &lt;= n; i++) {
          fact *= i;
      }
      return fact;
  }
  //Bernstein polynomial
  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 void besierCurvePixel(float t){
  
      double bPoly[] = new double[pointCount];
  
      for(int i = 0; i &lt; pointCount; i++){
          bPoly[i] = bernstein(t, pointCount, i+1);
      }
  
      double sumX = 0;
      double sumY = 0;
  
      for(int i = 0; i &lt; 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 + offsetX, y + offsetY, x + offsetX, y + offsetY);
  
  }
  </code></pre>
  
  <p>This is the method for adding the points (pointCount is 0 initially):</p>
  
  <pre><code>    points[pointCount] = new Point();
      points[pointCount].x = evt.getX() - this.offsetX;
      points[pointCount].y = evt.getY() - this.offsetY;
      pointCount++;
  
      this.drawScene();
  </code></pre>