09-07-2010 09:39 AM - edited 09-07-2010 09:42 AM
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.
Solved! Go to Solution.
09-07-2010 02:25 PM
09-07-2010 04:06 PM
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.
09-08-2010 06:19 AM - edited 09-08-2010 06:20 AM
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?
09-08-2010 10:36 AM
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.
09-08-2010 12:08 PM
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 ![]()