Skip to content

Commit 53abbf1

Browse files
First draft component
1 parent 5961df2 commit 53abbf1

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/main/java/io/vincenzopalazzo/placeholder/JTextFieldPlaceholder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.vincenzopalazzo.placeholder;
22

3+
import mdlaf.shadows.RoundedCornerBorder;
4+
35
import javax.swing.*;
46
import javax.swing.border.Border;
57
import java.awt.*;
@@ -23,7 +25,7 @@ protected void initView(){
2325

2426
placeholder = new JLabel();
2527
separator = new JSeparator(JSeparator.VERTICAL);
26-
placeholder.setBorder(BorderFactory.createEmptyBorder(5,0,5,2));
28+
placeholder.setBorder(BorderFactory.createEmptyBorder(0,0,0,2));
2729
this.add(placeholder);
2830
this.add(separator);
2931

@@ -32,6 +34,11 @@ protected void initView(){
3234
textField.setPreferredSize(new Dimension(50, 20));
3335
textField.setSize(new Dimension(50, 20));
3436
super.add(textField);
37+
38+
textField.setBorder(BorderFactory.createEmptyBorder());
39+
iconContainer.setBorder(BorderFactory.createEmptyBorder());
40+
placeholder.setBorder(BorderFactory.createEmptyBorder());
41+
setBorder(new RoundedCornerBorder(getBackground(), 6));
3542
}
3643

3744
protected void initStyle(){
@@ -40,6 +47,17 @@ protected void initStyle(){
4047
iconContainer.setBackground(getBackground());
4148
}
4249

50+
@Override
51+
protected void paintComponent(Graphics g) {
52+
super.paintComponent(g);
53+
this.paintLine(g);
54+
}
55+
56+
protected void paintLine(Graphics graphics){
57+
graphics.setColor(Color.CYAN);
58+
graphics.fillRect(iconContainer.getX(), this.getHeight() - this.getY(), this.getWidth() - iconContainer.getWidth(), 1);
59+
}
60+
4361
public JTextFieldPlaceholder setIcon(Icon icon){
4462
if(icon == null) throw new IllegalArgumentException("icon null");
4563
iconContainer.setIcon(icon);
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)