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
Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none
Accepted Solution

Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Help!

 

I'm new to Java, much more to blackberry. I'm trying to have some data be drawn, but all I see is a blank screen. :smileysad:

 

JDE is 4.5.0

 

 

// imports be here

public class navandApp extends UiApplication {
	public static void main(String[] args) {
		navandApp app = new navandApp();
		app.enterEventDispatcher();
	}

	public navandApp() {
		pushScreen(new Shcreen());
	}
}

final class Shcreen extends FullScreen {
	public Shcreen() {
		// I create an Array and fill it with color values
		int[] bitArray = new int[36];
		int color1 = 0xFFFF00AA;
		int color2 = 0xFFBBCC00;
		int color3 = 0xFF12BBEE;

		for (int i = 0; i < bitArray.length; i += 3) {
                        // Bitmap will be just a set of 3 consecutive colors over and over
			bitArray[i] = color1;
			bitArray[i + 1] = color2;
			bitArray[i + 2] = color3;
		}
		
		// The idea is to use this Array to form a bitmap and display it on the screen
		// Bitmap's dimensions will be 6x6
		Bitmap image = new Bitmap(6, 6);
		image.setARGB(bitArray, 0, 6, 0, 0, 0, 0);
		Graphics stupidGFXDoesntWork = new Graphics(image);
		// Not sure if the following would be necessary or not...
		stupidGFXDoesntWork.drawBitmap(0, 0, 6, 6, image, 0, 0);
		
		// Nothing happens! Blank screen! >:(
	}
}

 

 

...So what am I doing wrong?

 

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

you are drawing to the bitmap and not the screen. override paint to draw on the screen

Developer
Posts: 1,805
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.997 Dev OS, Z30 (STA100-5)-10.3.1.997 Dev OS, Passport (SQW100-1)-10.3.0.1418, PlayBook (16GB)-2.1.0.1917

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Your not drawing to anything, when you create a Graphics object from a Bimap it doesn't mean your drawing to the screen, it means your drawing to a Bitmap.

 

Try overriding the paint function and using the Graphics object passed into it to draw.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

The line:

 

 

image.setARGB(bitArray, 0, 6, 0, 0, 0, 0);

 

 

should be:

 

 

image.setARGB(bitArray, 0, 6, 0, 0, 6, 6);

 

 

Also, as CMY and rcmaniac25 point out, to draw to the screen you need to override paint(). First, create the Bitmap in the constructor the way you are doing, but just save it in a Shcreen instance variable. Then it's available for later use in paint():

 

 

final class Shcreen extends FullScreen {
    Bitmap image;

public Shcreen() { super(DEFAULT_CLOSE);
// ... } protected void paint(Graphics g) { g.drawBitmap(0, 0, 6, 6, image, 0, 0); } }

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Nothing's happening! I did what you said, but I still have a blank screen.

 

 

public class navandApp extends UiApplication {
	public static void main(String[] args) {
		navandApp app = new navandApp();
		app.enterEventDispatcher();
	}

	public navandApp() {
		pushScreen(new Shcreen());
	}
}

final class Shcreen extends FullScreen {
	public Shcreen() {
		int[] bitArray = new int[36];
		int color1 = 0xFFFF00AA;
		int color2 = 0xFFBBCC00;
		int color3 = 0xFF12BBEE;

		for (int i = 0; i < bitArray.length; i += 3) {
			bitArray[i] = color1;
			bitArray[i + 1] = color2;
			bitArray[i + 2] = color3;
		}

		Bitmap image = new Bitmap(6, 6);
		image.setARGB(bitArray, 0, 6, 0, 0, 6, 6);
		Graphics GGG = new Graphics(image);
		paint(GGG,image);
	}
	protected void paint(Graphics g, Bitmap I){
		g.drawBitmap(0, 0, 6, 6, I, 0, 0);
	}
}

 

 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Well, what you have isn't exactly what I suggested. To override a method in Java, you need to specify exactly the same formal parameters as the superclass method. Also, you need to move the declaration of the image out of the constructor and make it a class variable. Try this version of class Shcreen (I've marked up the changes that I think you should make to your code):

 

 

final class Shcreen extends FullScreen {
Bitmap image;
public Shcreen() {
int[] bitArray = new int[36];
int color1 = 0xFFFF00AA;
int color2 = 0xFFBBCC00;
int color3 = 0xFF12BBEE;

for (int i = 0; i < bitArray.length; i += 3) {
bitArray[i] = color1;
bitArray[i + 1] = color2;
bitArray[i + 2] = color3;
}

Bitmap image = new Bitmap(6, 6);
image.setARGB(bitArray, 0, 6, 0, 0, 6, 6);
Graphics GGG = new Graphics(image);
paint(GGG,image);
}
protected void paint(Graphics g, Bitmap I){
super.paint(g);
g.drawBitmap(0, 0, 6, 6, I image, 0, 0);
}
}

 

The key to understanding this is that the system automatically calls a screen's paint(Graphics) method when it is time for the screen to show on the display. You should not be trying to do this from within the constructor.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Ahhh... now it works.

 

Thank you so much. Before solving the thread, could someone explain to me a few things?

 

1- In that code, why did you eliminated the Graphics? I see paint uses a Graphics as a parameter, but no Graphics are made... is one automatically created when paint is called? 

 

2- also, in a previous post you put:

public Shcreen() {
        super(DEFAULT_CLOSE);
        // ...
    }

 What does this mean?

 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

  1. The system does indeed automatically create a Graphics before calling your screen's paint method.
     
  2. DEFAULT_CLOSE is a style bit that affects the default behavior of the screen's onClose() method (which is usually called in response to the escape key, the "Close" menu item, or some other events). My understanding of onClose() is that it behaves as if it were coded like this:
public boolean onClose() {
  if ((getStyle() & DEFAULT_CLOSE) == DEFAULT_CLOSE) {
    if (!isDirty() || onSavePrompt()) {
      close();
      return true;
    }
  }
  return false;
}

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Posts: 28
Registered: ‎03-15-2010
My Device: none

Re: Graphics, drawBitmap, setARGB.... why can't I draw a bitmap onto the screen?

Thank you indeed.