# Problem with redraw

**URL:** <https://discourse.processing.org/t/problem-with-redraw/20922>\
**Category:** Processing.py\
**Created:** [May 15, 2020, 9:11pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922 "2020-05-15T21:11:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bunal](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@bunal](https://discourse.processing.org/u/bunal)\
**Post date:** [May 15, 2020, 9:11pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/1 "2020-05-15T21:11:07Z")

</div>

Hello, I’m trying to create a grid solver. I’m calling redraw after I visit some cell and I want that cell to be green one by one until the goal cell. But it just makes all of the visited cells green after the function terminates. Can you help me?

My draw function is:

```auto
def draw():
    global state
    if state == SMALL:
       process_small() # I will show this part for simplification.
    elif state == LARGE:
        process_large()
    else:
        process_menu()

def process_small():
    global SMALL_GRID, W_SMALL, i
    y = 0
    for row in SMALL_GRID:
        x = 0
        for col in row:
            if col == -1:
                fill(255, 0, 0)
            elif col == -2:
                fill(0,0,0)
            elif col == 2: # Changes color to green
                fill(0,255,0)
            else:
                fill(255,255,255)
            rect(x,y,W_SMALL,W_SMALL)
            x += W_SMALL
        y += W_SMALL
    print("here " + str(i))
    i+=1
    process_algorithms()

```

My function is :

```auto
    noLoop()    
    while True:
        # Some algorithm
        for neighbor in find_neighbors(current_node, map):
            # Some code
            if not current_visited[row][column]:
                # More code
                map[row][column] = 2 # This part is supposed to change the color to green
                redraw()
                delay(100)
                
    loop()

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 15, 2020, 9:58pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/2 "2020-05-15T21:58:12Z")

</div>

This is happening because you have redraw() in a while loop…

You can read about the **while** iteration here:  
[https://processing.org/reference/while.html](https://processing.org/reference/while.html)  
To determine if while loop is appropriate for what you are trying to accomplish.

And check out redraw here:  
[https://processing.org/reference/redraw\_.html](https://processing.org/reference/redraw_.html)  
🤓

---

<div class="post-metadata">

**Author:** ![bunal](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@bunal](https://discourse.processing.org/u/bunal)\
**Post date:** [May 15, 2020, 10:29pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/3 "2020-05-15T22:29:00Z")

</div>

I checked both of them but they don’t give me an answer to my question. The code works by the way. I just simplified the code in here for readability. It makes the visited cells green after exiting the function but I want to make each cell green one by one in each iteration.

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 15, 2020, 11:55pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/4 "2020-05-15T23:55:56Z")

</div>

My apologies @bunal! I _just_ realized this is a python sketch!!  
Disregard earlier post…

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 16, 2020, 1:05am UTC](https://discourse.processing.org/t/problem-with-redraw/20922/6 "2020-05-16T01:05:48Z")

</div>

[https://py.processing.org/reference/redraw.html](https://py.processing.org/reference/redraw.html)

In structuring a program, it only makes sense to call `redraw()` within events such as `mousePressed()` . This is because `redraw()` does not run `draw()` immediately (it only sets a flag that indicates an update is needed).

---

<div class="post-metadata">

**Author:** ![bunal](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@bunal](https://discourse.processing.org/u/bunal)\
**Post date:** [May 16, 2020, 6:20am UTC](https://discourse.processing.org/t/problem-with-redraw/20922/7 "2020-05-16T06:20:40Z")

</div>

Is there no way to fill it with green one by one without using mousePressed() ? Because my grid is around 80x80 and if I do it that way I have to press mouse around 6000 times…

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 16, 2020, 7:00am UTC](https://discourse.processing.org/t/problem-with-redraw/20922/8 "2020-05-16T07:00:10Z")

</div>

Build a rapid mouse clicker device? 😉

You’ll have to restructure your program. Maybe something like this, where you can adjust the `frameRate()` to whatever suits you:

```auto
def setup():
    frameRate(5)

column = 0
        
def draw():
    global column
    process_small()
    column += 1
    # Some algorithm
    # ...

def process_small():
    circle(column*10, 10, 10)

```

Of course, this doesn’t quite match your program, but hopefully gives you some idea of how you could approach the problem.

---

<div class="post-metadata">

**Author:** ![bunal](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@bunal](https://discourse.processing.org/u/bunal)\
**Post date:** [May 16, 2020, 7:59am UTC](https://discourse.processing.org/t/problem-with-redraw/20922/9 "2020-05-16T07:59:05Z")

</div>

Hahahahah yes I think that would solve my problem but I don’t think that is a good engineering practice 🙂 If there is no way to bend the drawing rules, I can try it then. Thank you for your answers 🙂

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [May 16, 2020, 8:50pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/10 "2020-05-16T20:50:47Z")

</div>

This is a limitation/feature of the framework. Even if you are running a separate thread you are not supposed to draw to the screen, only at the end of the draw() “main loop” does the drawing occur.

Other frameworks give more control over drawing to the screen, but at the cost of much more complexity, for a friendly framework I guess this is a small limitation.

I don’t think that putting your `while body` inside draw(), or inside a function called in draw(), but guarded by some conditional, would be bad 🙂

---

<div class="post-metadata">

**Author:** ![bunal](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@bunal](https://discourse.processing.org/u/bunal)\
**Post date:** [May 16, 2020, 9:12pm UTC](https://discourse.processing.org/t/problem-with-redraw/20922/11 "2020-05-16T21:12:13Z")

</div>

Exactly 🙂 I think I can find a way to do it but I don’t know if it’s worth it or not. I started using Processing like a week ago and from documentation to coding, it’s been so much fun 🙂 I will definitely use this framework in my future projects 🙂
