1
6 import java.util.*;
7 import javax.swing.*;
8 import java.awt.*;
9 import java.awt.geom.*;
10 import java.awt.event.*;
11 import java.awt.Graphics2D;
12
13
15 public class Box extends Sub implements ActionListener {
16
17 private static final long serialVersionUID = Version.getSUID();
18
19
22 Size bordersize = null;
23
24
27 LineType borderstyle = null;
28
29
32 Color bordercolor = Color.black;
33
34
37 Fill fill = null;
38
39
42 Color color = null;
43
44
48 public Box(){}
49
50
53 public Box(Point2D where,View view,Object arg) {
54 super(where,view,(arg instanceof Rectangle2D)?arg:new Rectangle2D.Double(-0.5,-0.5,1,1));
55 editscale = true;
56 editrotation=false;
57 subaspect = 1.0;
58 init(where,view);
59 }
60
61 transient JTextField aspfield = null;
62 transient JComboBox bordersizefield=null;
63 transient JComboBox borderstylefield=null;
64 transient ColorBox bordercolorbox=null;
65 transient JButton fillbutton=null;
66 transient ColorBox colorbox=null;
67
68 transient Library filllib = null;
69
70 public int getLevel() {return(super.getLevel()+1);}
71
72 public static String NOFILL = "Transparent";
73
74 public void getPropertyEdit(Object[] edits,int slot,Set sub, Symbol parent) {
75 JPanel res = new JPanel();
76 res.setLayout(new BoxLayout(res,BoxLayout.Y_AXIS));
77 edits[slot] = res;
78 res.setName("Box");
79
80 aspfield = PrefEditor.addDouble(res,subaspect,"Aspect Ratio");
81
82 JPanel row = new JPanel();
83 row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
84
85 row.add(new JLabel("Border thickness "));
86 bordersizefield = new JComboBox(Size.getAll(Size.class));
87 bordersizefield.setSelectedItem(bordersize);
88 row.add(bordersizefield);
89 res.add(row);
90
91 row = new JPanel();
92 row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
93
94 row.add(new JLabel("Border Style "));
95 borderstylefield = new JComboBox(LineType.predefined.toArray());
96 borderstylefield.setSelectedItem(borderstyle);
97 row.add(borderstylefield);
98 res.add(row);
99
100 bordercolorbox=new ColorBox(bordercolor,"Border Color");
101 res.add(bordercolorbox);
102
103 row = new JPanel();
104 row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS));
105
123
124
125
126
127 colorbox=new ColorBox(color,"Fill Color");
128 row.add(colorbox);
129
130 JButton butt = new JButton(NOFILL);
131 row.add(butt);
132 butt.addActionListener(this);
133
134 res.add(row);
135
136 super.getPropertyEdit(edits,slot-1,sub,parent);
137 }
138
139 public void acceptPropertyEdit() {
140 if (aspfield!=null) {
141 double newasp = PrefEditor.readDouble(aspfield);
142 if (newasp!=subaspect) {
143 subaspect=newasp;
144 reshape();
145 }
146
147 bordersize = (Size)bordersizefield.getSelectedItem();
148
149 bordercolor = bordercolorbox.color;
150 borderstyle = (LineType)borderstylefield.getSelectedItem();
151
152 color = colorbox.color;
153
154 aspfield = null;
155 bordersizefield=null;
156 borderstylefield=null;
157 bordercolorbox=null;
158 fillbutton=null;
159 colorbox=null;
160 filllib = null;
161 }
162 super.acceptPropertyEdit();
163 }
164
165 public void abandonPropertyEdit() {
166 aspfield = null;
167 bordersizefield=null;
168 borderstylefield=null;
169 bordercolorbox=null;
170 fillbutton=null;
171 colorbox=null;
172 filllib = null;
173
174 super.abandonPropertyEdit();
175 }
176
177 public void actionPerformed(ActionEvent e) {
178 fill = null;
179 colorbox.color = null;
180
181 }
182
183
186 protected Vector getEditModes() {
187 Vector res = super.getEditModes();
188 res.add("Aspect");
189
190 res.add("Scale & Aspect");
191 return(res);
192 }
193
194 public Area getIn(View view) {
195 if (view.dontShowIn!=null && view.dontShowIn.contains(this)) return(new Area());
196 Area res = new Area(getShape(view.trans));
197
209 return(res);
210 }
211
212
214 public void paint(View view) {
215 if (!view.visible.isMember(this)) return;
216 super.paint(view);
217 Point2D tpos = new Point2D.Double();
218 Point2D tpos2 = new Point2D.Double();
219
220 locate();
221
222 GeneralPath path = new GeneralPath();
223
224 view.trans.transform(corners[0],tpos);
225 path.moveTo((int)tpos.getX(),(int)tpos.getY());
226
227 for (int i=1;i<4;i++) {
228 view.trans.transform(corners[i],tpos);
229 path.lineTo((int)tpos.getX(),(int)tpos.getY());
230 }
231 path.closePath();
232
233 Graphics2D gr = (Graphics2D)view.draw;
234 Paint realpaint=null;
235 if (fill!=null) realpaint = fill.getPaint(view,path);
236 if (color!=null) realpaint = color;
237 if (realpaint!=null) {
238 Paint pa = gr.getPaint();
239 gr.setPaint(realpaint);
240 gr.fill(path);
241 gr.setPaint(pa);
242 }
243
244
245 if (bordersize==null) bordersize = (Size)Size.lookup(Size.class,"One Pixel");
246
247 if (bordercolor==null) bordercolor = Color.black;
248 if (borderstyle==null) borderstyle = LineType.lookup("Generic");
249
250 SavableStroke stroke = borderstyle.getStroke(bordersize.getSize(view.mapscale));
251 if (stroke!=null) {
252 Stroke st = view.draw.getStroke();
253 Color color = view.draw.getColor();
254 view.draw.setStroke(stroke);
255 view.draw.setColor(bordercolor);
256
257
258 view.draw.draw(path);
259 view.draw.setStroke(st);
260 view.draw.setColor(color);
261 }
262 }
263
264
268 public void paintContents(View view) {}
269 }
270