source: trunk/src/org/expeditee/gio/swing/SwingConversions.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 13.4 KB
Line 
1package org.expeditee.gio.swing;
2
3public class SwingConversions {
4
5 public static org.expeditee.core.Dimension fromSwingDimension(java.awt.Dimension dimension)
6 {
7 if (dimension == null) return null;
8
9 return new org.expeditee.core.Dimension(dimension.width, dimension.height);
10 }
11
12 public static java.awt.Dimension toSwingDimension(org.expeditee.core.Dimension dimension)
13 {
14 if (dimension == null) return null;
15
16 return new java.awt.Dimension(dimension.width, dimension.height);
17 }
18
19 public static java.awt.Color toSwingColor(org.expeditee.core.Colour c)
20 {
21 if (c == null) return null;
22
23 int r = c.getRed255();
24 int g = c.getGreen255();
25 int b = c.getBlue255();
26 int a = c.getAlpha255();
27
28 return new java.awt.Color(r, g, b, a);
29 }
30
31 public static org.expeditee.core.Colour fromSwingColor(java.awt.Color c)
32 {
33 if (c == null) return null;
34
35 return org.expeditee.core.Colour.FromRGBA255(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
36 }
37
38 public static java.awt.Point toSwingPoint(org.expeditee.core.Point point)
39 {
40 if (point == null) return null;
41
42 return new java.awt.Point(point.x, point.y);
43 }
44
45 public static org.expeditee.core.Point fromSwingPoint(java.awt.Point point)
46 {
47 if (point == null) return null;
48
49 return new org.expeditee.core.Point(point.x, point.y);
50 }
51
52 public static org.expeditee.core.Point fromSwingPoint(java.awt.geom.Point2D point)
53 {
54 if (point == null) return null;
55
56 return new org.expeditee.core.Point((int) point.getX(), (int) point.getY());
57 }
58
59 public static java.awt.Rectangle toSwingRectangle(org.expeditee.core.bounds.AxisAlignedBoxBounds b)
60 {
61 if (b == null) return null;
62
63 return new java.awt.Rectangle(b.getMinX(), b.getMinY(), b.getWidth(), b.getHeight());
64 }
65
66 public static org.expeditee.core.bounds.AxisAlignedBoxBounds fromSwingRectangle(java.awt.Rectangle r)
67 {
68 if (r == null) return null;
69
70 return new org.expeditee.core.bounds.AxisAlignedBoxBounds(r.x, r.y, r.width, r.height);
71 }
72
73 public static java.awt.font.TextHitInfo toSwingTextHitInfo(org.expeditee.core.TextHitInfo thi)
74 {
75 if (thi == null) return null;
76
77 if (thi.isLeadingEdge) {
78 return java.awt.font.TextHitInfo.leading(thi.charIndex);
79 } else {
80 return java.awt.font.TextHitInfo.trailing(thi.charIndex);
81 }
82 }
83
84 public static org.expeditee.core.TextHitInfo fromSwingTextHitInfo(java.awt.font.TextHitInfo swingThi)
85 {
86 if (swingThi == null) return null;
87
88 return new org.expeditee.core.TextHitInfo(swingThi.getCharIndex(), swingThi.isLeadingEdge());
89 }
90
91 public static int toSwingFontStyle(org.expeditee.core.Font.Style style)
92 {
93 switch (style) {
94 case PLAIN:
95 return java.awt.Font.PLAIN;
96 case BOLD:
97 return java.awt.Font.BOLD;
98 case ITALIC:
99 return java.awt.Font.ITALIC;
100 case BOLD_ITALIC:
101 return java.awt.Font.BOLD | java.awt.Font.ITALIC;
102 default:
103 return -1;
104 }
105 }
106
107 public static org.expeditee.core.Font.Style fromSwingFontStyle(int style)
108 {
109 switch (style) {
110 case java.awt.Font.BOLD | java.awt.Font.ITALIC:
111 return org.expeditee.core.Font.Style.BOLD_ITALIC;
112 case java.awt.Font.BOLD:
113 return org.expeditee.core.Font.Style.BOLD;
114 case java.awt.Font.ITALIC:
115 return org.expeditee.core.Font.Style.ITALIC;
116 case java.awt.Font.PLAIN:
117 return org.expeditee.core.Font.Style.PLAIN;
118 default:
119 return null;
120 }
121 }
122
123 public static int toSwingCursorType(org.expeditee.core.Cursor.CursorType type)
124 {
125 switch (type) {
126 case DEFAULT:
127 return java.awt.Cursor.DEFAULT_CURSOR;
128 case TEXT:
129 return java.awt.Cursor.TEXT_CURSOR;
130 case CROSSHAIR:
131 return java.awt.Cursor.CROSSHAIR_CURSOR;
132 default:
133 return java.awt.Cursor.CUSTOM_CURSOR;
134 }
135 }
136
137 public static org.expeditee.core.Cursor.CursorType fromSwingCursorType(int type)
138 {
139 switch (type) {
140 case java.awt.Cursor.DEFAULT_CURSOR:
141 return org.expeditee.core.Cursor.CursorType.DEFAULT;
142 case java.awt.Cursor.TEXT_CURSOR:
143 return org.expeditee.core.Cursor.CursorType.TEXT;
144 case java.awt.Cursor.CROSSHAIR_CURSOR:
145 return org.expeditee.core.Cursor.CursorType.CROSSHAIR;
146 case java.awt.Cursor.CUSTOM_CURSOR:
147 return org.expeditee.core.Cursor.CursorType.CUSTOM;
148 default:
149 return null;
150 }
151 }
152
153 public static org.expeditee.core.GradientFill fromSwingGradientPaint(java.awt.GradientPaint paint)
154 {
155 if (paint == null) return null;
156
157 org.expeditee.core.Colour colour1 = fromSwingColor(paint.getColor1());
158 org.expeditee.core.Point point1 = fromSwingPoint(paint.getPoint1());
159 org.expeditee.core.Colour colour2 = fromSwingColor(paint.getColor2());
160 org.expeditee.core.Point point2 = fromSwingPoint(paint.getPoint2());
161
162 return new org.expeditee.core.GradientFill(colour1, point1, colour2, point2, paint.isCyclic());
163 }
164
165 public static java.awt.GradientPaint toSwingGradientPaint(org.expeditee.core.GradientFill fill)
166 {
167 if (fill == null) return null;
168
169 java.awt.Color color1 = toSwingColor(fill.getFromColour());
170 java.awt.Color color2 = toSwingColor(fill.getToColour());
171
172 return new java.awt.GradientPaint(fill.getFromPoint().x, fill.getFromPoint().y, color1, fill.getToPoint().x, fill.getToPoint().y, color2, !fill.isAcyclic());
173 }
174
175 public static org.expeditee.core.Stroke fromSwingStroke(java.awt.BasicStroke basicStroke)
176 {
177 if (basicStroke == null) return null;
178
179 org.expeditee.core.Stroke stroke = new org.expeditee.core.Stroke();
180 stroke.thickness = basicStroke.getLineWidth();
181 stroke.capType = fromSwingCapType(basicStroke.getEndCap());
182 stroke.joinType = fromSwingJoinType(basicStroke.getLineJoin());
183 stroke.dashArray = basicStroke.getDashArray().clone();
184 stroke.dashPhase = basicStroke.getDashPhase();
185
186 return stroke;
187 }
188
189 public static java.awt.BasicStroke toSwingStroke(org.expeditee.core.Stroke stroke)
190 {
191 if (stroke == null) return null;
192
193 java.awt.BasicStroke basicStroke = new java.awt.BasicStroke(stroke.thickness,
194 toSwingCapType(stroke.capType),
195 toSwingJoinType(stroke.joinType),
196 10.0f, // This is the default for miterLimit
197 stroke.dashArray.clone(),
198 stroke.dashPhase);
199
200 return basicStroke;
201 }
202
203 public static org.expeditee.core.Stroke.CAP fromSwingCapType(int swingCapType)
204 {
205 switch(swingCapType) {
206 case java.awt.BasicStroke.CAP_BUTT:
207 return org.expeditee.core.Stroke.CAP.BUTT;
208 case java.awt.BasicStroke.CAP_ROUND:
209 return org.expeditee.core.Stroke.CAP.ROUND;
210 case java.awt.BasicStroke.CAP_SQUARE:
211 return org.expeditee.core.Stroke.CAP.SQUARE;
212 default:
213 return null;
214 }
215 }
216
217 public static int toSwingCapType(org.expeditee.core.Stroke.CAP cap)
218 {
219 switch(cap) {
220 case BUTT:
221 return java.awt.BasicStroke.CAP_BUTT;
222 case ROUND:
223 return java.awt.BasicStroke.CAP_ROUND;
224 case SQUARE:
225 return java.awt.BasicStroke.CAP_SQUARE;
226 default:
227 return -1;
228 }
229 }
230
231 public static org.expeditee.core.Stroke.JOIN fromSwingJoinType(int swingJoinType)
232 {
233 switch(swingJoinType) {
234 case java.awt.BasicStroke.JOIN_BEVEL:
235 return org.expeditee.core.Stroke.JOIN.BEVEL;
236 case java.awt.BasicStroke.JOIN_MITER:
237 return org.expeditee.core.Stroke.JOIN.MITER;
238 case java.awt.BasicStroke.JOIN_ROUND:
239 return org.expeditee.core.Stroke.JOIN.ROUND;
240 default:
241 return null;
242 }
243 }
244
245 public static int toSwingJoinType(org.expeditee.core.Stroke.JOIN join)
246 {
247 switch(join) {
248 case BEVEL:
249 return java.awt.BasicStroke.JOIN_BEVEL;
250 case MITER:
251 return java.awt.BasicStroke.JOIN_MITER;
252 case ROUND:
253 return java.awt.BasicStroke.JOIN_ROUND;
254 default:
255 return -1;
256 }
257 }
258
259 public static org.expeditee.core.bounds.PolygonBounds fromSwingPolygon(java.awt.Polygon polygon)
260 {
261 org.expeditee.core.bounds.PolygonBounds polygonBounds = new org.expeditee.core.bounds.PolygonBounds();
262
263 for (int i = 0; i < polygon.npoints; i++) {
264 polygonBounds.addPoint(polygon.xpoints[i], polygon.ypoints[i]);
265 }
266
267 return polygonBounds;
268 }
269
270 public static java.awt.Polygon toSwingPolygon(org.expeditee.core.bounds.PolygonBounds polygonBounds)
271 {
272 java.awt.Polygon polygon = new java.awt.Polygon();
273
274 for (int i = 0; i < polygonBounds.getPointCount(); i++) {
275 org.expeditee.core.Point p = polygonBounds.getPoint(i);
276 polygon.addPoint(p.x, p.y);
277 }
278
279 return polygon;
280 }
281
282 public static int toSwingMouseButton(org.expeditee.gio.input.KBMInputEvent.MouseButton mouseButton)
283 {
284 switch (mouseButton) {
285 case LEFT:
286 return java.awt.event.MouseEvent.BUTTON1;
287 case MIDDLE:
288 return java.awt.event.MouseEvent.BUTTON2;
289 case RIGHT:
290 return java.awt.event.MouseEvent.BUTTON3;
291 default:
292 return java.awt.event.MouseEvent.NOBUTTON;
293 }
294 }
295
296 public static org.expeditee.gio.input.KBMInputEvent.Key fromSwingVirtualKey(int swingKey)
297 {
298 switch (swingKey) {
299 case java.awt.event.KeyEvent.VK_SHIFT: return org.expeditee.gio.input.KBMInputEvent.Key.SHIFT;
300 case java.awt.event.KeyEvent.VK_CONTROL: return org.expeditee.gio.input.KBMInputEvent.Key.CTRL;
301 case java.awt.event.KeyEvent.VK_ALT: return org.expeditee.gio.input.KBMInputEvent.Key.ALT;
302 case java.awt.event.KeyEvent.VK_ESCAPE: return org.expeditee.gio.input.KBMInputEvent.Key.ESC;
303 case java.awt.event.KeyEvent.VK_F1: return org.expeditee.gio.input.KBMInputEvent.Key.F1;
304 case java.awt.event.KeyEvent.VK_F2: return org.expeditee.gio.input.KBMInputEvent.Key.F2;
305 case java.awt.event.KeyEvent.VK_F3: return org.expeditee.gio.input.KBMInputEvent.Key.F3;
306 case java.awt.event.KeyEvent.VK_F4: return org.expeditee.gio.input.KBMInputEvent.Key.F4;
307 case java.awt.event.KeyEvent.VK_F5: return org.expeditee.gio.input.KBMInputEvent.Key.F5;
308 case java.awt.event.KeyEvent.VK_F6: return org.expeditee.gio.input.KBMInputEvent.Key.F6;
309 case java.awt.event.KeyEvent.VK_F7: return org.expeditee.gio.input.KBMInputEvent.Key.F7;
310 case java.awt.event.KeyEvent.VK_F8: return org.expeditee.gio.input.KBMInputEvent.Key.F8;
311 case java.awt.event.KeyEvent.VK_F9: return org.expeditee.gio.input.KBMInputEvent.Key.F9;
312 case java.awt.event.KeyEvent.VK_F10: return org.expeditee.gio.input.KBMInputEvent.Key.F10;
313 case java.awt.event.KeyEvent.VK_F11: return org.expeditee.gio.input.KBMInputEvent.Key.F11;
314 case java.awt.event.KeyEvent.VK_F12: return org.expeditee.gio.input.KBMInputEvent.Key.F12;
315 case java.awt.event.KeyEvent.VK_UP: return org.expeditee.gio.input.KBMInputEvent.Key.UP_ARROW;
316 case java.awt.event.KeyEvent.VK_DOWN: return org.expeditee.gio.input.KBMInputEvent.Key.DOWN_ARROW;
317 case java.awt.event.KeyEvent.VK_LEFT: return org.expeditee.gio.input.KBMInputEvent.Key.LEFT_ARROW;
318 case java.awt.event.KeyEvent.VK_RIGHT: return org.expeditee.gio.input.KBMInputEvent.Key.RIGHT_ARROW;
319 case java.awt.event.KeyEvent.VK_HOME: return org.expeditee.gio.input.KBMInputEvent.Key.HOME;
320 case java.awt.event.KeyEvent.VK_END: return org.expeditee.gio.input.KBMInputEvent.Key.END;
321 case java.awt.event.KeyEvent.VK_DELETE: return org.expeditee.gio.input.KBMInputEvent.Key.DELETE;
322 case java.awt.event.KeyEvent.VK_PAGE_UP: return org.expeditee.gio.input.KBMInputEvent.Key.PAGE_UP;
323 case java.awt.event.KeyEvent.VK_PAGE_DOWN: return org.expeditee.gio.input.KBMInputEvent.Key.PAGE_DOWN;
324 case java.awt.event.KeyEvent.VK_BACK_SPACE: return org.expeditee.gio.input.KBMInputEvent.Key.BACKSPACE;
325 case java.awt.event.KeyEvent.VK_TAB: return org.expeditee.gio.input.KBMInputEvent.Key.TAB;
326 case java.awt.event.KeyEvent.VK_A: return org.expeditee.gio.input.KBMInputEvent.Key.A;
327 case java.awt.event.KeyEvent.VK_B: return org.expeditee.gio.input.KBMInputEvent.Key.B;
328 case java.awt.event.KeyEvent.VK_C: return org.expeditee.gio.input.KBMInputEvent.Key.C;
329 case java.awt.event.KeyEvent.VK_D: return org.expeditee.gio.input.KBMInputEvent.Key.D;
330 case java.awt.event.KeyEvent.VK_E: return org.expeditee.gio.input.KBMInputEvent.Key.E;
331 case java.awt.event.KeyEvent.VK_F: return org.expeditee.gio.input.KBMInputEvent.Key.F;
332 case java.awt.event.KeyEvent.VK_G: return org.expeditee.gio.input.KBMInputEvent.Key.G;
333 case java.awt.event.KeyEvent.VK_H: return org.expeditee.gio.input.KBMInputEvent.Key.H;
334 case java.awt.event.KeyEvent.VK_I: return org.expeditee.gio.input.KBMInputEvent.Key.I;
335 case java.awt.event.KeyEvent.VK_J: return org.expeditee.gio.input.KBMInputEvent.Key.J;
336 case java.awt.event.KeyEvent.VK_K: return org.expeditee.gio.input.KBMInputEvent.Key.K;
337 case java.awt.event.KeyEvent.VK_L: return org.expeditee.gio.input.KBMInputEvent.Key.L;
338 case java.awt.event.KeyEvent.VK_M: return org.expeditee.gio.input.KBMInputEvent.Key.M;
339 case java.awt.event.KeyEvent.VK_N: return org.expeditee.gio.input.KBMInputEvent.Key.N;
340 case java.awt.event.KeyEvent.VK_O: return org.expeditee.gio.input.KBMInputEvent.Key.O;
341 case java.awt.event.KeyEvent.VK_P: return org.expeditee.gio.input.KBMInputEvent.Key.P;
342 case java.awt.event.KeyEvent.VK_Q: return org.expeditee.gio.input.KBMInputEvent.Key.Q;
343 case java.awt.event.KeyEvent.VK_R: return org.expeditee.gio.input.KBMInputEvent.Key.R;
344 case java.awt.event.KeyEvent.VK_S: return org.expeditee.gio.input.KBMInputEvent.Key.S;
345 case java.awt.event.KeyEvent.VK_T: return org.expeditee.gio.input.KBMInputEvent.Key.T;
346 case java.awt.event.KeyEvent.VK_U: return org.expeditee.gio.input.KBMInputEvent.Key.U;
347 case java.awt.event.KeyEvent.VK_V: return org.expeditee.gio.input.KBMInputEvent.Key.V;
348 case java.awt.event.KeyEvent.VK_W: return org.expeditee.gio.input.KBMInputEvent.Key.W;
349 case java.awt.event.KeyEvent.VK_X: return org.expeditee.gio.input.KBMInputEvent.Key.X;
350 case java.awt.event.KeyEvent.VK_Y: return org.expeditee.gio.input.KBMInputEvent.Key.Y;
351 case java.awt.event.KeyEvent.VK_Z: return org.expeditee.gio.input.KBMInputEvent.Key.Z;
352 default: return null;
353 }
354 }
355}
Note: See TracBrowser for help on using the repository browser.