Skip to content

Commit 7ca810e

Browse files
committed
figure drawing method
1 parent d5f03af commit 7ca810e

File tree

9 files changed

+185
-114
lines changed

9 files changed

+185
-114
lines changed

com.archimatetool.canvas/src/com/archimatetool/canvas/figures/CanvasBlockFigure.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.draw2d.text.TextFlow;
1919
import org.eclipse.swt.SWT;
2020
import org.eclipse.swt.graphics.Color;
21+
import org.eclipse.swt.graphics.Path;
2122

2223
import com.archimatetool.canvas.model.ICanvasModelBlock;
2324
import com.archimatetool.editor.diagram.figures.AbstractContainerFigure;
@@ -129,36 +130,47 @@ protected void drawTargetFeedback(Graphics graphics) {
129130
private void drawFigure(Graphics graphics, Color background) {
130131
graphics.pushState();
131132

132-
graphics.setAntialias(SWT.ON);
133-
134-
graphics.setAlpha(getAlpha());
135-
136133
Rectangle rect = getBounds().getCopy();
137134

138135
// Reduce width and height by 1 pixel
139136
rect.resize(-1, -1);
140137

141-
boolean drawBorder = getBorderColor() != null && getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE;
142-
143-
if(drawBorder) {
144-
// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
145-
setLineWidth(graphics, rect);
146-
setLineStyle(graphics);
147-
}
148-
138+
graphics.setAlpha(getAlpha());
149139
graphics.setBackgroundColor(background);
150-
graphics.fillRectangle(rect);
140+
Path path = createPath(rect, false);
141+
graphics.fillPath(path);
142+
path.dispose();
151143

152144
// Icon
153145
drawIconImage(graphics, rect);
154146

155147
// Border
156-
if(drawBorder) {
148+
if(getBorderColor() != null && getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE) {
149+
graphics.setLineWidth(getLineWidth());
150+
setLineStyle(graphics);
157151
graphics.setAlpha(getLineAlpha());
158152
graphics.setForegroundColor(getBorderColor());
159-
graphics.drawRectangle(rect.x, rect.y, rect.width, rect.height);
153+
path = createPath(rect, true);
154+
graphics.drawPath(path);
155+
path.dispose();
160156
}
161157

162158
graphics.popState();
163159
}
160+
161+
protected Path createPath(Rectangle rect, boolean drawingOutline) {
162+
Path path = new Path(null);
163+
164+
float inset = drawingOutline ? getLineWidth() / 2.0f : 0;
165+
166+
float x = rect.x + inset;
167+
float y = rect.y + inset;
168+
float width = rect.width - inset * 2;
169+
float height = rect.height - inset * 2;
170+
171+
path.addRectangle(x, y, width, height);
172+
173+
return path;
174+
}
175+
164176
}

com.archimatetool.canvas/src/com/archimatetool/canvas/figures/CanvasStickyFigure.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,13 @@ public Color getBorderColor() {
108108
protected void paintFigure(Graphics graphics) {
109109
graphics.pushState();
110110

111-
graphics.setAntialias(SWT.ON);
112-
113111
graphics.setAlpha(getAlpha());
114112

115113
Rectangle rect = getBounds().getCopy();
116114

117115
// Reduce width and height by 1 pixel
118116
rect.resize(-1, -1);
119117

120-
// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
121-
setLineWidth(graphics, rect);
122-
123118
// Bug on Linux hi-res using Graphics.fillGradient()
124119
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=568864
125120
if(PlatformUtils.isLinux() && ImageFactory.getDeviceZoom() > 100) {
@@ -137,23 +132,25 @@ protected void paintFigure(Graphics graphics) {
137132

138133
// Border
139134
if(getBorderColor() != null) {
135+
graphics.setLineWidth(getLineWidth());
140136
graphics.setAlpha(getLineAlpha());
141137

142-
float lineOffset = (float)getLineWidth() / 2;
138+
float inset = (float)getLineWidth() / 2;
139+
float width = rect.width - inset * 2;
143140

144141
graphics.setForegroundColor(ColorFactory.getLighterColor(getBorderColor(), 0.82f));
145142
Path path = new Path(null);
146-
path.moveTo(rect.x - lineOffset, rect.y);
147-
path.lineTo(rect.x + rect.width, rect.y);
148-
path.lineTo(rect.x + rect.width, rect.y + rect.height);
143+
path.moveTo(rect.x, rect.y + inset);
144+
path.lineTo(rect.x + inset + width, rect.y + inset);
145+
path.lineTo(rect.x + inset + width, rect.y + rect.height);
149146
graphics.drawPath(path);
150147
path.dispose();
151148

152149
graphics.setForegroundColor(getBorderColor());
153150
path = new Path(null);
154-
path.moveTo(rect.x, rect.y - lineOffset);
155-
path.lineTo(rect.x, rect.y + rect.height);
156-
path.lineTo(rect.x + rect.width + lineOffset, rect.y + rect.height);
151+
path.moveTo(rect.x + inset, rect.y);
152+
path.lineTo(rect.x + inset, rect.y + rect.height);
153+
path.lineTo(rect.x + inset + rect.width - inset, rect.y + rect.height);
157154
graphics.drawPath(path);
158155
path.dispose();
159156
}

com.archimatetool.editor/src/com/archimatetool/editor/diagram/figures/AbstractDiagramModelObjectFigure.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,7 @@ protected void drawFigure(Graphics graphics) {
150150
* @param bounds The bounds of the object
151151
*/
152152
protected void setLineWidth(Graphics graphics, Rectangle bounds) {
153-
setLineWidth(graphics, getLineWidth(), bounds);
154-
}
155-
156-
/**
157-
* Set the line width and compensate the figure bounds width and height for this line width and translate the graphics instance
158-
* @param graphics The graphics instance
159-
* @param lineWidth The line width
160-
* @param bounds The bounds of the object
161-
*/
162-
protected void setLineWidth(Graphics graphics, int lineWidth, Rectangle bounds) {
153+
int lineWidth = getLineWidth();
163154
graphics.setLineWidth(lineWidth);
164155

165156
final double scale = FigureUtils.getGraphicsScale(graphics);

com.archimatetool.editor/src/com/archimatetool/editor/diagram/figures/AbstractFigureDelegate.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,6 @@ protected void setLineWidth(Graphics graphics, Rectangle bounds) {
119119
getOwner().setLineWidth(graphics, bounds);
120120
}
121121

122-
/**
123-
* Set the line width and compensate the figure bounds width and height for this line width and translate the graphics instance
124-
* @param graphics The graphics instance
125-
* @param lineWidth The line width
126-
* @param bounds The bounds of the object
127-
*/
128-
protected void setLineWidth(Graphics graphics, int lineWidth, Rectangle bounds) {
129-
getOwner().setLineWidth(graphics, lineWidth, bounds);
130-
}
131-
132122
protected int getLineStyle() {
133123
return getOwner().getLineStyle();
134124
}

com.archimatetool.editor/src/com/archimatetool/editor/diagram/figures/EllipseFigureDelegate.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.eclipse.draw2d.Graphics;
99
import org.eclipse.draw2d.geometry.Rectangle;
10+
import org.eclipse.swt.graphics.Path;
1011
import org.eclipse.swt.graphics.Pattern;
1112

1213
import com.archimatetool.model.IDiagramModelObject;
@@ -33,33 +34,29 @@ public void drawFigure(Graphics graphics) {
3334
// Reduce width and height by 1 pixel
3435
rect.resize(-1, -1);
3536

36-
boolean drawOutline = getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE;
37-
38-
if(drawOutline) {
39-
// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
40-
setLineWidth(graphics, rect);
41-
setLineStyle(graphics);
42-
}
43-
4437
graphics.setAlpha(getAlpha());
4538

4639
if(!isEnabled()) {
4740
setDisabledState(graphics);
4841
}
4942

43+
// Fill
44+
Path path = createPath(rect, false);
5045
graphics.setBackgroundColor(getFillColor());
51-
5246
Pattern gradient = applyGradientPattern(graphics, rect);
53-
54-
graphics.fillOval(rect);
55-
47+
graphics.fillPath(path);
5648
disposeGradientPattern(graphics, gradient);
57-
49+
path.dispose();
50+
5851
// Outline
59-
if(drawOutline) {
52+
if(getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE) {
53+
graphics.setLineWidth(getLineWidth());
54+
setLineStyle(graphics);
6055
graphics.setAlpha(getLineAlpha());
6156
graphics.setForegroundColor(getLineColor());
62-
graphics.drawOval(rect);
57+
path = createPath(rect, true);
58+
graphics.drawPath(path);
59+
path.dispose();
6360
}
6461

6562
// Image Icon
@@ -69,4 +66,20 @@ public void drawFigure(Graphics graphics) {
6966

7067
graphics.popState();
7168
}
69+
70+
protected Path createPath(Rectangle rect, boolean drawingOutline) {
71+
Path path = new Path(null);
72+
73+
float inset = drawingOutline ? getLineWidth() / 2.0f : 0;
74+
75+
float x = rect.x + inset;
76+
float y = rect.y + inset;
77+
float width = rect.width - inset * 2;
78+
float height = rect.height - inset * 2;
79+
80+
path.addArc(x, y, width, height, 0, 360);
81+
82+
return path;
83+
}
84+
7285
}

com.archimatetool.editor/src/com/archimatetool/editor/diagram/figures/RectangleFigureDelegate.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.eclipse.draw2d.Graphics;
99
import org.eclipse.draw2d.geometry.Rectangle;
10+
import org.eclipse.swt.graphics.Path;
1011
import org.eclipse.swt.graphics.Pattern;
1112

1213
import com.archimatetool.model.IDiagramModelObject;
@@ -33,34 +34,29 @@ public void drawFigure(Graphics graphics) {
3334
// Reduce width and height by 1 pixel
3435
rect.resize(-1, -1);
3536

36-
boolean drawOutline = getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE;
37-
38-
if(drawOutline) {
39-
// Set line width here so that the whole figure is constrained, otherwise SVG graphics will have overspill
40-
setLineWidth(graphics, rect);
41-
setLineStyle(graphics);
42-
}
43-
4437
graphics.setAlpha(getAlpha());
4538

4639
if(!isEnabled()) {
4740
setDisabledState(graphics);
4841
}
49-
42+
5043
// Fill
5144
graphics.setBackgroundColor(getFillColor());
52-
5345
Pattern gradient = applyGradientPattern(graphics, rect);
54-
55-
graphics.fillRectangle(rect);
56-
46+
Path path = createPath(rect, false);
47+
graphics.fillPath(path);
48+
path.dispose();
5749
disposeGradientPattern(graphics, gradient);
5850

5951
// Outline
60-
if(drawOutline) {
52+
if(getLineStyle() != IDiagramModelObject.LINE_STYLE_NONE) {
53+
graphics.setLineWidth(getLineWidth());
54+
setLineStyle(graphics);
6155
graphics.setAlpha(getLineAlpha());
6256
graphics.setForegroundColor(getLineColor());
63-
graphics.drawRectangle(rect);
57+
path = createPath(rect, true);
58+
graphics.drawPath(path);
59+
path.dispose();
6460
}
6561

6662
// Icon
@@ -69,4 +65,19 @@ public void drawFigure(Graphics graphics) {
6965

7066
graphics.popState();
7167
}
68+
69+
protected Path createPath(Rectangle rect, boolean drawingOutline) {
70+
Path path = new Path(null);
71+
72+
float inset = drawingOutline ? getLineWidth() / 2.0f : 0;
73+
74+
float x = rect.x + inset;
75+
float y = rect.y + inset;
76+
float width = rect.width - inset * 2;
77+
float height = rect.height - inset * 2;
78+
79+
path.addRectangle(x, y, width, height);
80+
81+
return path;
82+
}
7283
}

0 commit comments

Comments
 (0)