2009-08-26 62 views
1

我在方法中創建一個內部類。之後,我訪問了一些之類的語句,訪問內部類創建BitmapField並將其添加到Horizo​​ntalFieldManager

public class Test extends MainScreen 
{ 
    HorizontalFieldManager hfm; 
    Bitmap bitmap[] = new Bitmap[100]; 
    BitmapField[] bitmapField = new BitmapField[100]; 
    int countBitmap = 0; 

    Test() 
    { 
     VerticalFieldManager vfm_Main = new VerticalFieldManager(); 
     hfm = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH); 
     vfm_Main.add(hfm); 
     add(vfm_Main); 
    } 


    void drawBitmap() 
    { 
    bitmap[countBitmap] = new Bitmap(100, 100); 
    bitmapField[countBitmap] = new BitmapField(bitmap[countBitmap]){ 
    public void paint(Graphics g) 
    { 
      ................ 
      ................ 
      g.drawLine(x1,y1,x2,y2); 
     } 
    } 


    synchronized(UiApplication.getEventLock()) 
    { 

     for(int i = 0 ; i < bitmapField.length; i++) 
     { 
     if(bitmapField[i] != null) 
     { 
       bitmapField[i].setBitmap(bitmap[i]); 
     } 
     } 
     hfm.add(bitmapField[countBitmap]); 
     countBitmap++; 

但這裏的問題是創建bitmapField之前創建位圖&後,就進入

synchronized(UiApplication.getEventLock()){hfm.add(bitmapField[countBitmap]); } 

所以創建bitmapField之前,它增加了它在hfm。

所以輸出就像「每次在hfm中添加一個新的BitmapField(意味着通過替換前一個位置在相同的位置)」。但是我希望BitmapField在hfm中相鄰。

怎麼辦?

控件在新的bitmapField()內部類之前首先轉到hfm.add()的任何解決方案?

+0

你的編碼風格在這裏似乎參差不齊的,而且似乎有些接近括號丟失。讓你難以理解你在這裏做什麼。例如,第二代代碼被剪切到哪裏?它是drawBitmap()的結束嗎? – Richard 2009-08-26 19:46:26

+0

現在解決替換問題(通過替換前一個替換相同的位置)。但是一個新問題是,最近的圖形是通過替換前一個圖形而添加的。 意思是如果你第一次畫,那麼它正確地在bitmapField中。如果你第二次繪製,那麼通過替換前一個,第二次繪製的圖形被添加到bitmapField中。 paint()被調用2次繪製2個圖紙。 如何解決這個問題? – Shreyas 2009-08-27 09:22:01

回答

1

首先,有關代碼的幾個建議:

  • 看不出有任何理由使用同步,因爲它的UI線程
  • 不setBitmap,位圖已經傳遞給BitmapField構造
  • 實際上所有BitmapFields來在hfm中彼此相鄰,爲了說清楚,我已經爲每一個添加了數字。
  • ,如果你想一些自定義構造函數或BitmapField新的領域,它更好地創建新類作爲BitmapField的擴展

    class TestScr extends MainScreen { 
    HorizontalFieldManager hfm; 
    Bitmap bitmap[] = new Bitmap[100]; 
    BitmapField[] bitmapField = new BitmapField[100]; 
    
    TestScr() { 
        hfm = new HorizontalFieldManager(); 
        add(hfm); 
        drawBitmap(); 
    } 
    
    void drawBitmap() { 
        for (int i = 0; i < 5; i++) { 
         bitmap[i] = new Bitmap(50, 50); 
    
         Graphics graphics = new Graphics(bitmap[i]); 
         graphics.setColor(Color.RED); 
         String number = Integer.toString(i); 
         Font font = graphics.getFont().derive(Font.BOLD, 40, Ui.UNITS_px); 
         graphics.setFont(font); 
         int textWidth = graphics.getFont().getAdvance(number); 
         int textHeight = graphics.getFont().getHeight(); 
         int x = (bitmap[i].getWidth() - textWidth)/2; 
         int y = (bitmap[i].getHeight() - textHeight)/2; 
         graphics.drawText(number, x, y); 
    
         bitmapField[i] = new BitmapField(bitmap[i]) { 
          public void paint(Graphics g) { 
           super.paint(g); 
           int width = getWidth() - 1; 
           int height = getHeight() - 1; 
           g.setColor(Color.GREEN); 
           g.drawLine(0, 0, width, 0); 
           g.drawLine(width, 0, width, height); 
           g.drawLine(width, height, 0, height); 
           g.drawLine(0, height, 0, 0); 
          } 
         }; 
    
         hfm.add(bitmapField[i]); 
        } 
    } 
    } 
    
+0

你好,首先thx很多的答案。現在它正在彼此相鄰。也請參閱我的答案。我試了3天。非常感謝...... – Shreyas 2009-08-27 10:14:38

相關問題