# How does P2D make lines look so smooth and how can this be replicated in the default renderer?

**URL:** <https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287>\
**Category:** Processing\
**Created:** [April 11, 2021, 9:20pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287 "2021-04-11T21:20:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shishironeal](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@shishironeal](https://discourse.processing.org/u/shishironeal)\
**Post date:** [April 11, 2021, 9:20pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287/1 "2021-04-11T21:20:20Z")

</div>

When I set the renderer to P2D, the lines seem to be a lot more smooth compared to the regular JAVA2D renderer. Is there a reason for this and can it be done with other renderers?

 ![p1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/33c21e180d9bdb8dab90e960242f34e63582fa88.png)  
Same code, but the quality for the P2D is significantly higher

```processing
void setup() {
  size(800, 800); 
  smooth(8);
   textSize(60);
}

PShape l;

float f(float x) {
  return 200*sin((x+3*frameCount/2000)/23f);
}

void draw() {
  
  background(0);
  translate(width/2,height/2);
  l = createShape();

  l.beginShape(LINES);
  l.stroke(0,255,0);
  l.strokeWeight(5.5);
  for (float i = -500; i < 500; i+=0.2) {
    l.vertex(i, f(i));
  }

  l.endShape();
  shape(l);

}

```

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [April 12, 2021, 2:25pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287/2 "2021-04-12T14:25:06Z")

</div>

I’ve noticed this problem with the JAVA2D renderer particularly when there’s overlapping line segments.

To get a much smoother result, increment the curve samples (`i`) by 1 rather than 0.2 so it does not produce tightly packed/overlapping segments.

```auto
void draw() {

  background(0);
  translate(width/2, height/2);
  l = createShape();

  l.beginShape();
  l.stroke(0, 255, 0);
  l.noFill();
  l.strokeWeight(5.5);
  for (float i = -500; i < 500; i+=1) {
    l.vertex(i, f(i));
  }

  l.endShape();
  shape(l);
}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ad6373db9e51481077d43633d70419c8a3dfe042.png)

_…or use the JavaFX renderer `FX2D` (which is faster too)._

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [April 12, 2021, 3:07pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287/3 "2021-04-12T15:07:06Z")

</div>

> [@micycle](#):
>
> …the JavaFX renderer `FX2D` (which is faster too).

To which? Be careful in measurement of FX2D - it uses asynchronous rendering.

---

<div class="post-metadata">

**Author:** ![shishironeal](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@shishironeal](https://discourse.processing.org/u/shishironeal)\
**Post date:** [April 12, 2021, 3:34pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287/4 "2021-04-12T15:34:33Z")

</div>

Thank you for replying. Would it be possible to use hint(DISABLE\_ASYNC\_SAVEFRAME) to force it to be synchronous?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [April 12, 2021, 4:00pm UTC](https://discourse.processing.org/t/how-does-p2d-make-lines-look-so-smooth-and-how-can-this-be-replicated-in-the-default-renderer/29287/5 "2021-04-12T16:00:18Z")

</div>

That hint isn’t controlling this - it’s to do with saving images. There’s no way to control it, it’s just how JavaFX itself works. With `P2D` and `P3D` the thread that calls `draw()` is the thread writing to the screen. In JavaFX, the animation thread buffers instructions that get rendered later by a hidden rendering thread. If the rendering thread can’t keep up, it will throw stuff away.

See eg. [jfx/GraphicsContext.java at master · openjdk/jfx · GitHub](https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/java/javafx/scene/canvas/GraphicsContext.java#L70) and [jfx/GraphicsContext.java at master · openjdk/jfx · GitHub](https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/java/javafx/scene/canvas/GraphicsContext.java#L824)

EDIT - Discourse really isn’t helpful when you link to two different lines on GitHub! 😄
