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
Developer
Posts: 196
Registered: ‎12-14-2009
My Device: Not Specified

Repeating background color change

I want the background to change colors repeatedly.

I tried:

 

While (True)

{

set background one color in java code using proper backgroundfactory api;

set background another color in java code;

set background another color in java code;

set background another color in java code;

set background another color in java code;

}

 

and the app doesn't even open. The "thinking" icon comes up and the simulator freezes. Any idea how I can simple implement a flashing repeating background color change on one screen?

 

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Repeating background color change

[ Edited ]

Use your code within separate thread like below

 

class BrowserFieldDemoScreen extends Screen
{
	int arr[]={Color.RED,Color.BLUE,Color.GREEN,Color.YELLOW,Color.WHITE};
	int color=arr[0];
    public BrowserFieldDemoScreen()
    {
    	super(new VerticalFieldManager(VERTICAL_SCROLL));
    	ChangeColor();
    }
    public void ChangeColor()
    {
    	Thread t=new Thread(new Runnable(){

			public void run()
			{
				while (true)
		    	{
			    	for(int i=0;i<arr.length;i++)
			    	{
			    		color=arr[i];
			    		invalidate()
			    		;
			    		try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
			    	}
		    	}
			}});
    	t.start();
    	
    }
    protected void sublayout(int arg0, int arg1) {
		 setExtent(Display.getWidth(), Display.getHeight());
	     setPosition(0,0);
	     layoutDelegate(Display.getWidth(), Display.getHeight());
	}
	
	protected void paint(Graphics graphics) {
		
		graphics.setColor(color);
		graphics.fillRect(0, 0, Display.getWidth(), Display.getHeight());
		super.paint(graphics);		
	}
}

 

 

Developer
Posts: 196
Registered: ‎12-14-2009
My Device: Not Specified

Re: Repeating background color change

[ Edited ]

I get an error building

 

at: super(new VerticalFieldManager(VERTICAL_SCROLL));

 

"The constructor MainScreen(

VerticalFieldManager) is undefined

 

 

possible fix:

 

 

"Remove argument to match MainScreen"

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Repeating background color change

If you are using MainScreen then no need of super(new VerticalFieldManager(VERTICAL_SCROLL)); in the constructor. This is for the Screen.

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

Re: Repeating background color change

Here is a variation on Aman_vivek's code that I think is a little more robust and more efficient in that it only runs while the overriden Field is displayed.

 

You can use this code to override a single Field.  But the same code will work for a Manager as well. 

 

I've removed the layout code which I think is not necessary, even if you are trying to update the complete screen.  If you are struggling with getting a manager to do the full screen, I recommend this article:

http://supportforums.blackberry.com/t5/Java-Development/MainScreen-explained/ta-p/606644

 

I hope this code works for you.

 

LabelField padLab = new LabelField("Focusable flashing Label", LabelField.FOCUSABLE) {
    int currentColorIndex = -1;
    int [] colorArray = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.PURPLE };
    Thread flashingThread;
    public void onDisplay() {
        this.onExposed();
    }
    public void onExposed() {
        if ( flashingThread == null ||
             !flashingThread.isAlive() ) {
            flashingThread = new Thread() {
                public void run() {
                    try {
                        while (true) {
                            invalidate();
                            Thread.sleep(500);
                        }
                    } catch (Exception e) {
                    }
                }
            };
           flashingThread.start();
        }
    }
    public void onObscured() {
        if ( flashingThread != null ) {
            if ( flashingThread.isAlive() ) {
                flashingThread.interrupt();
            }
        }
    }
    protected void paintBackground(Graphics graphics) {
        if ( ++currentColorIndex >= colorArray.length ) {
            currentColorIndex = 0;
        }
        int currentBackground = graphics.getBackgroundColor();
        try {
            graphics.setBackgroundColor(colorArray[currentColorIndex]);
            graphics.clear();
        } finally {
            graphics.setBackgroundColor(currentBackground);
        }
    }
};

Developer
Posts: 196
Registered: ‎12-14-2009
My Device: Not Specified

Re: Repeating background color change

For some reason with both codes all I get is a white screen when the app opens. 

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

Re: Repeating background color change

Have you pushed a Screen containing the flashing Field or Manager?

 

Why don't you take one of the RIM samples and add a flashing Filed to it to see what happens?