Introducing a Lightweight UI Toolkit: Bringing Desktop Development into Java ME
Loading a theme and resources
After defining your theme, adding your images, and setting your fonts, your app needs to load all this information. I put the resources file on the default package in the src directory, to be packed easily in the jar by Netbeans, but you can modify the proportioned ant script to satisfy your needs. To load your theme, you have to open your resource file, set your theme on UIManager (Singleton class that manages the look of the application), and at least, if you are using WTK as an emulator, refresh the theme on Display. I didn't find any reference in the LWUIT documentation but until I had refreshed my theme, my changes weren't shown. If you plan to change themes at runtime, you also have to refresh it. In the following three lines of code, the created theme is applied to the weather app:
res = Resources.open("/weather.res"); UIManager.getInstance().setThemeProps(res.getTheme (res.getThemeResourceNames()[0])); ... //Without doing this, the theme is not shown Display.getInstance().getCurrent().refreshTheme();
To load images from the resource file, you simply call the getImage method, and to change their size, call scaling methods, as shown here.
Label icon = new Label(resources.getImage(weatherRss.actualCondition.code + ".png").scaled(40, 40));
When you select your obfuscating level on project properties, don't set it very high or calls to Image.scaled() will fail, exiting your application. For example, I selected a level of 4. |
In the WeatherDisplay class, I show a scaled image for the "actual condition," and I use the normal size image for "forecast." And here you have final result:
Figure 9: A more polished screen
Figure 10: Good read on small screens
Page 6 of 7
This article was originally published on July 17, 2008