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
New Contributor
Posts: 8
Registered: ‎01-21-2014
My Device: 9800
My Carrier: my Self

i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

public class Images extends MainScreen {

int mImgWidth = 80;
int mImgHeight = 80;
int mImgMargin = 10;

String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
"6.png", "7.png" };
EncodedImage[] mImages;

public Images() {
super();
System.out.println("hello");
prepareImages();
}

private void prepareImages() {
mImages = new EncodedImage[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
EncodedImage image = EncodedImage
.getEncodedImageResource(fileNames[i]);
mImages[i] = sizeImage(image, mImgWidth, mImgHeight);
}
}

protected void paint(Graphics graphics) {
paintImages(graphics);
super.paint(graphics);
}

private void paintImages(Graphics graphics) {
int scrWidth = Display.getWidth();
int columns = scrWidth / (mImgWidth + 2 * mImgMargin);
int rows = mImages.length / columns
+ (mImages.length % columns > 0 ? 1 : 0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
EncodedImage image = mImages[i * columns + j];
graphics.drawImage(posX, posY, mImgWidth, mImgHeight,
image, 0, 0, 0);
}
}
}

public EncodedImage sizeImage(EncodedImage image, int width,
int height) {
EncodedImage result = null;

int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);

int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
requiredHeightFixed32);

result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return result;
}
}

New Contributor
Posts: 8
Registered: ‎01-21-2014
My Device: 9800
My Carrier: my Self

Re: i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

Array Index Out of Bounds Exception
Highlighted
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

arrays do not resize. initialize mImages with the required size or use a dynamic structure (like vector).

using a debugger should show you exactly where the exception occurs.
----------------------------------------------------------
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: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

And I suspect your problem is here:

 

EncodedImage image = mImages[i * columns + j] ...

 

I think you need to check that the index you are accessing in mImages actually exists.

New Contributor
Posts: 8
Registered: ‎01-21-2014
My Device: 9800
My Carrier: my Self

Re: i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

still i don't solve it, help me bro

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

Re: i want to show 7 images in screen ,Every image size should be same if i rotate mobile that will adjust

Have you debugged as Simon suggested?  Which statement is throwing the Exception?