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
Highlighted
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch
Accepted Solution

Create Bitmap from Graphics object

I have a Graphics object for one field and I override paint() and I do some additional paining.

After that I want to create a Bitmap but the problem is that the Graphics object isn't for the Bitmap class.

 

So how can I create a Bitmap object using the Graphics I have?

----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Create Bitmap from Graphics object

I have never found a way to do this.

 

Just to confirm the question, you have a paint method and in it you have painted onto your Graphics object.  Now you want to make a copy of this painted region, on a separate Bitmap so that you can use it elsewhere.

 

In this circumstance, I have only two solutions

a) Paint onto a separate Bitmap first, and then paint the Bitmap onto the graphics object.

b) Display the Graphics on a screen and do a Screenshot. 

 

Love to hear other options. 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Create Bitmap from Graphics object

i share peters experience.
you can create an empty bitmap and paint on both the bitmap and the field.
calling super.paint with a different graphics could also work, did not try this yet.
----------------------------------------------------------
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
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Create Bitmap from Graphics object

[ Edited ]

Yeah, you can pass the new Graphics object into the paint of your Screen directly as long as you make the paint method public. If your Screen is already populated, then you should get the same results as what you would see on the display. The same works for any Manager or Field. Just be aware that the background of the new Graphics object starts out as white and not transparent.

Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Create Bitmap from Graphics object

Thanks for the suggestions everyone!

 

@peter_strange:

I don't know how to paint on a bitmap. If by paint you mean to override the paint() method this is not possible because BItmap is a final class and extending it is not allowed. Maybe I did not understand you correctly?

Making a screenshot also is not a solution for me because the bitmap is way bigger than the screen and I am using scrollable container to show it.

 

Also creating a new Graphics object for the bitmap is not a solution for me because as I mentioned my bitmap is big (1600x1200) and creating that big bitmap causes IllegalArgumentException to be thrown in the graphic's constructor.

----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Create Bitmap from Graphics object

Hm, I'm confused. Are you trying to paint/copy your Field/Manager to a new Graphics/Bitmap, or are you trying to paint a Bitmap to your Field/Manager?

Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Create Bitmap from Graphics object

OK let me explain.

I have a big bitmap in a bitmap field. At some point I call invalidate() and as a result the paint() method is called. There I repaint that bitmap but rotated using the graphics object from the paint() method. Visually it looks like I want it to look but the bitmap instance is not updated and the rotated image is not saved.
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Create Bitmap from Graphics object

i would suggest that you manipulate the bitmap directly (not in the paint method) and use setBitmap on the bitmapfield afterwards.
----------------------------------------------------------
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
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Create Bitmap from Graphics object

OK It seems that there is no easy way to do it. I found a way to rotate image but it works only for square shaped images so I decided to go and resize the bitmap and then rotate it. I created a new thread for that:

http://supportforums.blackberry.com/t5/Java-Development/How-to-resize-Bitmap/td-p/1301825
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Create Bitmap from Graphics object

I did something which works for me in case someone is interested:
http://supportforums.blackberry.com/t5/Java-Development/How-to-rotate-big-images/m-p/1293987/highlig...
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy