Repository navigation
Expand file tree
/
Copy pathToolPalette.java
More file actions
147 lines (129 loc) · 4.52 KB
/
Copy pathToolPalette.java
File metadata and controls
147 lines (129 loc) · 4.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package java_paint;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* In the ToolPalette class we configure some buttons that we add to a JToolBar component.
* These button allows us to configure the type of brush that we would use to create a
* BrushStroke instance.
*
* @author
*
*/
public class ToolPalette extends JToolBar implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JToggleButton buttonPencil;
private JToggleButton buttonLine;
private JToggleButton buttonRectangle;
private JToggleButton buttonEllipse;
private JToggleButton buttonArc;
private JToggleButton buttonPolygon;
private String choice;
private String PATH_IMAGES = "/jpaint/images/";
public ToolPalette() {
choice = "pencil";
setFloatable(true); // Let the user to move the tool bar
initializeComponents();
updateButtonState();
}
private void initializeComponents() {
setupPencilButton();
setupLineButton();
setupRectangleButton();
setupEllipseButton();
setupArcButton();
setupPolygonButton();
}
private void setupPencilButton() {
buttonPencil = new JToggleButton();
buttonPencil.setIcon(createImageIcon(PATH_IMAGES + "pencil.png"));
buttonPencil.addActionListener(this);
add(buttonPencil);
}
private void setupLineButton() {
buttonLine = new JToggleButton();
buttonLine.setIcon(createImageIcon(PATH_IMAGES + "line.png"));
buttonLine.addActionListener(this);
add(buttonLine);
}
private void setupRectangleButton() {
buttonRectangle = new JToggleButton();
buttonRectangle.setIcon(createImageIcon(PATH_IMAGES + "rectangle.png"));
buttonRectangle.addActionListener(this);
add(buttonRectangle);
}
private void setupEllipseButton() {
buttonEllipse = new JToggleButton();
buttonEllipse.setIcon(createImageIcon(PATH_IMAGES + "ellipse.png"));
buttonEllipse.addActionListener(this);
add(buttonEllipse);
}
private void setupArcButton() {
buttonArc = new JToggleButton();
buttonArc.setIcon(createImageIcon(PATH_IMAGES + "arc.png"));
buttonArc.addActionListener(this);
add(buttonArc);
}
private void setupPolygonButton() {
buttonPolygon = new JToggleButton();
buttonPolygon.setIcon(createImageIcon(PATH_IMAGES + "polygon.png"));
buttonPolygon.addActionListener(this);
add(buttonPolygon);
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ToolPalette.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("File not found: " + path);
return null;
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonPencil) {
setButtonSelected("pencil");
} else if (e.getSource() == buttonLine) {
setButtonSelected("line");
} else if (e.getSource() == buttonRectangle) {
setButtonSelected("rectangle");
} else if (e.getSource() == buttonEllipse) {
setButtonSelected("ellipse");
} else if (e.getSource() == buttonArc) {
setButtonSelected("arc");;
} else if (e.getSource() == buttonPolygon) {
setButtonSelected("polygon");
}
}
public void setButtonSelected(String userChoice) {
this.choice = userChoice;
updateButtonState();
}
public String getButtonSelected() {
return choice;
}
private void updateButtonState() {
buttonPencil.setSelected(false);
buttonLine.setSelected(false);
buttonRectangle.setSelected(false);
buttonEllipse.setSelected(false);
buttonArc.setSelected(false);
buttonPolygon.setSelected(false);
if (choice.equalsIgnoreCase("pencil")) {
buttonPencil.setSelected(true);
} else if (choice.equalsIgnoreCase("line")) {
buttonLine.setSelected(true);
} else if (choice.equalsIgnoreCase("rectangle")) {
buttonRectangle.setSelected(true);
} else if (choice.equalsIgnoreCase("ellipse")) {
buttonEllipse.setSelected(true);
} else if (choice.equalsIgnoreCase("arc")) {
buttonArc.setSelected(true);
} else {
buttonPolygon.setSelected(true);
}
}
}