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
Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Canvas-like screen in UiApplication

Is there a way to perform some low level painting in a UiApplication? What I need is something like Canvas in Midlets. At this time what I am doing is to add a Field in a Mainscreen with its paint() method overwritten, from where I can draw the entire screen.
This works, but I think there must be a more proper manner. I would like to do it in a class lighter than MainScreen (for example Screen), because I don't really need the default managers nor menu options. Where should I place the drawing code? Some snippets will be welcomed.
Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

I would suggest a subclass of FullScreen.

 

You can create the canvas like this:

 

Bitmap canvas = new Bitmap(Display.getWidth(),Display,getHeight())

Graphics g = Graphics.create(canvas)

 

Then use this Graphics object to do your painting on the canvas, and override the paint() method in your FullScreen to paint the canvas using drawBitmap()

 

...or something like that. Your mileage may vary.

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

The Bitmap option is a good one, and provides you with the ability to double buffer (multiple Bitmaps), which you might find useful.  I think this is the official way.

 

One other possibility is to use:

Screen.getGraphics();

 

Here is the text from the javadoc.

 

public Graphics getGraphics()
Retrieves the graphics context for this screen.
This method returns the graphics context for this screen, with a clipping region on top of the graphics stack set to the screen's entire extent.
The Graphics object returned by this call becomes invalid and should not be used after the end of the current UI event.
Note: We recommend against direct use of this method; instead, use the various invalidate() methods. It may be used for direct drawing to the display when the screen contains no fields.

 

So this basically gives the same as the Bitmap, but you don't need to paint the bitmap onto the screen buffer, because you are doing it directly.  I've tried this, it does seem to work, but have not used this in the wild yet.  So, YMMV.

Please use plain text.
Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

Ok, thanks both. Although BlackBerry, like most recent mobile phones, is double buffered so there's no need to perform your own buffering.

I miss an official explanation of this in manuals.

Please use plain text.
Developer
simon_hain
Posts: 16,001
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Canvas-like screen in UiApplication

direct drawing on the screen works very well, regardless of other items displayed on the screen.
if you use transparency you can paint several layers if you have to.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

I'm trying the direct draw approach, but all I get is a blank screen.

Tried RexDoug Bitmap approach also, but this method:

Graphics g = Graphics.create(canvas);

doesn't seem to exist. I'm using OS 4.2.1

 

 

Please use plain text.
Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

Ok, I have found two ways by now:

 

OPTION 1: Use a Bitmap and draw it in the overwritten paint() method:

 

import net.rim.device.api.system.Bitmap; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.container.FullScreen; public class MyCanvas extends FullScreen { private Graphics canvas; private Main origen; private Bitmap bm; private int width, height; public MyCanvas(){ super(); width = Graphics.getScreenWidth(); height = Graphics.getScreenHeight(); bm = new Bitmap(width, height); canvas = new Graphics(bm); } public void paint(Graphics g){ //draw on canvas, for instance canvas.fillRect g.drawBitmap(0, 0, origen.width, origen.height, bm, 0, 0); } }

 OPTION 2: why to use the bitmap?:

 

public void paint(Graphics g){ //draw directly on g }

And for those searching in manuals, where this issue is not covered, this link can also be useful:

article on developer journal

 

The third option I tryed was to use Screen.getGraphics(), but is returning me a blank screen.

 


 

 

Please use plain text.
New Developer
fred_sf
Posts: 1
Registered: ‎11-27-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

I'm new to BB developement. I trying to show a Field (obtained by calling : (Field)_video_controller.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field"):smileywink: with a Bitmap (with transparency) on top of it.

 

Reading your post gave me some ideas... For example, I overrided the paint method of the FullScreen that contains the Field, like this :

public void paint(Graphics g){     
        super.paint(g);
        
        g.drawBitmap(30, 30, _imageItems.getWidth(), _imageItems.getHeight(), _imageItems, 0, 0);
       
     }

 

with this approach the bitmap is drawn order the Field. Any ideas of what I'm doing wrong ?

 

What would be the best way for painting layers ?

 

Thanks

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Canvas-like screen in UiApplication

Your call to super.paint() is delegated to all of the contained managers and fields; then you painted over top of the fields.

 

If you want a bitmap to be the background, override paintBackground() in your screen class. This is what it was designed for.

 

After paintBackground(), the other paint() methods will be hit and everything will paint over the background image.

 

See the API docs for class Screen.

 

Please use plain text.
New Contributor
reyal
Posts: 3
Registered: ‎08-03-2010
My Device: Blackberry storm 9520

Re: Canvas-like screen in UiApplication

Hi RexDoug,

 

I don't think you understood fred_sf correctly. I think what he wants is to make the bitmap(_imageItems) to appear on top of the video field. I want to do the same but after reading to some of the posts here, I think the API is not yet ready for this kind of layout.

Please use plain text.