-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundedPanel.java
More file actions
46 lines (35 loc) · 1.34 KB
/
Copy pathRoundedPanel.java
File metadata and controls
46 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Rent_Rover; // make sure this matches your package
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class RoundedPanel extends JPanel {
private int cornerRadius = 20; // default radius
public RoundedPanel() {
super();
setOpaque(false); // important for rounded corners
}
public RoundedPanel(int radius) {
super();
cornerRadius = radius;
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Background color
g2.setColor(getBackground());
// Draw rounded rectangle
g2.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), cornerRadius, cornerRadius));
g2.dispose();
super.paintComponent(g);
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(getForeground());
g2.draw(new RoundRectangle2D.Double(0, 0, getWidth()-1, getHeight()-1, cornerRadius, cornerRadius));
g2.dispose();
}
}