views:

350

answers:

4

Hi all,

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?

A: 

Hi Shreyas! 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]);
    }
    
    } }
Max Gontar
Hi coldice, first of all thx a lot for the answer. Now its coming next to each other. See my answer also. I was trying this for 3 days. Thanks a lot......
Shreyas
A: 

Hi Coldice,

First of all thx a lot for the answer.

u r right.

setBitmap() is not necessary & synchronized(UiApplication.getEventLock()) is necessary. If i dont use it, the error comes as

java.lang.IllegalStateException: UI engine accessed without holding the event lock.

Now its coming next to each other.

Instead of drawing it using paint(), I used

Graphics graphics = new Graphics(bitmap[i]);

& using g.drawLine(), I m drawing the Bitmap.

    bitmap[i] = new Bitmap(50, 50);
    Graphics graphics = new Graphics(bitmap[i]);
    bitmapField[i] = new BitmapField(bitmap[i]);

    synchronized(UiApplication.getEventLock())
    {
      hfm_BitmapField.add(bitmapField[i]);  
    }

Thanks a lot.......

Shreyas
A: 

Now the problem is, Initially I set BackgroundColor to HorizontalFieldManager.

Background grayBackground = BackgroundFactory.createSolidBackground(Color.LIGHTGREY);
hfm_BitmapField.setBackground(grayBackground);

when I draws bitmap using ,

bitmap[i] = new Bitmap(50, 50);  
Graphics g = new Graphics(bitmap[i]);
g.setBackgroundColor(Color.LIGHTGREY);
g.drawLine(5,5,25,25);
bitmapField[i] = new BitmapField(bitmap[i]);
synchronized(UiApplication.getEventLock())
        {
            hfm_BitmapField.add(bitmapField[i]);
        }

in Simulator when I runs initially BackgroundColor for HorizontalFieldManager is set to grayBackground but when I draws bitmap, creates bitmapField & is added to HFM then the BackgroundColor for that specific bitmapField is removed.

Any clue why this happens? How to set the BackgroundColor for HFM permanently?

Shreyas
A: 

Hi all,

as

Graphics graphics = new Graphics(bitmap[i]);

is Deprecated, u can use

Graphics g = Graphics.create(bitmap[i]);
Shreyas