Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ImageElement |
|
| 3.0;3 |
1 | package com.liquidatom.derbyscore.theme; | |
2 | ||
3 | import java.awt.Rectangle; | |
4 | import javax.annotation.concurrent.Immutable; | |
5 | import javax.annotation.concurrent.ThreadSafe; | |
6 | ||
7 | /** | |
8 | * Represents an image element within the theme. | |
9 | * | |
10 | * @author Russell Francis (russ@metro-six.com) | |
11 | */ | |
12 | @ThreadSafe | |
13 | @Immutable | |
14 | public class ImageElement extends ThemeElement { | |
15 | ||
16 | final private String imgRef; | |
17 | final private Rectangle position; | |
18 | ||
19 | /** | |
20 | * Construct a new {@code ImageElement} used to conditionally render an image as part of the theme. | |
21 | * | |
22 | * @param theme The theme which this element belongs too. | |
23 | * @param conditionJs A snippet of javascript code which if non-null will be evaluated to determine if the image | |
24 | * should be rendered. The javascript has access to the current {@link Bout} instance. | |
25 | * @param position An optional rectangle which defines the confines into which the image should be scaled and | |
26 | * rendered. If this has 0 width the width of the image will be used. If this has 0 height, the height of the | |
27 | * image will be used. If this is null the width and height will be taken from the referenced image. | |
28 | * @param imgRef The unique label for the image which we wish to render. | |
29 | */ | |
30 | public ImageElement(final Theme theme, final String conditionJs, final Rectangle position, final String imgRef) { | |
31 | 3 | super(theme, conditionJs); |
32 | 2 | this.imgRef = imgRef; |
33 | ||
34 | 2 | int x = (int) (position == null ? 0 : position.getX()); |
35 | 2 | int y = (int) (position == null ? 0 : position.getY()); |
36 | 2 | int width = (int) (position == null ? 0 : position.getWidth()); |
37 | 2 | if (width == 0) { |
38 | 1 | width = getTheme().getImage(imgRef).getWidth(); |
39 | } | |
40 | ||
41 | 2 | int height = (int) (position == null ? 0 : position.getHeight()); |
42 | 2 | if (height == 0) { |
43 | 1 | height = getTheme().getImage(imgRef).getHeight(); |
44 | } | |
45 | ||
46 | 2 | this.position = new Rectangle(x, y, width, height); |
47 | 2 | } |
48 | ||
49 | /** | |
50 | * Get the unique label used to identify the image that this element will render. | |
51 | * | |
52 | * @return The unique label used to identify the image that this element will render. | |
53 | */ | |
54 | public String getImgRef() { | |
55 | 1 | return imgRef; |
56 | } | |
57 | ||
58 | /** | |
59 | * Get a {@link Rectangle} describing the bounds which this image will be rendered into. | |
60 | * | |
61 | * @return A Rectangle describing the bounds which this image will be rendered into. | |
62 | */ | |
63 | @Override | |
64 | public Rectangle getPosition() { | |
65 | 7 | return (Rectangle) position.clone(); |
66 | } | |
67 | } |