-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundedImagePanel.java
More file actions
56 lines (46 loc) · 1.92 KB
/
Copy pathRoundedImagePanel.java
File metadata and controls
56 lines (46 loc) · 1.92 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
47
48
49
50
51
52
53
54
55
56
package Rent_Rover;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
public class RoundedImagePanel extends JPanel {
private Image backgroundImage;
private int cornerRadius = 30; // default, can change
// Constructor with image path and optional corner radius
public RoundedImagePanel(String imagePath, int radius) {
try {
ImageIcon icon = new ImageIcon(imagePath);
backgroundImage = icon.getImage();
} catch (Exception e) {
System.out.println("Image not found: " + imagePath);
}
cornerRadius = radius;
setOpaque(false); // important for rounded corners
setLayout(null); // optional: allows you to place components freely
}
// Overloaded constructor with default radius
public RoundedImagePanel(String imagePath) {
this(imagePath, 30);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw background image scaled to panel size
if (backgroundImage != null) {
g2.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
} else {
g2.setColor(getBackground());
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();
}
}