04-13-2010 05:35 PM
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. ![]()
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?
Solved! Go to Solution.
04-13-2010 06:51 PM
you are drawing to the bitmap and not the screen. override paint to draw on the screen
04-13-2010 07:14 PM
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.
04-13-2010 07:45 PM
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);
}
}
04-14-2010 11:42 AM
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);
}
}
04-14-2010 12:22 PM
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.
04-14-2010 01:12 PM
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?
04-14-2010 01:25 PM
public boolean onClose() {
if ((getStyle() & DEFAULT_CLOSE) == DEFAULT_CLOSE) {
if (!isDirty() || onSavePrompt()) {
close();
return true;
}
}
return false;
}
04-14-2010 02:17 PM
Thank you indeed.