1+ /**
2+ * MIT License
3+ *
4+ * Copyright (c) 2019-2020 Vincenzo Palazzo vincenzopalazzo1996@gmail.com
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+ package mdlaf .shadows ;
25+
26+ import mdlaf .utils .MaterialColors ;
27+
28+ import javax .swing .*;
29+ import javax .swing .border .AbstractBorder ;
30+ import java .awt .*;
31+ import java .awt .geom .Area ;
32+ import java .awt .geom .Rectangle2D ;
33+ import java .awt .geom .RoundRectangle2D ;
34+ import java .util .Objects ;
35+
36+ /**
37+ * @author https://github.com/vincenzopalazzo
38+ */
39+ public class RoundedCornerBorder extends AbstractBorder {
40+
41+ protected int arch = 12 ; //default value
42+ protected Color colorLine ;
43+ protected float withBorder = 1.2f ;
44+
45+ public RoundedCornerBorder () {
46+ colorLine = MaterialColors .LIGHT_BLUE_400 ;
47+ }
48+
49+ public RoundedCornerBorder (Color colorLine ) {
50+ this .colorLine = colorLine ;
51+ //alphaZero = new Color(0x0, true);
52+ }
53+
54+ public RoundedCornerBorder (Color colorLine , int arch ) {
55+ this .colorLine = colorLine ;
56+ this .arch = arch ;
57+ }
58+
59+ public RoundedCornerBorder (int arch , Color colorLine , float withBorder ) {
60+ this .arch = arch ;
61+ this .colorLine = colorLine ;
62+ this .withBorder = withBorder ;
63+ }
64+
65+ @ Override
66+ public void paintBorder (Component c , Graphics g , int x , int y , int width , int height ) {
67+ Graphics2D g2 = (Graphics2D ) g .create ();
68+ g2 .setStroke (new BasicStroke (withBorder ));
69+ g2 .setRenderingHint (RenderingHints .KEY_ANTIALIASING , RenderingHints .VALUE_ANTIALIAS_ON );
70+ int r = arch ;
71+ int w = width - 1 ;
72+ int h = height - 1 ;
73+
74+ Area round = new Area (new RoundRectangle2D .Float (x , y , w , h , r , r ));
75+ if (c instanceof JPopupMenu ) {
76+ g2 .setPaint (c .getBackground ());
77+ g2 .fill (round );
78+ } else {
79+ Container parent = c .getParent ();
80+ if (Objects .nonNull (parent )) {
81+ g2 .setPaint (parent .getBackground ());
82+ Area corner = new Area (new Rectangle2D .Float (x , y , width , height ));
83+ corner .subtract (round );
84+ g2 .fill (corner );
85+ }
86+ }
87+ g2 .setPaint (colorLine );
88+ g2 .draw (round );
89+ g2 .dispose ();
90+ }
91+
92+ @ Override
93+ public Insets getBorderInsets (Component c ) {
94+ return new Insets (4 , 8 , 4 , 8 );
95+ }
96+
97+ @ Override
98+ public Insets getBorderInsets (Component c , Insets insets ) {
99+ insets .set (4 , 8 , 4 , 8 );
100+ return insets ;
101+ }
102+ }
0 commit comments