views:

20

answers:

1

Hi,

I am trying to put in an icon (a scaled image) as part of a button that also contains some text. I am programming in J2ME for the Nokia SDK (S60 device) and using Eclipse.

The code is as follows:
but = new Button("Some text");
Image img = null;
try {
img = Image.createImage("/flower.png");
} catch(IOException e) {
e1.printStackTrace();
}
but.setIcon(img);

The above lines are the code that works properly. I am facing problems in scaling the image to the size of the button. Whenever I try to do that, I get a divide by zero error. The function I am using to scale the image and the way it is being scaled is:
Image img2 = null;
img2 = img.scaled(but.getWidth()/2, but.getHeight());
but.setIcon(img2);

I am unable to figure out why I get a divide by zero error every time I try to run the above code. Is there some other function that I should use? Or is there something I am missing?

Any help is most welcome, Thanks.

A: 

which UI Framework are using, is it LWUIT? if yes, you can't get the width/height of any component before showing the form, you should use getPreferredWidth instead

Mahdi Hijazi
hi, yes, I am using LWUIT. I think the problem arises because I am using fractions to scale the width and height while they are int. Say (0.5)*width might become 0 in integer arithmetic. I am yet to try this out since I have been busy with other things, but I think this could be a possible reason.
Sriram
You don't have fractions at all, getWidth returns int, when you divide it by 2 you will get an int not float, unless you divide on 2.0f.
Mahdi Hijazi