-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPieceButton.java
63 lines (50 loc) · 1.4 KB
/
PieceButton.java
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
package boardgame.gui;
import java.awt.*;
import javax.swing.*;
import boardgame.pieces.*;
@SuppressWarnings("serial")
public class PieceButton extends JButton {
public Piece source;
public PieceButton() {
// TODO Auto-generated constructor stub
}
public PieceButton(Icon icon) {
super(icon);
// TODO Auto-generated constructor stub
}
public PieceButton(String text) {
super(text);
// TODO Auto-generated constructor stub
}
public PieceButton(Action a) {
super(a);
// TODO Auto-generated constructor stub
}
public PieceButton(String text, Icon icon) {
super(text, icon);
// TODO Auto-generated constructor stub
}
public static PieceButton createPieceButton(Piece piece) {
String color = piece.getColor().toString();
String name = piece.getPieceName().toString();
String filepath = "images/" + color + " "+ name + ".png";
ImageIcon icon = new ImageIcon(filepath);
Image unscaled = icon.getImage();
Image scaled = unscaled.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(scaled);
PieceButton newButton = new PieceButton(icon);
newButton.source = piece;
return newButton;
}
@Override
public Dimension getPreferredSize() {
Container c = this.getParent();
if (c == null) {
return new Dimension(50, 50);
}
Dimension d = c.getSize();
int height = (int)d.getHeight();
height /= 9;
return new Dimension(height, height);
}
}