1 package org.argosfields.image;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.graphics.Device;
5 import org.eclipse.swt.graphics.Drawable;
6 import org.eclipse.swt.graphics.GC;
7 import org.eclipse.swt.graphics.Image;
8 import org.eclipse.swt.graphics.Rectangle;
9
10 /***
11 * ImageSet.java
12 *
13 * @author Xavier Cho
14 * @version $Revision: 1.2 $ $Date: 2003/10/23 14:59:56 $
15 */
16 public abstract class ImageSet {
17 protected abstract Image getImage();
18
19 public abstract int getColumns();
20
21 public abstract int getRows();
22
23 public abstract int getIconWidth();
24
25 public abstract int getIconHeight();
26
27 public final Image getIcon(final int index, final Device device) {
28 return getIcon(getIconBounds(index), device);
29 }
30
31 public final Image getIcon(final int x, final int y, final Device device) {
32 return getIcon(getIconBounds(x, y), device);
33 }
34
35 protected final Image getIcon(
36 final Rectangle bounds,
37 final Device device) {
38 if (bounds == null || device == null) {
39 SWT.error(SWT.ERROR_NULL_ARGUMENT);
40 }
41
42 Image image = null;
43 GC gc = null;
44
45 try {
46 image = new Image(device, bounds);
47 gc = new GC(image);
48
49 gc.drawImage(
50 getImage(),
51 bounds.x,
52 bounds.y,
53 bounds.width,
54 bounds.height,
55 0,
56 0,
57 bounds.width,
58 bounds.height);
59 } finally {
60 if (gc != null) {
61 gc.dispose();
62 }
63 }
64
65 return image;
66 }
67
68 public final void drawIcon(
69 final int index,
70 final Rectangle bounds,
71 final Drawable drawable,
72 final GC gc) {
73
74 drawIcon(getIconBounds(index), bounds, drawable, gc);
75 }
76
77 public final void drawIcon(
78 final int x,
79 final int y,
80 final Rectangle bounds,
81 final Drawable drawable,
82 final GC gc) {
83
84 drawIcon(getIconBounds(x, y), bounds, drawable, gc);
85 }
86
87 protected final void drawIcon(
88 final Rectangle src,
89 final Rectangle target,
90 final Drawable drawable,
91 final GC gc) {
92
93 if (target == null || drawable == null || gc == null) {
94 SWT.error(SWT.ERROR_NULL_ARGUMENT);
95 }
96
97 gc.drawImage(
98 getImage(),
99 src.x,
100 src.y,
101 src.width,
102 src.height,
103 target.x,
104 target.y,
105 target.width,
106 target.height);
107 }
108
109 public final Rectangle getIconBounds(final int index) {
110 int maxIndex = getRows() * getColumns();
111
112 if (index < 0 || index > maxIndex) {
113 SWT.error(SWT.ERROR_INVALID_RANGE);
114 }
115
116 int y = (int) (index / getColumns());
117 int x = index % getColumns();
118
119 return getIconBounds(x, y);
120 }
121
122 public final Rectangle getIconBounds(final int x, final int y) {
123 if (x < 0 || x >= getColumns()) {
124 SWT.error(SWT.ERROR_INVALID_RANGE);
125 } else if (y < 0 || y >= getRows()) {
126 SWT.error(SWT.ERROR_INVALID_RANGE);
127 }
128
129 Rectangle bounds =
130 new Rectangle(
131 x * getIconWidth(),
132 y * getIconHeight(),
133 getIconWidth(),
134 getIconHeight());
135
136 return bounds;
137 }
138 }
This page was automatically generated by Maven