1 /*******************************************************************************
2 * Copyright (c) 2006, 2016 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14
15 package org.eclipse.swt.examples.graphics;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Device;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Path;
21 import org.eclipse.swt.graphics.Pattern;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Menu;
30
31 /**
32 * This tab demonstrates various line joins. It allows a user to choose from
33 * bevel, miter and round.
34 */
35 public class LineJoinTab extends GraphicsTab {
36
37 private Combo joinCb;
38 private Button colorButton;
39 private GraphicsBackground shapeColor;
40 private Menu menu;
41 private int [] joinValues = new int [] {SWT.JOIN_BEVEL, SWT.JOIN_MITER, SWT.JOIN_ROUND};
42
LineJoinTab(GraphicsExample example)43 public LineJoinTab(GraphicsExample example) {
44 super(example);
45 }
46
47 @Override
getCategory()48 public String getCategory() {
49 return GraphicsExample.getResourceString("Lines"); //$NON-NLS-1$
50 }
51
52 @Override
getText()53 public String getText() {
54 return GraphicsExample.getResourceString("LineJoin"); //$NON-NLS-1$
55 }
56
57 @Override
getDescription()58 public String getDescription() {
59 return GraphicsExample.getResourceString("LineJoinDescription"); //$NON-NLS-1$
60 }
61
62 @Override
dispose()63 public void dispose() {
64 if (menu != null) {
65 menu.dispose();
66 menu = null;
67 }
68 }
69
70 @Override
createControlPanel(Composite parent)71 public void createControlPanel(Composite parent) {
72
73 // create drop down combo for choosing clipping
74 Composite comp = new Composite(parent, SWT.NONE);
75 comp.setLayout(new GridLayout(2, false));
76
77 new Label(comp, SWT.CENTER).setText(GraphicsExample
78 .getResourceString("LineJoin")); //$NON-NLS-1$
79 joinCb = new Combo(comp, SWT.DROP_DOWN);
80 joinCb.add(GraphicsExample
81 .getResourceString("bevel")); //$NON-NLS-1$
82 joinCb.add(GraphicsExample
83 .getResourceString("miter")); //$NON-NLS-1$
84 joinCb.add(GraphicsExample
85 .getResourceString("round")); //$NON-NLS-1$
86 joinCb.select(1);
87 joinCb.addListener(SWT.Selection, event -> example.redraw());
88
89 // color menu
90 ColorMenu cm = new ColorMenu();
91 cm.setPatternItems(example.checkAdvancedGraphics());
92 menu = cm.createMenu(parent.getParent(), gb -> {
93 shapeColor = gb;
94 colorButton.setImage(gb.getThumbNail());
95 example.redraw();
96 });
97
98 // initialize the shape color to the 4th item in the menu (green)
99 shapeColor =(GraphicsBackground)menu.getItem(3).getData();
100
101 // color button
102 comp = new Composite(parent, SWT.NONE);
103 comp.setLayout(new GridLayout(2, false));
104
105 colorButton = new Button(comp, SWT.PUSH);
106 colorButton.setText(GraphicsExample
107 .getResourceString("Color")); //$NON-NLS-1$
108 colorButton.setImage(shapeColor.getThumbNail());
109 colorButton.addListener(SWT.Selection, event -> {
110 final Button button = (Button) event.widget;
111 final Composite parent1 = button.getParent();
112 Rectangle bounds = button.getBounds();
113 Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
114 menu.setLocation(point.x, point.y + bounds.height);
115 menu.setVisible(true);
116 });
117
118 }
119
120 @Override
paint(GC gc, int width, int height)121 public void paint(GC gc, int width, int height) {
122 if (!example.checkAdvancedGraphics()) return;
123 Device device = gc.getDevice();
124
125 gc.setLineWidth(20);
126 gc.setLineJoin(joinValues[joinCb.getSelectionIndex()]);
127
128 // set the foreground color or pattern
129 Pattern pattern = null;
130 if (shapeColor.getBgColor1() != null) {
131 gc.setForeground(shapeColor.getBgColor1());
132 } else if (shapeColor.getBgImage() != null) {
133 pattern = new Pattern(device, shapeColor.getBgImage());
134 gc.setForegroundPattern(pattern);
135 }
136
137 // draw the shape
138 Path path = new Path(device);
139 path.moveTo(width/2, 25);
140 path.lineTo(2*width/3, height/3);
141 path.lineTo(width-25, height/2);
142 path.lineTo(2*width/3, 2*height/3);
143 path.lineTo(width/2, height-25);
144 path.lineTo(width/3, 2*height/3);
145 path.lineTo(25, height/2);
146 path.lineTo(width/3, height/3);
147 path.lineTo(width/2, 25);
148 path.close();
149 gc.drawPath(path);
150 path.dispose();
151
152 if (pattern != null) pattern.dispose();
153 }
154
155 }
156
157
158