This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
/
mxGraphics2DCanvas.java
621 lines (534 loc) · 14.6 KB
/
mxGraphics2DCanvas.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.canvas;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.util.HashMap;
import java.util.Map;
import javax.swing.CellRendererPane;
import com.mxgraph.shape.mxActorShape;
import com.mxgraph.shape.mxArrowShape;
import com.mxgraph.shape.mxCloudShape;
import com.mxgraph.shape.mxConnectorShape;
import com.mxgraph.shape.mxCurveShape;
import com.mxgraph.shape.mxCylinderShape;
import com.mxgraph.shape.mxDefaultTextShape;
import com.mxgraph.shape.mxDoubleEllipseShape;
import com.mxgraph.shape.mxDoubleRectangleShape;
import com.mxgraph.shape.mxEllipseShape;
import com.mxgraph.shape.mxHexagonShape;
import com.mxgraph.shape.mxHtmlTextShape;
import com.mxgraph.shape.mxIShape;
import com.mxgraph.shape.mxITextShape;
import com.mxgraph.shape.mxImageShape;
import com.mxgraph.shape.mxLabelShape;
import com.mxgraph.shape.mxLineShape;
import com.mxgraph.shape.mxRectangleShape;
import com.mxgraph.shape.mxRhombusShape;
import com.mxgraph.shape.mxStencilRegistry;
import com.mxgraph.shape.mxSwimlaneShape;
import com.mxgraph.shape.mxTriangleShape;
import com.mxgraph.swing.util.mxSwingConstants;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
/**
* An implementation of a canvas that uses Graphics2D for painting.
*/
public class mxGraphics2DCanvas extends mxBasicCanvas
{
/**
*
*/
public static final String TEXT_SHAPE_DEFAULT = "default";
/**
*
*/
public static final String TEXT_SHAPE_HTML = "html";
/**
* Specifies the image scaling quality. Default is Image.SCALE_SMOOTH.
*/
public static int IMAGE_SCALING = Image.SCALE_SMOOTH;
/**
* Maps from names to mxIVertexShape instances.
*/
protected static Map<String, mxIShape> shapes = new HashMap<String, mxIShape>();
/**
* Maps from names to mxITextShape instances. There are currently three different
* hardcoded text shapes available here: default, html and wrapped.
*/
protected static Map<String, mxITextShape> textShapes = new HashMap<String, mxITextShape>();
/**
* Static initializer.
*/
static
{
putShape(mxConstants.SHAPE_ACTOR, new mxActorShape());
putShape(mxConstants.SHAPE_ARROW, new mxArrowShape());
putShape(mxConstants.SHAPE_CLOUD, new mxCloudShape());
putShape(mxConstants.SHAPE_CONNECTOR, new mxConnectorShape());
putShape(mxConstants.SHAPE_CYLINDER, new mxCylinderShape());
putShape(mxConstants.SHAPE_CURVE, new mxCurveShape());
putShape(mxConstants.SHAPE_DOUBLE_RECTANGLE, new mxDoubleRectangleShape());
putShape(mxConstants.SHAPE_DOUBLE_ELLIPSE, new mxDoubleEllipseShape());
putShape(mxConstants.SHAPE_ELLIPSE, new mxEllipseShape());
putShape(mxConstants.SHAPE_HEXAGON, new mxHexagonShape());
putShape(mxConstants.SHAPE_IMAGE, new mxImageShape());
putShape(mxConstants.SHAPE_LABEL, new mxLabelShape());
putShape(mxConstants.SHAPE_LINE, new mxLineShape());
putShape(mxConstants.SHAPE_RECTANGLE, new mxRectangleShape());
putShape(mxConstants.SHAPE_RHOMBUS, new mxRhombusShape());
putShape(mxConstants.SHAPE_SWIMLANE, new mxSwimlaneShape());
putShape(mxConstants.SHAPE_TRIANGLE, new mxTriangleShape());
putTextShape(TEXT_SHAPE_DEFAULT, new mxDefaultTextShape());
putTextShape(TEXT_SHAPE_HTML, new mxHtmlTextShape());
}
/**
* Optional renderer pane to be used for HTML label rendering.
*/
protected CellRendererPane rendererPane;
/**
* Global graphics handle to the image.
*/
protected Graphics2D g;
/**
* Constructs a new graphics canvas with an empty graphics object.
*/
public mxGraphics2DCanvas()
{
this(null);
}
/**
* Constructs a new graphics canvas for the given graphics object.
*/
public mxGraphics2DCanvas(Graphics2D g)
{
this.g = g;
// Initializes the cell renderer pane for drawing HTML markup
try
{
rendererPane = new CellRendererPane();
}
catch (Exception e)
{
// ignore
}
}
/**
*
*/
public static void putShape(String name, mxIShape shape)
{
shapes.put(name, shape);
}
/**
*
*/
public mxIShape getShape(Map<String, Object> style)
{
String name = mxUtils.getString(style, mxConstants.STYLE_SHAPE, null);
mxIShape shape = shapes.get(name);
if (shape == null && name != null)
{
shape = mxStencilRegistry.getStencil(name);
}
return shape;
}
/**
*
*/
public static void putTextShape(String name, mxITextShape shape)
{
textShapes.put(name, shape);
}
/**
*
*/
public mxITextShape getTextShape(Map<String, Object> style, boolean html)
{
String name;
if (html)
{
name = TEXT_SHAPE_HTML;
}
else
{
name = TEXT_SHAPE_DEFAULT;
}
return textShapes.get(name);
}
/**
*
*/
public CellRendererPane getRendererPane()
{
return rendererPane;
}
/**
* Returns the graphics object for this canvas.
*/
public Graphics2D getGraphics()
{
return g;
}
/**
* Sets the graphics object for this canvas.
*/
public void setGraphics(Graphics2D g)
{
this.g = g;
}
/*
* (non-Javadoc)
* @see com.mxgraph.canvas.mxICanvas#drawCell()
*/
public Object drawCell(mxCellState state)
{
Map<String, Object> style = state.getStyle();
mxIShape shape = getShape(style);
if (g != null && shape != null)
{
// Creates a temporary graphics instance for drawing this shape
float opacity = mxUtils.getFloat(style, mxConstants.STYLE_OPACITY,
100);
Graphics2D previousGraphics = g;
g = createTemporaryGraphics(style, opacity, state);
// Paints the shape and restores the graphics object
shape.paintShape(this, state);
g.dispose();
g = previousGraphics;
}
return shape;
}
/*
* (non-Javadoc)
* @see com.mxgraph.canvas.mxICanvas#drawLabel()
*/
public Object drawLabel(String text, mxCellState state, boolean html)
{
Map<String, Object> style = state.getStyle();
mxITextShape shape = getTextShape(style, html);
if (g != null && shape != null && drawLabels && text != null
&& text.length() > 0)
{
// Creates a temporary graphics instance for drawing this shape
float opacity = mxUtils.getFloat(style,
mxConstants.STYLE_TEXT_OPACITY, 100);
Graphics2D previousGraphics = g;
g = createTemporaryGraphics(style, opacity, null);
// Draws the label background and border
Color bg = mxUtils.getColor(style,
mxConstants.STYLE_LABEL_BACKGROUNDCOLOR);
Color border = mxUtils.getColor(style,
mxConstants.STYLE_LABEL_BORDERCOLOR);
paintRectangle(state.getLabelBounds().getRectangle(), bg, border);
// Paints the label and restores the graphics object
shape.paintShape(this, text, state, style);
g.dispose();
g = previousGraphics;
}
return shape;
}
/**
*
*/
public void drawImage(Rectangle bounds, String imageUrl)
{
drawImage(bounds, imageUrl, PRESERVE_IMAGE_ASPECT, false, false);
}
/**
*
*/
public void drawImage(Rectangle bounds, String imageUrl,
boolean preserveAspect, boolean flipH, boolean flipV)
{
if (imageUrl != null && bounds.getWidth() > 0 && bounds.getHeight() > 0)
{
Image img = loadImage(imageUrl);
if (img != null)
{
int w, h;
int x = bounds.x;
int y = bounds.y;
Dimension size = getImageSize(img);
if (preserveAspect)
{
double s = Math.min(bounds.width / (double) size.width,
bounds.height / (double) size.height);
w = (int) (size.width * s);
h = (int) (size.height * s);
x += (bounds.width - w) / 2;
y += (bounds.height - h) / 2;
}
else
{
w = bounds.width;
h = bounds.height;
}
Image scaledImage = (w == size.width && h == size.height) ? img
: img.getScaledInstance(w, h, IMAGE_SCALING);
if (scaledImage != null)
{
AffineTransform af = null;
if (flipH || flipV)
{
af = g.getTransform();
int sx = 1;
int sy = 1;
int dx = 0;
int dy = 0;
if (flipH)
{
sx = -1;
dx = -w - 2 * x;
}
if (flipV)
{
sy = -1;
dy = -h - 2 * y;
}
g.scale(sx, sy);
g.translate(dx, dy);
}
drawImageImpl(scaledImage, x, y);
// Restores the previous transform
if (af != null)
{
g.setTransform(af);
}
}
}
}
}
/**
* Implements the actual graphics call.
*/
protected void drawImageImpl(Image image, int x, int y)
{
g.drawImage(image, x, y, null);
}
/**
* Returns the size for the given image.
*/
protected Dimension getImageSize(Image image)
{
return new Dimension(image.getWidth(null), image.getHeight(null));
}
/**
*
*/
public void paintPolyline(mxPoint[] points, boolean rounded)
{
if (points != null && points.length > 1)
{
mxPoint pt = points[0];
mxPoint pe = points[points.length - 1];
double arcSize = mxConstants.LINE_ARCSIZE * scale;
GeneralPath path = new GeneralPath();
path.moveTo((float) pt.getX(), (float) pt.getY());
// Draws the line segments
for (int i = 1; i < points.length - 1; i++)
{
mxPoint tmp = points[i];
double dx = pt.getX() - tmp.getX();
double dy = pt.getY() - tmp.getY();
if ((rounded && i < points.length - 1) && (dx != 0 || dy != 0))
{
// Draws a line from the last point to the current
// point with a spacing of size off the current point
// into direction of the last point
double dist = Math.sqrt(dx * dx + dy * dy);
double nx1 = dx * Math.min(arcSize, dist / 2) / dist;
double ny1 = dy * Math.min(arcSize, dist / 2) / dist;
double x1 = tmp.getX() + nx1;
double y1 = tmp.getY() + ny1;
path.lineTo((float) x1, (float) y1);
// Draws a curve from the last point to the current
// point with a spacing of size off the current point
// into direction of the next point
mxPoint next = points[i + 1];
// Uses next non-overlapping point
while (i < points.length - 2 && Math.round(next.getX() - tmp.getX()) == 0 && Math.round(next.getY() - tmp.getY()) == 0)
{
next = points[i + 2];
i++;
}
dx = next.getX() - tmp.getX();
dy = next.getY() - tmp.getY();
dist = Math.max(1, Math.sqrt(dx * dx + dy * dy));
double nx2 = dx * Math.min(arcSize, dist / 2) / dist;
double ny2 = dy * Math.min(arcSize, dist / 2) / dist;
double x2 = tmp.getX() + nx2;
double y2 = tmp.getY() + ny2;
path.quadTo((float) tmp.getX(), (float) tmp.getY(),
(float) x2, (float) y2);
tmp = new mxPoint(x2, y2);
}
else
{
path.lineTo((float) tmp.getX(), (float) tmp.getY());
}
pt = tmp;
}
path.lineTo((float) pe.getX(), (float) pe.getY());
g.draw(path);
}
}
/**
*
*/
public void paintRectangle(Rectangle bounds, Color background, Color border)
{
if (background != null)
{
g.setColor(background);
fillShape(bounds);
}
if (border != null)
{
g.setColor(border);
g.draw(bounds);
}
}
/**
*
*/
public void fillShape(Shape shape)
{
fillShape(shape, false);
}
/**
*
*/
public void fillShape(Shape shape, boolean shadow)
{
int shadowOffsetX = (shadow) ? mxConstants.SHADOW_OFFSETX : 0;
int shadowOffsetY = (shadow) ? mxConstants.SHADOW_OFFSETY : 0;
if (shadow)
{
// Saves the state and configures the graphics object
Paint p = g.getPaint();
Color previousColor = g.getColor();
g.setColor(mxSwingConstants.SHADOW_COLOR);
g.translate(shadowOffsetX, shadowOffsetY);
// Paints the shadow
fillShape(shape, false);
// Restores the state of the graphics object
g.translate(-shadowOffsetX, -shadowOffsetY);
g.setColor(previousColor);
g.setPaint(p);
}
g.fill(shape);
}
/**
*
*/
public Stroke createStroke(Map<String, Object> style)
{
double width = mxUtils
.getFloat(style, mxConstants.STYLE_STROKEWIDTH, 1) * scale;
boolean dashed = mxUtils.isTrue(style, mxConstants.STYLE_DASHED);
if (dashed)
{
float[] dashPattern = mxUtils.getFloatArray(style,
mxConstants.STYLE_DASH_PATTERN,
mxConstants.DEFAULT_DASHED_PATTERN, " ");
float[] scaledDashPattern = new float[dashPattern.length];
for (int i = 0; i < dashPattern.length; i++)
{
scaledDashPattern[i] = (float) (dashPattern[i] * scale * width);
}
return new BasicStroke((float) width, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, scaledDashPattern, 0.0f);
}
else
{
return new BasicStroke((float) width);
}
}
/**
*
*/
public Paint createFillPaint(mxRectangle bounds, Map<String, Object> style)
{
Color fillColor = mxUtils.getColor(style, mxConstants.STYLE_FILLCOLOR);
Paint fillPaint = null;
if (fillColor != null)
{
Color gradientColor = mxUtils.getColor(style,
mxConstants.STYLE_GRADIENTCOLOR);
if (gradientColor != null)
{
String gradientDirection = mxUtils.getString(style,
mxConstants.STYLE_GRADIENT_DIRECTION);
float x1 = (float) bounds.getX();
float y1 = (float) bounds.getY();
float x2 = (float) bounds.getX();
float y2 = (float) bounds.getY();
if (gradientDirection == null
|| gradientDirection
.equals(mxConstants.DIRECTION_SOUTH))
{
y2 = (float) (bounds.getY() + bounds.getHeight());
}
else if (gradientDirection.equals(mxConstants.DIRECTION_EAST))
{
x2 = (float) (bounds.getX() + bounds.getWidth());
}
else if (gradientDirection.equals(mxConstants.DIRECTION_NORTH))
{
y1 = (float) (bounds.getY() + bounds.getHeight());
}
else if (gradientDirection.equals(mxConstants.DIRECTION_WEST))
{
x1 = (float) (bounds.getX() + bounds.getWidth());
}
fillPaint = new GradientPaint(x1, y1, fillColor, x2, y2,
gradientColor, true);
}
}
return fillPaint;
}
/**
*
*/
public Graphics2D createTemporaryGraphics(Map<String, Object> style,
float opacity, mxRectangle bounds)
{
Graphics2D temporaryGraphics = (Graphics2D) g.create();
// Applies the default translate
temporaryGraphics.translate(translate.x, translate.y);
// Applies the rotation on the graphics object
if (bounds != null)
{
double rotation = mxUtils.getDouble(style,
mxConstants.STYLE_ROTATION, 0);
if (rotation != 0)
{
temporaryGraphics.rotate(Math.toRadians(rotation),
bounds.getCenterX(), bounds.getCenterY());
}
}
// Applies the opacity to the graphics object
if (opacity != 100)
{
temporaryGraphics.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, opacity / 100));
}
return temporaryGraphics;
}
}