How to invoke paint method in blackberry
How can i invoke paint multiple time in my app i tried invalidate but but it is not calling paint i think can anybody provide sample code for invoking paint multiple time.
package mypackage;
import com.rss.logger.Log;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
BitmapField objBitmapField;
boolean objBoolean;
int postion=0;
public MyScreen()
{
objBitmapField=new BitmapField(Bitmap.getBitmapResource("bb.png"));
// Set the displayed title of the screen
setTitle("MyTitle");
objBoolean=false;
new AnimationThread().start();
}
private class AnimationThread extends Thread{
public void run() {
super.run();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run()
{
objBoolean=true;
//Add a new LabelField to the screen.
//theScreen.add(new LabelField("Hello there.");
//Call the screen’s invalidate method to
//force the screen to redraw itself.
//Note that invalidate can be called
//at the screen, manager or field level,
//which means you can inform the
//BlackBerry to only redraw the area that
//has changed.
for (int i = 0; i < 20; i++) {
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
开发者_StackOverflow社区 Log.info("in run");
postion=postion+5;
MyScreen.this.invalidate();
}
}
});
}
}
protected void paint(Graphics graphics) {
super.paint(graphics);
Log.info("in paint");
if(objBoolean)
graphics.drawBitmap(20, postion, 50, 50, Bitmap.getBitmapResource("bb.png"), 30, 40);
}
}
Hi piyus find the working code. invalidate() need to call in the paint() method. And one thing is also important that should not create object in paint method.
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
private boolean objBoolean=false;
private int postion=0;
private Bitmap bb=Bitmap.getBitmapResource("bb.png");
public MyScreen()
{
setTitle("MyTitle");
new AnimationThread().start();
}
private class AnimationThread extends Thread
{
public void run()
{
objBoolean=true;
for (int i = 0; i < 20; i++)
{
try
{
sleep(200);
}
catch (InterruptedException e)
{
}
postion=postion+5;
}
}
}
protected void paint(Graphics graphics)
{
super.paint(graphics);
if(objBoolean)
graphics.drawBitmap(20, postion, 50, 50, bb, 30, 40);
invalidate();
}
}
paint() method can be invoked either by using invalidate method or by creating an object of the class that you are using now.
I think there is Typo Error in the sample code you have mentioned here. One cannot have a class inside a class just by defining it. Coming to the issue you are facing
While, calling the invalidate method, as you are using it as MyScreen.this.invalidate
which is not the right way to call it, one cannot call the paint method from another class, as in your case it is AnimationThread
is not possible. The propreitary feature of Screen class and the paint method is, it gets called once the Object of the screen is created.
And as I understand one can call the invalidate method for a manager or a screen, inside the same class but not by creating an object of the screen in another class and by calling it.
It should be called simply without using any objects simply, invalidate();
, gets the screen repainted or invalidated.
One can also achieve it by creating an object of the same screen, may be if one wanted to display the screen in a different way, they can create an object by using different arguments, and handle it accordingly in the paint method. Here, whenever the thread is run, you can create an object and handle it according to your choice.
As a sample :
public class theScreen extends MainScreen{
int dummy;
public theScreen(int firstArg){
// Handle the firstArgument here
dummy=0;
}
public theScreen(int secondArg,int dummy){
// Handle the second Argument here
this.dummy = dummy;
}
public void paint(Graphics graphcis)
{
if(dummy == 0)
{
//Handle what you would like to paint here
}
else{
//Handle with a different paint here
}
}}
Hope this resolves your problem Happy Coding. Cheers
You may try using Screen.doPaint()
.
Is your Bitmap big enough to be completely drawn starting from top/left (30, 40)? It may just be that it doesn't look like it's painting because you're outside of the bounds that make sense for it to be drawn.
精彩评论