Skip to content

Commit 1f4c4f1

Browse files
committed
Fine Tuned Drop Shadow
- Made Drop Shadow a bit Softer - RectangleDropShadow correctly considers the corner radius for the start of the shadow Added example app showing the shadows.
1 parent 9d34bc1 commit 1f4c4f1

File tree

3 files changed

+121
-7
lines changed

3 files changed

+121
-7
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Johannes Kepler University Linz
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Alois Zoitl - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.draw2d.examples.shadows;
15+
16+
import org.eclipse.swt.graphics.Color;
17+
import org.eclipse.swt.widgets.Shell;
18+
19+
import org.eclipse.draw2d.ColorConstants;
20+
import org.eclipse.draw2d.Figure;
21+
import org.eclipse.draw2d.IFigure;
22+
import org.eclipse.draw2d.examples.AbstractExample;
23+
import org.eclipse.draw2d.geometry.Rectangle;
24+
import org.eclipse.draw2d.shadows.EllipseDropShadowBorder;
25+
import org.eclipse.draw2d.shadows.RectangleDropShadowBorder;
26+
27+
public class ShadowsExample extends AbstractExample {
28+
29+
public static void main(String[] args) {
30+
new ShadowsExample().run();
31+
}
32+
33+
@Override
34+
protected IFigure createContents() {
35+
IFigure panel = new Figure();
36+
37+
panel.add(createPage());
38+
panel.add(createRectangleFigure(480, 40, 50, 30, ColorConstants.green));
39+
panel.add(createRectangleFigure(480, 230, 50, 30, ColorConstants.black));
40+
41+
return panel;
42+
}
43+
44+
@Override
45+
protected void hookShell(Shell shell) {
46+
getFigureCanvas().setSize(600, 500);
47+
}
48+
49+
private static IFigure createCircleFigure(int x, int y, int r, Color color) {
50+
IFigure circle = new Figure() {
51+
@Override
52+
protected void paintFigure(org.eclipse.draw2d.Graphics graphics) {
53+
// super paint figure first to draw the border
54+
super.paintFigure(graphics);
55+
graphics.fillOval(getBounds());
56+
}
57+
};
58+
59+
circle.setBounds(new Rectangle(x - r, y - r, 2 * r, 2 * r));
60+
circle.setBackgroundColor(color);
61+
circle.setBorder(new EllipseDropShadowBorder());
62+
return circle;
63+
}
64+
65+
private static IFigure createPage() {
66+
IFigure page = createRectangleFigure(10, 10, 350, 420, ColorConstants.white);
67+
// give page a larger softer schadow
68+
RectangleDropShadowBorder pageBorder = new RectangleDropShadowBorder();
69+
pageBorder.setDropShadowSize(12);
70+
pageBorder.setHaloSize(6);
71+
pageBorder.setSoftness(4.5);
72+
page.setBorder(pageBorder);
73+
74+
page.add(createCircleFigure(40, 40, 20, ColorConstants.darkBlue));
75+
page.add(createRoundedRectangleFigure(150, 45, 120, 100, 10, ColorConstants.button));
76+
page.add(createRectangleFigure(200, 200, 120, 75, ColorConstants.cyan));
77+
page.add(createRectangleFigure(75, 300, 200, 100, ColorConstants.lightGreen));
78+
79+
return page;
80+
}
81+
82+
private static IFigure createRectangleFigure(int x, int y, int w, int h, Color color) {
83+
IFigure circle = new Figure() {
84+
@Override
85+
protected void paintFigure(org.eclipse.draw2d.Graphics graphics) {
86+
// super paint figure first to draw the border
87+
super.paintFigure(graphics);
88+
graphics.fillRectangle(getBounds());
89+
}
90+
};
91+
92+
circle.setBounds(new Rectangle(x, y, w, h));
93+
circle.setBackgroundColor(color);
94+
circle.setBorder(new RectangleDropShadowBorder());
95+
return circle;
96+
}
97+
98+
private static IFigure createRoundedRectangleFigure(int x, int y, int w, int h, int cornerRadius, Color color) {
99+
IFigure circle = new Figure() {
100+
@Override
101+
protected void paintFigure(org.eclipse.draw2d.Graphics graphics) {
102+
// super paint figure first to draw the border
103+
super.paintFigure(graphics);
104+
graphics.fillRoundRectangle(getBounds(), cornerRadius, cornerRadius);
105+
}
106+
};
107+
108+
circle.setBounds(new Rectangle(x, y, w, h));
109+
circle.setBackgroundColor(color);
110+
circle.setBorder(new RectangleDropShadowBorder(cornerRadius));
111+
return circle;
112+
}
113+
114+
}

org.eclipse.draw2d/src/org/eclipse/draw2d/shadows/EllipseDropShadowBorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ protected void paintDropShadow(Graphics graphics, Rectangle shadowRect, int size
4444
for (int i = 0; i < size; i++) {
4545
final double progress = (double) i / size;
4646
graphics.setAlpha(calcAlphaValue(progress));
47-
graphics.drawArc(r, 270, 90);
4847
r.x++;
4948
r.y++;
49+
graphics.drawArc(r, 270, 90);
5050
}
5151
}
5252

org.eclipse.draw2d/src/org/eclipse/draw2d/shadows/RectangleDropShadowBorder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public RectangleDropShadowBorder(int cornerRadius) {
6464

6565
@Override
6666
protected void paintDropShadow(final Graphics graphics, final Rectangle shadowRect, final int size) {
67-
int bottomXStart = shadowRect.x + 1;
68-
int bottomY = shadowRect.y + 1 + shadowRect.height;
69-
final int bottomXEnd = shadowRect.x + shadowRect.width + 1 - cornerStartRadius;
67+
int bottomXStart = shadowRect.x + cornerStartRadius;
68+
int bottomY = shadowRect.y + 2 + shadowRect.height;
69+
final int bottomXEnd = shadowRect.x + shadowRect.width + 2 - cornerStartRadius;
7070

71-
int rightX = shadowRect.x + shadowRect.width + 1;
72-
int rightYStart = shadowRect.y + 1;
73-
final int rightYEnd = shadowRect.y + shadowRect.height + 1 - cornerStartRadius;
71+
int rightX = shadowRect.x + shadowRect.width + 2;
72+
int rightYStart = shadowRect.y + cornerStartRadius;
73+
final int rightYEnd = shadowRect.y + shadowRect.height + 2 - cornerStartRadius;
7474
int cornerDiameter = 2 * cornerStartRadius;
7575

7676
for (int i = 0; i <= size; i++) {

0 commit comments

Comments
 (0)