Search code examples
blackberryjava-me

accessing Inner class while creating BitmapField & adding it in HorizontalFieldManager


I'm creating a inner class in a method. After that I am accessing some statements like,

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++;

But here the problem is after creating the bitmap & before creating the bitmapField, control goes to

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

So before creating the bitmapField, it adds it in hfm.

So the output is coming like "Everytime a new BitmapField is added in hfm (means at the same position by replacing the previous one)". But I want the BitmapFields come next to each other in hfm.

How to do it?

Any solution why the control goes first to hfm.add() before the new bitmapField() inner class?


Solution

  • first of all, several suggestions about code:

    • see no reason using synchronized since it's UI thread
    • don't setBitmap, bitmap is already passed to BitmapField constructor
    • actually all BitmapFields come next to each other in hfm, to make it clear, I've add number to each one.
    • if you want some custom constructor or new fields in BitmapField, it's better to create new class as an extension of 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]);
          }
      }
      }