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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
amardeepjaiman
Posts: 293
Registered: ‎02-25-2009

drawing bitmap in Mainscreen background

hi,

 

How to draw any image in background of MainScreen ? the solutions mentioned in other threads of forum, is not working .

 

The solution mentioned using Background Class works but that API is provided only in 4.7.

 

For other JDK problem still remains.

Please use plain text.
Developer
simon_hain
Posts: 14,051
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: drawing bitmap in Mainscreen background

use a verticalfieldmanager with USE_ALL_HEIGHT and overwrite its paintBackground method.
----------------------------------------------------------
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
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008

Re: drawing bitmap in Mainscreen background

package com.samples.backgroundImage; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; public class BackgroundImage extends UiApplication { private Bitmap backgroundBitmap; private Bitmap fieldBitmap; public static void main(String[] args) { BackgroundImage theApp = new BackgroundImage(); theApp.enterEventDispatcher(); } public BackgroundImage() { //The background image. backgroundBitmap = Bitmap.getBitmapResource("background.png"); MainScreen mainScreen = new MainScreen(); HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT){ //Override the paint method to draw the background image. public void paint(Graphics graphics) { //Draw the background image and then call paint. graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0); super.paint(graphics); } }; //The LabelField will show up through the transparent image. LabelField labelField = new LabelField("This is a label"); //A bitmap field with a transparent image. //The background image will show up through the transparent BitMapField image. BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png")); //Add the manager to the screen. mainScreen.add(horizontalFieldManager); BasicEditField bef = new BasicEditField("To: ","",50,BasicEditField.FILTER_EMAIL); horizontalFieldManager.add(bef); //Add the fields to the manager. horizontalFieldManager.add(labelField); horizontalFieldManager.add(bitmapField); //Push the screen. pushScreen(mainScreen); } }

 


Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
amardeepjaiman
Posts: 293
Registered: ‎02-25-2009

Re: drawing bitmap in Mainscreen background

[ Edited ]
If i put listfield in a verticalFeildmanager, and put this manage into given horizontalfield manager, then this code doesnt work, and background image scrolls within list scroll. and some repainting occurs.
Message Edited by amardeepjaiman on 04-14-2009 07:33 PM
Please use plain text.
Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Carrier: Vodafone

Re: drawing bitmap in Mainscreen background

[ Edited ]

Try this:

 

 

class MSBMainScreen extends MainScreen { private VerticalFieldManager _container; private Bitmap _BG; public MSBMainScreen() { super(); setTitle( "Main Screen Background Hack" ); _BG = Bitmap.getBitmapResource( "background.png" ); _container = new VerticalFieldManager( Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) { protected void paint( Graphics g ) { // Instead of these next two lines, draw your bitmap int y = MSBMainScreen.this.getMainManager().getVerticalScroll(); g.drawBitmap( 0, y, _BG.getWidth(), _BG.getHeight(), _BG, 0, 0 ); super.paint( g ); } // The keydown and navigation overrides are a workaround // for an optimization hack RIM performed, perhaps assuming that // Managers would not try to take control of painting on // a MainScreen. protected boolean keyDown( int keycode, int status ) { invalidate(); return super.keyDown( keycode, status ); } protected boolean navigationMovement( int dx, int dy, int status, int time ) { invalidate(); return super.navigationMovement( dx, dy, status, time ); } }; super.add( _container ); } public void add( Field field ) { _container.add( field ); } public void delete( Field field ) { _container.delete( field ); } // Implement the rest of the field manipulation functions to redirect ro the new container }

 

 

Cheers

*edit* This seems like a lot of hacking just to draw an image in the BG. Is there any specific piece of functionality you need from a MainScreen that you can't get from a FullScreen?
Message Edited by pwerry on 04-15-2009 03:07 AM
Please use plain text.
Developer
Rajat_10Sep
Posts: 685
Registered: ‎12-02-2008

Re: drawing bitmap in Mainscreen background

Hi,

 

Thanks a lot your solution it really works.

 

One more thing we have to also overide moveFocus() of Manager so that in case of trackball move , image get painted properly.

 

here is the code for that

 

protected int moveFocus(int amount, int status, int time){
              invalidate();
               return super.moveFocus(amount, status, time);
            } 

 

 

The reason behind using mainscreen might be to avoid implementation on touch events.

 

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Please use plain text.
Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Carrier: Vodafone

Re: drawing bitmap in Mainscreen background

Hi Rajat,

 

Overriding navigationMovement() should be enough. Are you observing something different? 

Please use plain text.
Developer
Rajat_10Sep
Posts: 685
Registered: ‎12-02-2008

Re: drawing bitmap in Mainscreen background

Hi,

 

Yes actually I am also adding listField in this manager, so when i scroll with in listfield the image in manager do not paint itself properly so i added that piece of code and it worked fined

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Please use plain text.
Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Carrier: Vodafone

Re: drawing bitmap in Mainscreen background

That's very odd because that's exactly how I tested this one. 

 

Check this out and let me know if there's something you do differently:

 

class MSBMain extends UiApplication { public static void main( String[] args ) { new MSBMain().enterEventDispatcher(); } MSBMain() { // The following is really lazy code. Do not do this in // a consumer grade app :) String[] objects = new String[20]; for ( int i = 0; i < 20; i++ ) { objects[i] = "Line: " + (i + 1); } ObjectListField olf = new ObjectListField(); olf.set( objects ); MSBMainScreen msbms = new MSBMainScreen(); msbms.add( olf ); pushScreen( msbms ); } }

 

Please use plain text.
Developer
Rajat_10Sep
Posts: 685
Registered: ‎12-02-2008

Re: drawing bitmap in Mainscreen background

Hi,

 

Sorry for replying late

 

below is the code which i am using

 

import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.VerticalFieldManager; import net.rim.device.api.ui.component.BitmapField; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.component.ListField; import net.rim.device.api.ui.component.ListFieldCallback; import net.rim.device.api.system.Bitmap; import net.rim.device.api.ui.Manager; class MSBMainScreen extends MainScreen implements ListFieldCallback { String Data[] = {"Task ID","Priority","Task Name","Location","Task Type","State","AAA","BBB","CCCC","DDD","EEE","FFFF","GGGG","HHHH","IIII"}; private VerticalFieldManager objManager; private Bitmap objBitmap; ListField objList = null; public MSBMainScreen(){ super(); drawComponent(); setTitle( "Inbox Screen" ); objBitmap = Bitmap.getBitmapResource( "watermark.png"); objManager = new VerticalFieldManager( Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) { protected void paint( Graphics g ){ int y = MSBMainScreen.this.getMainManager().getVerticalScroll(); g.drawBitmap( 0, y, objBitmap.getWidth(), objBitmap.getHeight(), objBitmap, 0, 0 ); super.paint( g ); } protected boolean keyDown( int keycode, int status ){ invalidate(); return super.keyDown( keycode, status ); } protected boolean navigationMovement( int dx, int dy, int status, int time ){ invalidate(); return super.navigationMovement( dx, dy, status, time ); } protected int moveFocus(int amount, int status, int time){ invalidate(); return super.moveFocus(amount, status, time); } }; add(objList); super.add( objManager ); } public void add( Field field ){ objManager.add( field ); } public void delete( Field field ){ objManager.delete( field ); } public void drawListRow(ListField listField, Graphics g, int index, int y, int width){ g.drawText(Data[index], /*10*/10, /*CTIAMain.ROW_HEIGHT*index*/y ); } public int indexOfList(ListField listField, String prefix, int start){ return 0; } public Object get(ListField listField, int index){ return Data[index]; } public int getPreferredWidth(ListField listField){ return 0; } private void drawComponent(){ objList = new ListField(Data.length); objList.setCallback(this); } }

 

If i comment moveFocu() then , when i press trackball the image below list does not paint itself properly, but if i uncomment moveFocu() it works fine.

 

 

 

 

 

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Please use plain text.