Declare static ResourceItem fields with Resources.create(ResourceParam) (images, fonts, CSS, FXML). Call .get() for cached heavyweight objects stored weakly. Prefer interface “enum” groupings and @Preload for fonts referenced from CSS.
Resources are part of core. Declare typed ResourceItem constants and call .get() to obtain cached heavyweight objects (images, fonts, CSS, FXML).
<dependency>
<groupId>org.jrebirth.af</groupId>
<artifactId>core</artifactId>
<version>${jrebirth.version}</version>
</dependency>
Minimal example — load an image and a stylesheet:
import static org.jrebirth.af.core.resource.Resources.create;
public interface AppResources {
ImageItem LOGO = create(new RelImage("images/logo.png"));
StyleSheetItem MAIN = create(new StyleSheet("application"));
}
// In Application.preInit() or View.initView():
Image logo = AppResources.LOGO.get();
addCSS(scene, AppResources.MAIN);
Resources are stored weakly and rebuilt on demand when memory is reclaimed.
The JRebirth Framework provides an useful way to deal with your local resources, we currently support :
These resources can consume a lot of memory if you don't dispose them (especially big images and fonts) when you stop using them. JRebirth provides a mechanism to store them weakly and to rebuild them if necessary in order to use the less memory as required. To do this JRebirth stores a ResourceParam lightweight object that contains all information to create the heavyweight resource. This one is linked by a ResourceItem to facilitate its usage.
UML Diagram Overview:
Heavyweight resource objects are retrieved by calling the get() method of ResourceItem.
If the resource was already loaded it is retrieved from cache; otherwise it is loaded and stored weakly.
When the system is low on memory, resources not retained by a Component or a Scene node become eligible for garbage collection.
// Declarative — preferred for static assets
ColorItem accent = Resources.create(new WebColor("0088D3"));
Color c = accent.get();
// Dynamic — anonymous ResourceItem, still cached by path
Image dynamic = Resources.create(new RelImage(item.getImagePath())).get();
There are 3 ways to declare resources, each one is available for all kind of resources with special feature for parameters.
The first way is to hold static field declaration instantiated with custom ‘factory’ Resources (not named, with an overloaded create method to avoid later cast). This static fields can be hold in any class you want but we recommend to store them into an Interface used ‘as’ an enum. It's better to have an interface per resource type to avoid big file that blends all resources. Another interesting trick, is to add a static import on related static Resource.create method to shorten the resource declaration (Ctrl+Shift+M with Eclipse).
Static import declaration:
import static org.jrebirth.af.core.resource.Resources.create;
Example of Web color declaration:
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));
The other way to declare is a little bit complex, it implies to create an enum that implements a ResourceItem interface. But it requires to add use a custom syntax to use default method.
public enum EnumColors implements ColorEnum {
// @formatter:off
/** Color for slide title, white. */
SLIDE_TITLE {{ web("FFFFFF", 1.0); }},
/** Color for blue shape, xxx. */
SHAPE_BLUE{{ web("3495CE", 1.0); }},
/** Color for drop shadow, black. */
DROP_SHADOW{{ web("000000", 0.8); }},
/** Color for inner shadow, white. */
INNER_SHADOW{{ web("FFFFFE", 0.3); }};
}
Severals default methods exist, at least one per ResourceParams to create.
We can load dynamicalli resource by creating “anonymous” ResourceItem, the resource will be kept within cache if memory is not constrained otherwise it will be deleted. Another call with the same path will hit the cache and the image will be only stored once, but created as many times as deleted and retrieved again.
final Image image = Resources.create(new RelImage(item.getImage())).get();
In this sample item.getImage() return the relative srtring path of the image.
Resource enums annotated with @Preload are automatically loaded during application startup (in the preInit phase). This is useful for fonts that CSS references — they must be available before stylesheets are applied.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Preload {
}
For example to manage web color, (basic hexadecimal string #00CC00), you have to use this declaration:
ColorItem TEST_COLOR_WEB_1 = create(new WebColor("0088D3"));
For example to manage the Turtles font, you have to use this declaration:
FontItem TEST_REAL_FONT_1 = create(new RealFont(TestFontNames.Turtles, 10.0));
Hereafter the enumeration that stores font names.
public enum TestFontNames implements FontName {
/** Turtles. */
Turtles,
/** Report 1942. */
Report_1942,
}
Stylesheets are declared as StyleSheetItem and loaded via addCSS():
public interface SampleStyles {
/** The application main style sheet. */
StyleSheetItem MAIN = create(new StyleSheet("sample"));
}
Apply to the scene from your Application class:
@Override
protected void preInit() {
addCSS(getScene(), SampleStyles.MAIN);
}
Or apply locally from a View:
@Override
protected void initView() {
addCSS(SampleStyles.LOCAL);
}
import static org.jrebirth.af.core.resource.Resources.create;
ImageItem BANNER = create(new RelImage("images/banner.png"));
// Usage:
Image img = BANNER.get();
myImageView.setImage(img);
Relative paths resolve from the declaring class's package on the classpath.
These resource types (AudioClipItem, MediaItem) are defined in the JRebirth API but not yet implemented in core. They are planned for a future release and will follow the same ResourceItem/ResourceParam pattern as other resources.