Computer/J2ME for GSM

GX10/10i callSerially bug 및 프로그램 구조

holycall 2004. 10. 16. 21:44

 

callSerially 메소드에 심각한 문제가 있는듯 하다.

 

callSerially를 썼을 때 freeze하던 것이

 

 thread를 이용한 방식으로 바꾸었을 때 갑자기 돌아가기 시작했다.

 

원래 코드 구조가

class ... extends Canvas implemsnts Runnable {

 

Displayable display;

boolean SUSPEND;

 

public void hideNotify() {

  SUSPEND = true;

  pauseTimer();

}

 

public void showNotify() {

  SUSPEND = false;

  fullUpdate = true;

  resumeTimer();

}

 

public void run() {

  if (!SUSPEND) {

    processKey();   

    updateFrame();

    repaint();   

  }

  display.callSerially(this);
}

 

int width, height;

Image offImg;

Graphics off;

 

public void paint(Graphics g) {

  if (SUSPEND) return;

  if (offImg == null) {

    offImg = Image.createImage(width, height);

    off = offImage.getGraphics();

  }

  g.drawImage(offImg, 0, 0, 20);

}

 

...

 

}

 

이렇게 하면 안되고 보통 하는 식으로 해야 한다.

callSerially 쓰면 다른 폰들에서 7-8fps 정도 이득이 있었는데

샤프는 아예 동작을 안한다. T.T

되도록 바꾼다면,

 

class ... extends Canvas implemsnts Runnable {

 

Displayable display;

boolean SUSPEND;

 

public void hideNotify() {

  SUSPEND = true;

  pauseTimer();

}

 

public void showNotify() {

  SUSPEND = false;

  fullUpdate = true;

  resumeTimer();

}

 

long prevTime;

public void run() {

  while (isRunning) {

    long currentTime = System.currentTimeMillis();

    if (currentTime - prevTime < 50) { // 50ms per frame = 20 fps

      try {Thread.sleep(50 - currentTime + prevTime); } catch (Exception e) {}

    }

    if (!SUSPEND) {

      processKey();   

      updateFrame();

      repaint();   

      serviceRepaints();  // force repaint

    }

  }
}

...

 

}