桌游编辑器实现之EditBox

 

作为一款编辑器,最基本的一个功能模块的小功能 可变大小的选框。便于用户直接定位显示所需要的大小和位置

实现效果:

我称这个功能模块为EditBox

View Code
  1 package FinalClass;
2
3 import java.awt.Color;
4 import java.awt.Cursor;
5 import java.awt.Graphics;
6
7 import javax.swing.ImageIcon;
8 import javax.swing.JLabel;
9
10 import Util.Event;
11
12 import FileSys.ValueAttribute;
13
14 public class EditBox extends JLabel implements ValueAttribute {
15
16 /**
17 *
18 */
19 private static final long serialVersionUID = 1L;
20 private int px;
21 private int py;
22 private int posx;
23 private int posy;
24 private int width;
25 private int heigh;
26 private int readytochange = 0;
27 private int border = 4;
28 private boolean isEditable = false;
29 private ImageIcon img = null;
30 private String imgUrl;
31 private String valueName = "";
32 private int valueId = 0;
33 private int color = 0x00ff00;
34 private static int count=0;
35 private Event e;
36
37 public EditBox() {
38 this.setBounds(10, 10, 40, 40);
39 this.setFocusable(true);
40 valueName="EditBox"+count;
41 count++;
42 Action();
43 }
44
45 public void addEvent(Event e)
46 {
47 this.e=e;
48 }
49 public void setImg(String url) {
50 img = new ImageIcon(url);
51 this.imgUrl=url;
52 this.repaint();
53 }
54 public String getImgUrl()
55 {
56 return imgUrl;
57 }
58 private void getParam() {
59 posx = this.getX();
60 posy = this.getY();
61 width = this.getWidth();
62 heigh = this.getHeight();
63 }
64 @Override
65 public void setTop()
66 {
67 this.getParent().setComponentZOrder(this, 0);
68 }
69
70 @Override
71 public void paint(Graphics g) {
72 // TODO Auto-generated method stub
73 super.paint(g);
74 g.setColor(new Color(color));
75 if (isEditable) {
76 g.fillRect(0, 0, border, border);
77 g.fillRect(this.getWidth() - border, 0, border, border);
78 g.fillRect(0, this.getHeight() - border, border, border);
79 g.fillRect(this.getWidth() - border, this.getHeight() - border,
80 border, border);
81
82 g.drawLine(border / 2, border / 2, this.getWidth() - border / 2,
83 border / 2);
84 g.drawLine(border / 2, border / 2, border / 2, this.getHeight()
85 - border / 2);
86 g.drawLine(border / 2, this.getHeight() - border / 2, this.getWidth()
87 - border / 2, this.getHeight() - border / 2);
88 g.drawLine(this.getWidth() - border / 2, this.getHeight() - border / 2,
89 this.getWidth() - border / 2, border / 2);
90 }
91 if (img != null)
92 g.drawImage(img.getImage(), border / 2, border / 2, this.getWidth()
93 - border, this.getHeight() - border, this);
94
95 }
96
97 private void isRTC() {
98 readytochange = 0;
99 this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
100 if (px > 0 && px < border) {
101 if (py < border) {
102 readytochange = 1;
103 this.setCursor(Cursor
104 .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
105 }
106 if (py > this.getHeight() - border) {
107 readytochange = 2;
108 this.setCursor(Cursor
109 .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
110 }
111 }
112 if (px > this.getWidth() - border) {
113 if (py < border) {
114 readytochange = 4;
115 this.setCursor(Cursor
116 .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
117 }
118 if (py > this.getHeight() - border) {
119 readytochange = 3;
120 this.setCursor(Cursor
121 .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
122 }
123 }
124 }
125
126 private void changeBox(int mx, int my) {
127 getParam();
128 int wr = mx - px;
129 int hr = my - py;
130 px = mx;
131 py = my;
132 if (readytochange == 1) {
133 this.setBounds(posx + wr, posy + hr, width - wr, heigh - hr);
134 px -= wr;
135 py -= hr;
136 }
137 if (readytochange == 2) {
138 this.setBounds(posx + wr, posy, width - wr, heigh + hr);
139 px -= wr;
140 }
141 if (readytochange == 3) {
142 this.setBounds(posx, posy, width + wr, heigh + hr);
143 }
144 if (readytochange == 4) {
145 this.setBounds(posx, posy + hr, width + wr, heigh - hr);
146 py -= hr;
147 }
148 }
149
150 private void Action() {
151 final EditBox self = this;
152 this.addMouseListener(new java.awt.event.MouseAdapter() {
153 public void mousePressed(java.awt.event.MouseEvent e) {
154 if (isEditable) {
155 px = e.getX();
156 py = e.getY();
157 isRTC();
158 }
159 }
160
161 });
162 this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
163 public void mouseDragged(java.awt.event.MouseEvent e) {
164 if (isEditable) {
165 if (readytochange != 0) {
166 changeBox(e.getX(), e.getY());
167 } else {
168 int x = e.getX() + self.getX() - px, y = e.getY()
169 + self.getY() - py, width = self.getWidth(), heigh = self
170 .getHeight();
171 self.setBounds(x, y, width, heigh);
172 }
173 self.repaint();
174 }
175 }
176
177 public void mouseMoved(java.awt.event.MouseEvent e) {
178 if (isEditable) {
179 px = e.getX();
180 py = e.getY();
181 isRTC();
182 }
183 }
184 });
185 }
186
187 @Override
188 public int getValueId() {
189 // TODO Auto-generated method stub
190 return valueId;
191 }
192
193 @Override
194 public String getValueName() {
195 // TODO Auto-generated method stub
196 return valueName;
197 }
198
199 @Override
200 public void setValueName(String valueName) {
201 // TODO Auto-generated method stub
202 this.valueName = valueName;
203 if(e!=null) e.Change();
204 }
205 @Override
206 public void setColor(int color) {
207 this.color = color;
208 this.repaint();
209 }
210
211 public int getColor() {
212 return color;
213 }
214 @Override
215 public void setValueEditable(boolean isEditable) {
216 // TODO Auto-generated method stub
217 this.isEditable = isEditable;
218 this.repaint();
219 }
220
221 @Override
222 public ValueAttribute menuAdd() {
223 // TODO Auto-generated method stub
224 return null;
225 }
226
227 @Override
228 public ValueAttribute menuDel() {
229 // TODO Auto-generated method stub
230 return null;
231 }
232
233 @Override
234 public ValueAttribute menuEdit() {
235 // TODO Auto-generated method stub
236 EBDialog dia=new EBDialog();
237 dia.setEditBox(this);
238 return this;
239 }
240
241 }

 

posted @ 2012-03-14 10:40  HeiMan  阅读(624)  评论(0编辑  收藏  举报