|
| 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 | +} |
0 commit comments