-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundButton.java
More file actions
65 lines (48 loc) · 1.55 KB
/
RoundButton.java
File metadata and controls
65 lines (48 loc) · 1.55 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
57
58
59
60
61
62
63
64
65
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
public class RoundButton extends JButton{
private BufferedImage bim;
public RoundButton() {
setFocusable(false);
/*
These statements enlarges the button so that it
becomes a circle rather than an oval.
*/
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);
/*
This call causes the JButton not to paint the background.
*/
setContentAreaFilled(false);
}
protected void paintComponent(Graphics g) {
g.setClip(new Ellipse2D.Double(0, 0, getWidth(), getHeight())); // set the area that shall be painted
g.drawImage(bim, 0, 0, getWidth(), getHeight(), null);
if (getModel().isArmed()) {
g.setColor(Color.WHITE);
} else {
g.setColor(getBackground());
}
super.paintComponent(g);
}
public void setImage(BufferedImage pbim) {
bim = pbim;
repaint();
}
// Hit detection.
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size, make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
}