-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainObject.java
More file actions
210 lines (162 loc) · 6.24 KB
/
MainObject.java
File metadata and controls
210 lines (162 loc) · 6.24 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
200
201
202
203
204
205
206
207
208
209
210
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.event.MouseInputListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.Math;
public abstract class MainObject extends JButton{
private static final long serialVersionUID = 1L;
public abstract void draw(Graphics g);
public boolean selected = false;
int connection_port_width = 5;
GUI main_GUI;
int press_start_position_x, press_start_position_y;
public ArrayList<connection_port> ports;
connection_port north_port, east_port, south_port, west_port;
public CompositeGroup group_belong = null; //belong to which group
public MainObject(String btn_name, GUI m, int x, int y, int width, int heigh) {
super(btn_name);
setContentAreaFilled(false);
setOpaque(false);
setBorderPainted(false);
setBounds(x , y ,width,heigh);
setForeground(Color.BLACK);
setBackground(Color.WHITE);
// .set
setVerticalAlignment(SwingConstants.TOP);
this.main_GUI = m;
this.addMouseListener(new CanvasMouseEvent(this));
this.addMouseMotionListener(new CanvasMouseEvent(this));
this.north_port = new connection_port(width/2, 0);
this.east_port = new connection_port(width, heigh/2);
this.south_port = new connection_port(width/2, heigh);
this.west_port = new connection_port(0, heigh/2);
ports = new ArrayList<connection_port>(Arrays.asList(north_port, east_port, south_port, west_port));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
draw(g);
for(MainObject obj: main_GUI.selected_object) {
obj.selected = true;
}
}
public class connection_port{
int x;
int y;
public connection_port(int x, int y) {
this.x = x;
this.y = y;
}
}
public connection_port get_nearest_port(int x, int y) {
double distance = 99999;
connection_port nearest_port = ports.get(1);
for(connection_port p: ports) {
double d = Math.hypot((p.x - x), (p.y - y));
if(d<distance){
distance = d;
nearest_port = p;
}
}
return nearest_port;
}
public class CanvasMouseEvent extends MouseAdapter implements MouseInputListener{
MainObject current_obj;
public CanvasMouseEvent(MainObject obj) {
this.current_obj = obj;
}
public void mouseClicked(MouseEvent event) {
main_GUI.clear_selected_list();
// click on group element
if(current_obj.group_belong!=null){
CompositeGroup root_group = CompositeGroup.get_root_group(current_obj.group_belong);
current_obj.group_belong.select_all_object_relative(root_group);
return;
}
main_GUI.selected_object.add(current_obj);
System.out.println("mouseClicked selected_object.size():"+main_GUI.selected_object.size());
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if(main_GUI.current_clicked_button.getClass().getName()=="Select") {
// 如果使用者點擊A物件,卻跑到B物件上面去拖曳
if(!main_GUI.selected_object.contains(current_obj) ) {
System.out.println("使用者點擊A物件,卻跑到B物件上面去拖曳");
main_GUI.clear_selected_list();
//紀錄點擊物件的起始相對位置
current_obj.press_start_position_x = e.getX();
current_obj.press_start_position_y = e.getY();
main_GUI.selected_object.add(current_obj);
}
System.out.println("main_GUI.selected_object: ");
for(MainObject obj: main_GUI.selected_object) {
System.out.println(obj);
obj.setLocation(obj.getLocation().x + e.getX()-current_obj.press_start_position_x, obj.getLocation().y +e.getY()-current_obj.press_start_position_y);
}
System.out.println("=====");
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
// System.out.println("mouseMoved X:"+e.getX()+" Y:"+e.getY());
//滑鼠按住移動到其他物件上
// if(current_obj != main_GUI.canvas_area.pressed_obj) {
// main_GUI.canvas_area.entered_obj = current_obj;
// main_GUI.current_clicked_button.CanvasEnterEvent(e.getX(), e.getY(), current_obj);
// }
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mousePressed X:"+e.getX()+" Y:"+e.getY());
//紀錄點擊物件的起始相對位置
current_obj.press_start_position_x = e.getX();
current_obj.press_start_position_y = e.getY();
// if(main_GUI.current_clicked_button.getClass().getName()=="AssociationLine") {
// System.out.println("Pressed with AssociationLine");
// }
main_GUI.canvas_area.pressed_obj = current_obj;
main_GUI.current_clicked_button.CanvasPressEvent(e.getX(), e.getY(), current_obj);
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
// main_GUI.current_clicked_button.CanvasReleaseEvent(e.getX(), e.getY(), current_obj);
System.out.println("mouseReleased X:"+e.getX()+" Y:"+e.getY());
main_GUI.current_clicked_button.CanvasReleaseEvent(e.getX(), e.getY(), current_obj);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
main_GUI.current_clicked_button.CanvasEnterEvent(e.getX(), e.getY(), current_obj);
//
//滑鼠按住移動到其他物件上 - for association line
// if(main_GUI.canvas_area.pressed_obj != null && current_obj != main_GUI.canvas_area.pressed_obj) {
// System.out.println("Enter another object");
// main_GUI.canvas_area.entered_obj = current_obj;
// main_GUI.current_clicked_button.CanvasEnterEvent(e.getX(), e.getY(), current_obj);
// }
// System.out.println("mouseEnter X:"+e.getX()+" Y:"+e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
// main_GUI.canvas_area.entered_obj = null;
main_GUI.current_clicked_button.CanvasExitEvent(e.getX(), e.getY(), current_obj);
// System.out.println("mouseExited X:"+e.getX()+" Y:"+e.getY());
}
}
}