Fork me on GitHub

Using Resources

Decrease your memory footprint by using resource wrapper

Resources

The JRebirth Framework provides an useful way to deal with your local resources, we currently support :

  • Colors
  • Fonts

These resources can consume a lot of memory if you don't dispose them 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.

UML Diagram Overview:

To manage these resources we use a enum hack to cleanly define them and most important to have a concise way to use them without calling singleton getter or another complex set of methods.

So if you want to handle resources with JRebirth mechanism, you just have to create an enumeration that implement the interface of the resource you want, with a custom constructor.

Color

For example to manage web color, (basic hexadecimal string #00CC00), you have to use this declaration:

38
39
40
41
42
/** The web color. */
ColorItemBase TEST_COLOR_WEB_1 = ColorItemBase.build(new WebColor("0088D3"));
 
/** The web color. */
ColorItemBase TEST_COLOR_WEB_2 = ColorItemBase.build(new WebColor("0D88D3", 0.4));
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * The class <strong>TestColors</strong>.
 *
 * @author Sébastien Bordes
 */
public interface TestColors {
 
    /**************************************************************************************/
    /** ___________________________________Web Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The web color. */
    ColorItemBase TEST_COLOR_WEB_1 = ColorItemBase.build(new WebColor("0088D3"));
 
    /** The web color. */
    ColorItemBase TEST_COLOR_WEB_2 = ColorItemBase.build(new WebColor("0D88D3", 0.4));
 
    /** The web color. */
    ColorItemBase TEST_COLOR_WEB_3 = ColorItemBase.build(new WebColor("0087D3", 1.0));
 
    /**************************************************************************************/
    /** __________________________________Gray Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The gray color. */
    ColorItemBase TEST_COLOR_GRAY_1 = ColorItemBase.build(new GrayColor(0.3));
 
    /** The gray color. */
    ColorItemBase TEST_COLOR_GRAY_2 = ColorItemBase.build(new GrayColor(0.74, 0.9));
 
    /** The gray color. */
    ColorItemBase TEST_COLOR_GRAY_3 = ColorItemBase.build(new GrayColor(0.5, 1.0));
 
    /**************************************************************************************/
    /** ___________________________________HSB Colors.___________________________________ */
    /**************************************************************************************/
 
    /** The hsb color. */
    ColorItemBase TEST_COLOR_HSB_1 = ColorItemBase.build(new HSBColor(96.0, 0.4, 0.9));
 
    /** The hsb color. */
    ColorItemBase TEST_COLOR_HSB_2 = ColorItemBase.build(new HSBColor(45.0, 0.3, 0.8, 0.45));
 
    /** The hsb color. */
    ColorItemBase TEST_COLOR_HSB_3 = ColorItemBase.build(new HSBColor(153.0, 0.6, 0.75, 1.0));
 
    /**************************************************************************************/
    /** _________________________________RGB 01 Colors.__________________________________ */
    /**************************************************************************************/
 
    /** The rgb 0-1 color. */
    ColorItemBase TEST_COLOR_RGB01_1 = ColorItemBase.build(new RGB01Color(0.22, 0.752, 0.78));
 
    /** The rgb 0-1 color. */
    ColorItemBase TEST_COLOR_RGB01_2 = ColorItemBase.build(new RGB01Color(0.78, 0.653, 0.85, 0.12));
 
    /** The rgb 0-1 color. */
    ColorItemBase TEST_COLOR_RGB01_3 = ColorItemBase.build(new RGB01Color(0.96, 0.851, 0.41, 1.0));
 
    /**************************************************************************************/
    /** ___________________________________RGB 255 Colors._______________________________ */
    /**************************************************************************************/
 
    /** The rgb 255 color. */
    ColorItemBase TEST_COLOR_RGB255_1 = ColorItemBase.build(new RGB255Color(107, 69, 251));
 
    /** The rgb 255 color. */
    ColorItemBase TEST_COLOR_RGB255_2 = ColorItemBase.build(new RGB255Color(255, 248, 189, 70 / 100));
 
    /** The rgb 255 color. */
    ColorItemBase TEST_COLOR_RGB255_3 = ColorItemBase.build(new RGB255Color(112, 60, 63, 1.0));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Get more info at : www.jrebirth.org .
 * Copyright JRebirth.org © 2011-2013
 * Contact : sebastien.bordes@jrebirth.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jrebirth.core.resource.color;
 
/**
 * The class <strong>WebColor</strong> used to create a Web Color.
 *
 * @author Sébastien Bordes
 */
public class WebColor extends AbstractBaseColor {
 
    /** The hexadecimal string value [0-9A-F]{6} . */
    private final String hex;
 
    /**
     * Default Constructor.
     *
     * @param hex the hexadecimal value [0-9A-F]{6}
     */
    public WebColor(final String hex) {
        super();
        this.hex = hex;
    }
 
    /**
     * Default Constructor.
     *
     * @param hex the hexadecimal value [0-9A-F]{6}
     * @param opacity the color opacity [0.0-1.0]
     */
    public WebColor(final String hex, final double opacity) {
        super(opacity);
        this.hex = hex;
    }
 
    /**
     * Return the hexadecimal string value [0-9A-F]{6}.
     *
     * @return Returns the hexadecimal value.
     */
    public String hex() {
        return this.hex;
    }
 
}

But this interface doesn't explain how to register a resource, so let's see an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * Get more info at : www.jrebirth.org .
 * Copyright JRebirth.org © 2011-2013
 * Contact : sebastien.bordes@jrebirth.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jrebirth.presentation;
 
import javafx.scene.paint.Color;
 
import org.jrebirth.core.resource.ResourceBuilders;
import org.jrebirth.core.resource.color.ColorBuilder;
import org.jrebirth.core.resource.color.ColorItem;
import org.jrebirth.core.resource.color.ColorParams;
import org.jrebirth.core.resource.color.RGB255Color;
import org.jrebirth.core.resource.color.WebColor;
 
/**
 * The class <strong>PrezColors</strong>.
 *
 * @author Sébastien Bordes
 *
 */
public enum PrezColors implements ColorItem {
 
    /** Color for slide title, white. */
    SLIDE_TITLE(new WebColor("FFFFFF", 1.0)),
 
    /** Color for blue shape, xxx. */
    SHAPE_BLUE(new WebColor("3495CE", 1.0)),
 
    /** Color for drop shadow, black. */
    DROP_SHADOW(new WebColor("000000", 0.8)),
    /** Color for inner shadow, white. */
    INNER_SHADOW(new WebColor("FFFFFE", 0.3)),
 
    /** Color for first gradient, xxx. */
    GRADIENT_1(new WebColor("1AA2AC", 1.0)),
    /** Color for second gradient, xxx. */
    GRADIENT_2(new WebColor("F04F24", 1.0)),
    /** Color for third gradient, xxxx. */
    GRADIENT_3(new WebColor("FFF200", 1.0)),
 
    /** Color for splash text, xxx. */
    SPLASH_TEXT(new RGB255Color(60, 60, 70));
 
    /**
     * Private Constructor.
     *
     * @param colorParams the primitive values for the color
     */
    private PrezColors(final ColorParams colorParams) {
        builder().storeParams(this, colorParams);
    }
 
    /**
     * {@inheritDoc}
     */
    @Override
    public Color get() {
        return builder().get(this);
    }
 
    /**
     * {@inheritDoc}
     */
    @Override
    public ColorBuilder builder() {
        return ResourceBuilders.COLOR_BUILDER;
    }
}

Font

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * The class <strong>TestFonts</strong>.
 *
 * @author Sébastien Bordes
 */
public interface TestFonts {
 
    /**************************************************************************************/
    /** ___________________________________Real Fonts.___________________________________ */
    /**************************************************************************************/
 
    /** The real font. */
    FontItem TEST_REAL_FONT_1 = FontItemBase.build(new RealFont(TestFontNames.Turtles, 10.0));
 
    /** The real font. */
    FontItem TEST_REAL_FONT_2 = FontItemBase.build(new RealFont(TestFontNames.Turtles, 12.0));
 
    /**************************************************************************************/
    /** _________________________________Family Fonts.___________________________________ */
    /**************************************************************************************/
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_1 = FontItemBase.build(new FamilyFont("serif", 10.0, FontWeight.BOLD, FontPosture.ITALIC));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_2 = FontItemBase.build(new FamilyFont("sansserif", 16.0, FontWeight.BOLD));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_3 = FontItemBase.build(new FamilyFont("monospaced", 17.0, FontPosture.ITALIC));
 
    /** The family font. */
    FontItem TEST_FAMILY_FONT_4 = FontItemBase.build(new FamilyFont("monospaced", 8.0));
 
}

Parameters