File: ArtworkUtils.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,968 kB
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (63 lines) | stat: -rw-r--r-- 2,028 bytes parent folder | download | duplicates (4)
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
package com.explodingpixels.macwidgets.plaf;

import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;

import com.explodingpixels.widgets.ImageUtils;

public class ArtworkUtils {

    private ArtworkUtils() {
        // utility class - no constructor needed.
    }

    public static ImageSet getImageSet(URL imageLocation) {
        Image image = new ImageIcon(imageLocation).getImage();

        // ensure that the given image is divisible by three along the horizontal axis.
        checkImageDivisibleByThree(image);

        int subImageWidth = image.getWidth(null) / 3;
        int imageHeight = image.getHeight(null);
        Image inactiveImage = ImageUtils.getSubImage(image, 0, 0, subImageWidth, imageHeight);
        Image activeImage = ImageUtils.getSubImage(image, subImageWidth, 0, subImageWidth, imageHeight);
        Image pressedImage = ImageUtils.getSubImage(image, subImageWidth * 2, 0, subImageWidth, imageHeight);

        return new ImageSet(inactiveImage, activeImage, pressedImage);
    }

    private static void checkImageDivisibleByThree(Image image) {
        if (image.getWidth(null) % 3 != 0) {
            throw new IllegalArgumentException(
                    "The given image should contain three sub-images all of the same size.");
        }
    }

    public static class ImageSet {

        private final ImageIcon fInactiveImage;
        private final ImageIcon fActiveImage;
        private final ImageIcon fPressedImage;

        private ImageSet(Image inactiveImage, Image activeImage, Image pressedImage) {
            fInactiveImage = new ImageIcon(inactiveImage);
            fActiveImage = new ImageIcon(activeImage);
            fPressedImage = new ImageIcon(pressedImage);
        }

        public ImageIcon getInactiveImage() {
            return fInactiveImage;
        }

        public ImageIcon getActiveImage() {
            return fActiveImage;
        }

        public ImageIcon getPressedImage() {
            return fPressedImage;
        }
    }

}