Java App UI Animation: How to synchronize the paint function inside a Displayable?


I’m trying to display a small animation on the screen and I’m having trouble to synchronize the paint with the drawing on board.

My first attempt was to wait with a Thread sleep:

while (true) {

All was working fine. Then I try to remove the sleep and then I notice that my animation was missing some images.

If my understanding is correct this is because the draw is actually asynchronous.

But how can I do a wait until draw is done from my Displayable object ?

I tried to add in my paint function:


But instead of synchronization I get the following exception:

Exception in thread “DISPLpmp” java.lang.RuntimeException: MicroUI:E=1
at java.lang.System.getStackTrace(Unknown Source)
at java.lang.Throwable.fillInStackTrace(
at java.lang.Throwable.(
at java.lang.Exception.(
at java.lang.RuntimeException.(
at ej.microui.display.Display.waitForEvent(
at com.mycompany.Main.paint(
at ej.microui.display.Displayable.paintDisplayable(
at ej.microui.display.Display.executeEventRepaint(
at ej.microui.display.DisplayPump.execute(
at java.lang.Thread.runWrapper(

Can you help me please ?

Hi @jshaw,

If my understanding is correct this is because the draw is actually asynchronous.

Yes the repaint function is asynchronous, it will add few events in the pump.

But instead of synchronization I get the following exception MicroUI:E=1:
This error is :
Deadlock. Cannot wait for an event in the same thread that runs events.Display.waitForEvent() must not be called in the display pump thread (e.g. inpaint methods).

So if you really want to use the waitForEvent, you could use:

while (true) {

But this thread should not be in the display pump.

Another way to do would be to trigger the update and repaint of the image from the render function (for instance with a wait and notify).

Yet another way would be to use an explicit flush to avoid all the display pump.

public static void main(String[] args) {
	Display display = Display.getDefaultDisplay();
	ExplicitFlush g = display.getNewExplicitFlush();

	int color = Colors.BLACK;
	int x = 0;
	int y = 0;
	int size = 100;

	while (true) {
		// Modify model.

		// Draw.
		g.fillRect(x, y, size, size);



Thank you Pierre,

The ExplicitFlush was what I was looking for.

Concerning the first part of your response I’m not sure to understand it all.
Perhaps you can provide me a link to some documentation.

Thank you.

You can find the documentation for the error codes on the device developer guide.