# Image() flickers when changing sketch size with windowResize()

**URL:** https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952
**Category:** Coding Questions
**Created:** [September 23, 2022, 9:41pm UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952 "2022-09-23T21:41:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)
#### Post date: [September 23, 2022, 9:41pm UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952/1 "2022-09-23T21:41:28Z")

</div>

Context: I’m trying to make a demo sketch for the new window methods in Processing 4.0 (which replace the old `surface` methods).

I’m running in an issue where the image flickers at larger sizes.This happens even if I artificially reduce the frameRate, which makes me think that the resizing of the window might be at fault.

Any idea why that happens?

![elephAnt_optimized](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/6/f66fa5dc3e0e5c04a4f10bd6355ae105ecfcb29d.gif)

---

<div class="post-metadata">

### Author: ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)
#### Post date: [September 23, 2022, 9:42pm UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952/2 "2022-09-23T21:42:27Z")

</div>

This is what my code looks like:

```auto
String elephantEmoji = "🐘";
String antEmoji = "🐜";

PImage elephantImg;
PImage antImg;

int margin = 50; // margin

void setup(){
  windowResizable(true);
  windowRatio(1000,1000);
  elephantImg = loadImage("assets/elephant.png");
  antImg = loadImage("assets/ant.png");
  pixelDensity(1);
}

void draw(){
  float t = millis() * 0.001;

  int wHeight = 130+(int)(nSin(t)*600);
  int wWidth = wHeight;

  int wPosX = 200 + (int)(displayWidth/2-wWidth/2);
  int wPosY = (int)(displayHeight/2-wHeight/2);
  
  boolean isLarge = wHeight > 250;
  PImage bgImg = isLarge ? elephantImg : antImg;
  String wTitle = isLarge ? elephantEmoji : antEmoji;
  
  windowTitle(wTitle);
  windowResize(wWidth,wHeight);
  windowMove(wPosX,wPosY);

  background(255);

  image(bgImg,margin,margin,rwidth-margin*2,rheight-margin*2);
  
}

// Get a value that oscillates between 0.0 and 1.0
float nSin(float theta){
  return (sin(theta)+1.0)/2.0;
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 24, 2022, 1:01am UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952/3 "2022-09-24T01:01:45Z")

</div>

Hello @sableraph,

My exploration of this:

```auto
// windowResize Flicker
// v1.0.0
// GLV 2022-09-23

//References:
// https://forum.processing.org/one/topic/flickering-when-using-the-java-frame-resize-method#25080000001705051.html
// https://github.com/processing/processing4/blob/main/build/shared/revisions.md

PImage img1;
int sz;

void setup()
  {
  windowResizable(true);
  img1 = loadImage("frog.png");
  sz = 200;
  windowResize(sz, sz);
  delay(12);
  frameRate(60);
  }
  
void draw()
  {
  println(sz, width);
  image(img1, 20, 20, sz-2*20, sz-2*20);
  sz = 200 + frameCount%200;
  windowResize(sz, sz);
  delay(12);
  }

```

I gleaned some insight from here:

[https://forum.processing.org/one/topic/flickering-when-using-the-java-frame-resize-method#25080000001705051.html](https://forum.processing.org/one/topic/flickering-when-using-the-java-frame-resize-method#25080000001705051.html)

The delay got rid of the flicker.

`:)`

---

<div class="post-metadata">

### Author: ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)
#### Post date: [September 24, 2022, 11:37am UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952/4 "2022-09-24T11:37:59Z")

</div>

This works perfectly on my end too (macOS Monterey) 👍 Thanks for your help glv!

Note: the order of operations seems to matter:

1. Draw the image
2. Resize the window
3. Set the delay

![220924_elephAnt_noFlicker](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/c/0c013a8b7b7d83840f40006d71c97b19e414c58c.gif)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 23, 2023, 3:44pm UTC](https://discourse.processing.org/t/image-flickers-when-changing-sketch-size-with-windowresize/38952/5 "2023-02-23T15:44:18Z")

</div>

> [@sableraph](#):
>
> Note: the order of operations seems to matter:
> 
> 1. Draw the image
> 2. Resize the window
> 3. Set the delay

Absolutely!

I remember working through this and was preparing notes to add later about this.

I appreciated you adding these additional and necessary notes.

Working through a similar issue at the moment and remembered this challenge.

`:)`
