1

it seems to be very stupid question, but I really need a help, I'm making a task with drawing a background transparent gradient image and then draw some objects over it, the problem is I want to draw this image once but the other objects will be drawn multi time to perform some animation

the code is as following, this is the code that i want to run once, and I have created a boolen variable = false and then set it to true

public void drawLockLayer(Graphics g) {
        try {
            lock = Image.createImage(Paths.lock);
            g.drawImage(lock, 0, 0, LGBMainMidlet.width, LGBMainMidlet.height);
            System.out.println("After Draw Image");
            drawOnce = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

other code is as following

public void paint(Graphics g, Rectangle rect) {

        synchronized (g) {
            if (drawOnce == false) {
                drawLockLayer(g);
            }
        }
        pos = (int) ((System.currentTimeMillis() % 2700) / 300);

        int i = 0;

        g.setColor(bgColor);
        g.fillRoundRect(startX, startY, width, height, 20, 20);


        g.setColor(fgColor);
        g.setFont(font);
        g.drawString(loadMsg, startX + (spacing / 4), startY + (spacing / 4));

        for (int y = 0; y < 3; y++) {
            for (int x = 0; x < 3; x++) {
                int thickness = 6;
                if (i == pos) {
                    thickness = 14;
                } else if (i == pos - 1) {
                    thickness = 10;
                }
                g.fillRect((startX + x * spacing - (thickness / 2)) + (width / 3), (startY + y * spacing - (thickness / 2)) + (height / 3), thickness, thickness);
                i++;
            }
        }


    }

it is enter the method but it is not draw the background image , What I want to do is to lock the graphics object until he finish drawing the image then continue with other code

can anyone help please

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

2 Answers2

2

Several issues with the code:

  • synchronizing on a Graphics is generally a bad idea
  • paint is only supposed to be called from one thread. are you sure it isn't and you actually need to synchronize? where does rect come from?
  • to make it easier to maintain, you should set drawOnce to true in paint()
michael aubert
  • 6,836
  • 1
  • 16
  • 32
  • synchronizing : I was trying to make it done I need to run it in another thread as my main thread is working to load some data from web service I have tried to use it in several places but it does not work – Amira Elsayed Ismail Jun 28 '12 at 16:24
  • This answer is correct drawing in LWUIT/Codename One is always single threaded. Amira, I suggest you read the developer guide in LWUIT or Codename One and just use a background image style. – Shai Almog Jun 30 '12 at 09:03
-1

You need to set drawOnce to false before calling paint.

It looks like your code is based on GameCanvas. What do you think of the below approach?


    class MainCanvas extends GameCanvas {

    // rect is an attribute

    private void updateScreen() {
        drawOnce = false;
        paint(getGraphics(), rect);
    }

    public void start() {
        new DataThread().start();
        new AnimationThread().start();
    }

    class DataThread extends Thread {
        public void run() {
            while (/*IO stuff not done*/) {
                // save data
                updateScreen();
            }
        }
    }

    class AnimationThread extends Thread {
        public void run() {
            while (/*IO stuff not done*/) {
                // sleep a little
                updateScreen();
            }
        }
    }

    // drawLockLayer and paint(Graphics, Rectangle) methods

    } // end of MainCanvas class

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22