Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900
Accepted Solution

How to clear text written over the image

[ Edited ]

Hi,

 

I have one screen with background picture (bitmap). I draw text and other images over this background using graphics object and all looks ok when I show the screen - but only the first time because next time background picture is the one containing all these drawings on it. 

I would like to know if it's possible to clear these drawings so next time I can use clear background image to draw on it again?

 

I use it something like this:

 

 

String fText = "Text";

 

fBackground = Bitmap.getBitmapResource("background.png")

fGraphics = new Graphics (fBackground );

fvm1 = new VerticalFieldManager()    
{     
   
public void paint(Graphics graphics)  
{
   fGraphics.drawText(fText, 0, 0);

   graphics.drawBitmap(0, 0, fBackground.getWidth(),  fBackground.getHeight(), fBackground, 0, 0);
        
   super.paint(graphics);
}
}

 

 

Next time I want to draw text "Text2" so I change variable fText to "Text2". How can I remove text "Text" before doing that?

 

Thx in advance.

 

 

Please use plain text.
Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: How to clear text written over the image

After setting fText, try calling invalidate(). This should repaint your screen.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to clear text written over the image

The problem is that you are drawing onto your bitmap (using it as a canvas) thus permanently changing it. Your question should be "How else can I show that text on the screen"? Replace the call to fGraphics.drawText(...) with graphics.drawText(...) and put it after super.paint() - and you will be all set!

 

Anyway, an even better question is "what are you trying to achieve?"  If you could briefly describe what you want to see, we could help better.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900

Re: How to clear text written over the image

[ Edited ]

Thank you for your replies.

 

I want to have a screen which contains battery status, sound profiles and other icons and text fields like current time. I need to update the screen occasionally so the text will change and icons also (battery will drain etc). 

How to do that?

 

If i use only graphics and not fGraphics, nothing is shown on background picture. Also, invalidate doesn't helps me because problem occurs only when  paint() method is already called.

 

So, how can I have one background image with all these icons and text on it and change only text/icon without changing background image?

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to clear text written over the image

You really don't want to paint on the background bitmap to show changing data.

 

One of the most obvious solutions is to have Fields for that - both LabelField and RichTextField are good for displaying text, BitmapField is good for displaying bitmaps (use setBitmap to change the image), GaugeField is great for percent indication, etc.  Add the fields to your VerticalFieldManager, use margins and style bits such as FIELD_HCENTER to space and position them the way you like and enjoy. When a new value to be displayed comes, change the corresponding field and invalidate it. This is the right way of doing such stuff.

 

Of course - since you seem to be fixated on using Graphics directly - the quick and dirty approach is just to paint on the screen directly, using the Graphics object passed into paint().  Something like this:

 

fBackground = Bitmap.getBitmapResource("background.png")

// fGraphics = new Graphics (fBackground );

fvm1 = new VerticalFieldManager()    
{     
   
public void paint(Graphics graphics)  
{
   // fGraphics.drawText(fText, 0, 0);

   graphics.drawBitmap(0, 0, fBackground.getWidth(),  fBackground.getHeight(), fBackground, 0, 0);
   graphics.drawText(fText, 0, 0); // plus any other draws...
   super.paint(graphics);
}
}

Of course, you also need to override that VerticalFieldManager's sublayout() to set its dimensions properly.

 

However, doing it this way is really strange. Either you need something much simpler than a VerticalFieldManager to paint in it (extend Field, override its layout() to setExtent() properly and its paint() to do whatever painting you need - do not call super.paint(), it's abstract) or you need to add Fields to it as usual and let super.paint() do its job.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900

Re: How to clear text written over the image

Thx. 

 

I came to this solution myself too. Fields usage is much more easier way to do what I needed. Now all works like it should do :smileyhappy:

Please use plain text.