-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaletteView.java
More file actions
executable file
·199 lines (161 loc) · 6.53 KB
/
PaletteView.java
File metadata and controls
executable file
·199 lines (161 loc) · 6.53 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
enum Tool {
select, erase, line, circle, rectangle, bucket;
} //duplicated!!
*/
public class PaletteView extends JPanel implements IView {
private Model model;
private boolean toggled = false;
private JButton getButton(String path, Tool t) {
JButton b = new JButton();
b.setIcon(new ImageIcon(path));
b.setMargin(new Insets(10, 10, 10, 10));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.clearSelectedShape();
model.setCurrentTool(t);
}
});
return b;
}
public PaletteView(Model model1) {
model = model1;
//this.setBackground(Color.RED); //DEBUGGING TESTING
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setPreferredSize(new Dimension(200, 700));
JToolBar drawingToolBar = new JToolBar(JToolBar.VERTICAL);
JToolBar colorToolBar = new JToolBar(JToolBar.VERTICAL);
JToolBar thicknessToolBar = new JToolBar(JToolBar.VERTICAL);
thicknessToolBar.setPreferredSize(new Dimension(100, 150));
this.add(drawingToolBar);
this.add(colorToolBar);
this.add(thicknessToolBar);
JPanel drawingTools = new JPanel(new GridLayout(3, 2, 4, 4));
drawingTools.setPreferredSize(new Dimension(100, 150));
/**** CREATING DRAWING drawingTools BUTTON -- START ****/
// create Selection button
JButton selection = getButton("img/selection.png", Tool.select);
drawingTools.add(selection);
// create Eraser button
JButton eraser = getButton("img/eraser.png", Tool.erase);
drawingTools.add(eraser);
// create Line button
JButton line = getButton("img/line.png", Tool.line);
drawingTools.add(line);
// create Circle button
JButton circle = getButton("img/circle.png", Tool.circle);
drawingTools.add(circle);
// create Rectangle button
JButton rectangle = getButton("img/rectangle.png", Tool.rectangle);
drawingTools.add(rectangle);
// create Bucket button
JButton bucket = getButton("img/bucket.png", Tool.bucket);
drawingTools.add(bucket);
/**** CREATING DRAWING drawingTools BUTTON -- END ****/
// Add color pallete
// Reference:
// https://stackoverflow.com/questions/26565166/how-to-display-a-color-selector-when-clicking-a-button
JButton colorButton = new JButton();
JColorChooser colorChooser = new JColorChooser();
colorChooser.setBorder(null);
int colorButtonWidth = 50;
int colorButtonHeight = 30;
colorButton.setMinimumSize(new Dimension(colorButtonWidth, colorButtonHeight));
colorButton.setPreferredSize(new Dimension(colorButtonWidth, colorButtonHeight));
colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Color newColor = colorChooser.getColor();
colorButton.setBackground(newColor);
}
});
drawingToolBar.add(drawingTools);
// PaletteView current = this;
JToolBar current = colorToolBar;
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (toggled) {
current.remove(colorChooser);
} else {
colorChooser.setBounds(colorButton.getX(), colorButton.getY() + 20, 600, 300);
colorChooser.setVisible(true);
current.add(colorChooser);
model.setCurrentColor(colorChooser.getColor());
}
toggled = !toggled;
current.validate();
current.repaint();
}
});
// this.add(colorButton);
colorToolBar.add(colorButton);
/**** CODE FOR THICKNESS LINES -- START ****/
JPanel lines = new JPanel(new GridLayout(3, 1, 4, 4));
lines.setPreferredSize(new Dimension(100, 150));
// create Line1 button
JButton line1 = new JButton();
line1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Line 1 tool clicked");
model.setCurrentThickness(1);
if(model.currentTool == Tool.select) {
model.setSelectedShapeThickness(1);
}
}
});
// img = ImageIO.read(getClass().getResource("img/line1.png"));
line1.setIcon(new ImageIcon("img/line1.png"));
line1.setMargin(new Insets(10, 0, 10, 0));
lines.add(line1);
// create Line2 button
JButton line2 = new JButton();
line2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Line 2 tool clicked");
model.setCurrentThickness(2);
if(model.currentTool == Tool.select) {
model.setSelectedShapeThickness(2);
}
}
});
// img = ImageIO.read(getClass().getResource("img/line2.png"));
line2.setIcon(new ImageIcon("img/line2.png"));
line2.setMargin(new Insets(10, 0, 10, 0));
lines.add(line2);
// create Line2 button
JButton line3 = new JButton();
line3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Line 3 tool clicked");
model.setCurrentThickness(3);
if(model.currentTool == Tool.select) {
model.setSelectedShapeThickness(3);
}
}
});
// img = ImageIO.read(getClass().getResource("img/line3.png"));
line3.setIcon(new ImageIcon("img/line3.png"));
line3.setMargin(new Insets(10, 0, 10, 0));
lines.add(line3);
thicknessToolBar.add(lines);
/**** CODE FOR THICKNESS LINES -- END ****/
// define the layout
// http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("aaaaaaaa");
}
});
this.setVisible(true);
}
public void updateView() {
}
}