-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBSTView.java
More file actions
214 lines (181 loc) · 6.53 KB
/
BSTView.java
File metadata and controls
214 lines (181 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
public class BSTView extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener{
Boolean tabella = false;
final FinestraBT fbt;
ArrayList<Riga> BSTTab;
ArrayList<NodoGrafico> ElencoNodi;
ArrayList<Arco> ElencoArchi;
JTable table;
JScrollPane scrollPane;
double zoomFactor = 1;
double prevZoomFactor = 1;
boolean zoomer;
boolean dragger;
boolean released;
double xOffset = 0;
double yOffset = 0;
int xDiff;
int yDiff;
Point startPoint;
public BSTView(ArrayList<Riga> BSTTab, ArrayList<NodoGrafico> ng, ArrayList<Arco> ar, FinestraBT fbt) {
this.BSTTab = BSTTab;
this.ElencoArchi = ar;
this.ElencoNodi = ng;
this.fbt = fbt;
table = new JTable(new ModelloBSTTab(BSTTab));
scrollPane = new JScrollPane(table);
this.add(scrollPane);
addMouseListener(this);
setBackground(Color.white);
addMouseWheelListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (zoomer) {
AffineTransform at = new AffineTransform();
double xRel = MouseInfo.getPointerInfo().getLocation().getX() - getLocationOnScreen().getX();
double yRel = MouseInfo.getPointerInfo().getLocation().getY() - getLocationOnScreen().getY();
double zoomDiv = zoomFactor / prevZoomFactor;
xOffset = (zoomDiv) * (xOffset) + (1 - zoomDiv) * xRel;
yOffset = (zoomDiv) * (yOffset) + (1 - zoomDiv) * yRel;
at.translate(xOffset, yOffset);
at.scale(zoomFactor, zoomFactor);
prevZoomFactor = zoomFactor;
g2.transform(at);
zoomer = false;
}
if (dragger) {
AffineTransform at = new AffineTransform();
at.translate(xOffset + xDiff, yOffset + yDiff);
at.scale(zoomFactor, zoomFactor);
g2.transform(at);
if (released) {
xOffset += xDiff;
yOffset += yDiff;
dragger = false;
}
}
if (!tabella) {
scrollPane.setVisible(false);
int x = this.getWidth() / 2;
g.setFont(new Font("Courier", Font.BOLD, 20));
disegnaNodi(g2);
disegnaArchi(g2);
} else {
scrollPane.setVisible(true);
this.add(scrollPane);
}
}
public void ridisegna(boolean tabella) {
this.tabella = tabella;
repaint();
}
public void disegnaNodi(Graphics2D g2) {
g2.setStroke(new BasicStroke(3.0f));
for (NodoGrafico n : ElencoNodi) {
int lungContenuto = n.getContenuto().length();
if (n.getColore() != Color.white) {
g2.setColor(n.getColore());
g2.fillOval(n.getX() - n.getLarghezza() / 2, n.getY() - n.getAltezza() / 2, n.getLarghezza(), n.getAltezza());
}
g2.setColor(Color.black);
g2.drawOval(n.getX() - n.getLarghezza() / 2, n.getY() - n.getAltezza() / 2, n.getLarghezza(), n.getAltezza());
g2.drawString(n.getContenuto(), n.getX() - (lungContenuto * 13) / 2 + 2, n.getY() + 8);
}
}
public void disegnaArchi(Graphics2D g2) {
g2.setStroke(new BasicStroke(3.0f));
for (Arco a : ElencoArchi) {
g2.setColor(a.getColore());
g2.drawLine(a.getxStart(), a.getyStart(), a.getxEnd(), a.getyEnd());
g2.drawString(a.getLabel(), (a.getxStart() + a.getxEnd()) / 2 + 10, (a.getyStart() + a.getyEnd()) / 2);
}
}
public void zoomIn() {
zoomFactor *= 1.1;
repaint();
}
// Metodo per lo zoom out
public void zoomOut() {
zoomFactor /= 1.1;
repaint();
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if (fbt.JTBTab.isEnabled()) {
int coordX = mouseEvent.getX();
int coordY = mouseEvent.getY();
for (NodoGrafico n : ElencoNodi) {
if ((coordX > n.getX() - n.getLarghezza() / 2) & (coordY > n.getY() - n.getAltezza() / 2) & (coordX < n.getX() + n.getLarghezza() / 2) & (coordY < n.getY() + n.getAltezza() / 2)) {
if (!n.getColore().equals(Color.green)) {
n.setColore(Color.green);
if (fbt.JCBNum.isSelected())
fbt.cambiaListaSelezionati("Add", Double.parseDouble(n.getContenuto()));
else
fbt.cambiaListaSelezionati("Add", n.getContenuto());
fbt.resettaSuccPred();
repaint();
} else {
n.setColore(Color.white);
if (fbt.JCBNum.isSelected())
fbt.cambiaListaSelezionati("Del", Double.parseDouble(n.getContenuto()));
else
fbt.cambiaListaSelezionati("Del", n.getContenuto());
repaint();
fbt.resettaSuccPred();
}
}
}
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
released = false;
startPoint = MouseInfo.getPointerInfo().getLocation();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
released = true;
repaint();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
@Override
public void mouseDragged(MouseEvent e) {
Point curPoint = e.getLocationOnScreen();
xDiff = curPoint.x - startPoint.x;
yDiff = curPoint.y - startPoint.y;
dragger = true;
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoomer = true;
if (e.getWheelRotation() < 0) {
zoomFactor *= 1.1;
repaint();
}
if (e.getWheelRotation() > 0) {
zoomFactor /= 1.1;
repaint();
}
}
}