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
Trusted Contributor
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
My Carrier: Koodo
Accepted Solution

scaling bitmap problems

Hi there, I'm trying to make my app compatible on all phones, and I've come to a roadblock. 

-the sNormButton parameters are just an experiment to see if this works

-the normal parameters of the image are 40x20 I believe

-everything works perfectly before I put in the scaling code

-right now it's finding a null pointer exception when I trigger the image

Here's my code on the mainScreen:

 

Bitmap sNormButton = new Bitmap(10,5);

 

.......

public mainUIScreen()
    {
        super();
        ......

if((Display.getWidth() == 320) && (Display.getHeight() == 240))
        {
            A.newGame.scaleInto(sNormButton, Bitmap.FILTER_BILINEAR);
        }

 

.....

 

    newGameBut = new BitmapButtonField(A.newGame,A.newGameUp);
    newGameBut.setChangeListener(this);

 

 

I'm new to the scaling thing so any words of advice are greatly appreciated, thanks!!!

Developer
Posts: 285
Registered: ‎08-06-2010
My Device: Z10

Re: scaling bitmap problems

Is this for 5.0 and above?

 

If so, use this class for scaling: http://supportforums.blackberry.com/t5/Java-Development/Resizing-Transparent-Bitmaps/ta-p/703239 it's clean and works with images that have transparent backgrounds(which the BB SDK does not).



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Trusted Contributor
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
My Carrier: Koodo

Re: scaling bitmap problems

Yea it's 5.0 and above. Thanks for the response I'll check it out tomorrow and get back to you.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: scaling bitmap problems

IN theory you are just being given an alternative scaling mechanism.  How you scale it should not effect the processing, so in theory will not effect the NPE.

 

If you continue to see a problem, please tell us the line that throws the NPE. 

Trusted Contributor
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
My Carrier: Koodo

Re: scaling bitmap problems

Hey, so I tried the other method you described and it still doesn't work (although the initial picture at least shows up, the error happens when I remove focus from the rezised image now, so I will need to use your method thanks). The behaviour I guess is a problem in the custom button class (I ran the debugger).

 

It's having a problem with the line at the end that is "    graph.drawBitmap(0, 0, getWidth(), getHeight(),
                             _currentPicture, 0, 0);"

 

Here's the custom class I use:

 

 

public class BitmapButtonField extends Field
{

    Bitmap _currentPicture;
    private Bitmap _onPicture;
    Bitmap _offPicture;
    private int id;
    
    
    public BitmapButtonField (Bitmap  onImage, Bitmap offImage)
    {
       super(Field.FOCUSABLE|Field.FIELD_HCENTER);
       _offPicture = offImage;
       _onPicture = onImage;
       _currentPicture = _onPicture;
    }
    
    public void setButtonImage (Bitmap onImage, Bitmap offImage)
    {
        _offPicture = offImage;
        _onPicture = onImage;
        _currentPicture = _onPicture;
     }
    
    public void setButtonId(int id)
    {
        this.id = id;
    }
    public int getButtonId()
    {
        return this.id;
    }    
    
    public int getPreferredHeight()
    {
        return _onPicture.getHeight();
    }
    
    
    public int getPreferredWidth()
    {
        return _onPicture.getWidth();
    }
    
    protected void onFocus(int direction)
    {
        _currentPicture = _offPicture;
       invalidate();
    }
    
    protected void onUnfocus()
    {
        _currentPicture = _onPicture;
       invalidate();
    }
    
    protected void drawFocus(Graphics g, boolean on)
    {        
        g.setBackgroundColor(Color.BLACK);
    }
    
    protected void layout(int width, int height)
    {
        setExtent(Math.min( width, getPreferredWidth()), Math.min(
                            height, getPreferredHeight()));
    }
    
    

    //protected void paint(Graphics graphics)
    //{
      //  graphics.setBackgroundColor(Color.BLACK);
        //graphics.clear();
        //graphics.fillRect(0, 0, getWidth(), getHeight());
        //graphics.drawBitmap(0, 0, getWidth(), getHeight(),
      //                       _currentPicture, 0, 0);
    //}

    

        protected void paintBackground (Graphics graph){

            try {
            graph.setBackgroundColor(Color.BLACK);

            graph.clear();

            }
            finally{}
        }
        protected void paint (Graphics graph){
        graph.setColor(Color.BLACK);
        //super.paint(graph);
        graph.fillRect(0, 0, getWidth(), getHeight());
        graph.drawBitmap(0, 0, getWidth(), getHeight(),
                             _currentPicture, 0, 0);
    
        }

Trusted Contributor
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
My Carrier: Koodo

Re: scaling bitmap problems

So ya, does anybody know what the problem with this line is or why it would cause a Null Pointer Exception when there wasn't one before I started scaling?



" graph.drawBitmap(0, 0, getWidth(), getHeight(),
_currentPicture, 0, 0);"
Regular Contributor
Posts: 67
Registered: ‎08-16-2011
My Device: 7185
My Carrier: Reliance

Re: scaling bitmap problems

use this code

 

public class ImageResize
{
public static Bitmap resizeBitmap(Bitmap image, int width, int height)
    {        

        //Need an array (for RGB, with the size of original image)
        //
        int rgb[] = new int[image.getWidth()*image.getHeight()];

        //Get the RGB array of image into "rgb"
        //
        image.getARGB(rgb, 0, image.getWidth(), 0, 0,image.getWidth(), image.getHeight());

        //Call to our function and obtain RGB2
        //
        int rgb2[] = rescaleArray(rgb, image.getWidth(),

          image.getHeight(), width, height);

        //Create an image with that RGB array
        //
        Bitmap temp2 = new Bitmap(width, height);

        temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
        
        return temp2;
    }
    private static int[] rescaleArray(int[] ini, int x, int y, int x2,int y2)
    {
        int out[] = new int[x2*y2];
        for (int yy = 0; yy < y2; yy++)
        {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++)
            {
                int dx = xx * x / x2;
                out[(x2 * yy) + xx] = ini[(x * dy) + dx];
            }
        }
        return out;
    }
}

call

ImageResize.resizeBitmap(Bitmap image, int width, int height);

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: scaling bitmap problems

Not being rude or anything here but I would not use the scaling method suggested by GaneshPachpind.  Stick with the ones supplied by RIM.  You will find the results are better and the scaling will be significantly faster since the scaling will be done in C rather than java. 

 

And again, not being rude here, but I think you should be able to figure out this problem.

 

A null pointer exception means that there is an object that is null.  You say that this line throws the exception:

graph.drawBitmap(0, 0, getWidth(), getHeight(),_currentPicture, 0, 0);

 

The only object in here that could be null is _currentPicture, so what sets it to null?  You can put break points on any place that sets this and on the actual drawBitmap, so I would have thought it would be easy to work out what is setting this to null.  Can you do this?