# Sketch works with P2D but doesn't update with JAVA2D

**URL:** <https://discourse.processing.org/t/sketch-works-with-p2d-but-doesnt-update-with-java2d/8312>\
**Category:** Processing\
**Created:** [February 12, 2019, 4:02am UTC](https://discourse.processing.org/t/sketch-works-with-p2d-but-doesnt-update-with-java2d/8312 "2019-02-12T04:02:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 12, 2019, 4:02am UTC](https://discourse.processing.org/t/sketch-works-with-p2d-but-doesnt-update-with-java2d/8312/1 "2019-02-12T04:02:22Z")

</div>

Hi! Any ideas on why this program fails to draw different lines when clicking the mouse on `JAVA2D`, but does work with `P2D`? (on Processing 0269). Maybe it works on previous versions?

In `JAVA2D` it seems to draw always the same line when clicking, which might suggest that the `temp` layer is not updated. On the other hand, the `temp` layer is drawn continuously on the screen, and it is obviously changing.

```auto
PGraphics result, temp;
float len = 5, lenSpeed = 0;
float angle = 0, angleSpeed = 0;

// This program tries to demonstrate a concept for drawing.

// Instead of implementing an undo feature, a better option may be to show
// in real time how the drawing is going to look like when clicking the mouse.

// That way undo is not necessary, because you see the result in advance, interactively.
// This allows to search for the right design, instead of the "try (random result) + undo" approach.

int W = 800;
int H = 800;
String MODE = P2D; // P2D | JAVA2D

void settings() {
  size(W, H, MODE);
}

void setup() {
  result = createGraphics(W, H, MODE);  
  result.beginDraw();
  result.background(0);
  result.endDraw();

  temp = createGraphics(W, H, MODE);
  temp.beginDraw();
  temp.noFill();
  temp.stroke(255);
  temp.endDraw();
}

void draw() {
  checkKeys();

  // I think this background should not be unnecessary 
  // because pg should have an opaque background.
  background(0);
  
  updateTemp();
  image(result, 0, 0);
  image(temp, 0, 0);
}

void updateTemp() {
  len += lenSpeed;
  angle += angleSpeed;
  lenSpeed *= 0.9;
  angleSpeed *= 0.9;

  temp.beginDraw();
  temp.background(255, 0);
  float x = mouseX;
  float y = mouseY;
  temp.beginShape();
  for (int i=0; i<100; i++) {
    temp.vertex(x, y);
    float a = angle + TAU * noise(x * 0.01, y * 0.01);
    x += len * cos(a);
    y += len * sin(a);
  }
  temp.endShape();
  temp.endDraw();
}

void mousePressed() {
  // Here the transparency of pg seems to be changed,
  // even if I assume pg should keep its opaque background
  result.beginDraw();
  result.image(temp, 0, 0);
  result.endDraw();
}

void checkKeys() {
  if (keyPressed) {
    switch(keyCode) {
    case UP:
      lenSpeed += 0.003;
      break;
    case DOWN:
      lenSpeed -= 0.003;
      break;
    case LEFT:
      angleSpeed += 0.003;
      break;
    case RIGHT:
      angleSpeed -= 0.003;
      break;
    }
  }
}

```

Any suggestions on improving the antialiasing? I tried with different combinations of `clear()` and `background()` with either black or white transparency, but nothing seemed to help. Currently it looks better while the line is inside `temp`, but as soon as it is transferred to `result` the antialias is seriously injured.
