Skip to content

Commit 89b7b16

Browse files
committed
improved pan and zoom
1 parent 1b26178 commit 89b7b16

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

src/main/java/com/marginallyclever/donatello/NodeGraphViewPanel.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.awt.font.FontRenderContext;
1414
import java.awt.font.TextLayout;
1515
import java.awt.geom.AffineTransform;
16+
import java.awt.geom.NoninvertibleTransformException;
1617
import java.util.ArrayList;
1718
import java.util.List;
1819

@@ -149,8 +150,8 @@ public void mouseDragged(MouseEvent e) {
149150
Point now = new Point(e.getX(),e.getY());
150151
if(SwingUtilities.isMiddleMouseButton(e)) {
151152
Point delta = new Point(now.x- previousMouse.x,now.y- previousMouse.y);
152-
camera.x += zoom * delta.x;
153-
camera.y += zoom * delta.y;
153+
camera.x -= zoom * delta.x;
154+
camera.y -= zoom * delta.y;
154155
repaint();
155156
}
156157
previousMouse.setLocation(now);
@@ -165,12 +166,16 @@ public void mouseMoved(MouseEvent e) {
165166
super.mouseMoved(e);
166167
}
167168
});
168-
/*
169+
169170
this.addMouseWheelListener(e -> {
170171
double notches = e.getWheelRotation() * 0.1;
172+
173+
Rectangle r = getBounds();
174+
int w2 = (int)(r.getWidth()/2.0);
175+
int h2 = (int)(r.getHeight()/2.0);
171176
setZoom(getZoom() - notches);
172177
repaint();
173-
});*/
178+
});
174179
}
175180

176181
@Override
@@ -184,52 +189,57 @@ protected void paintComponent(Graphics g) {
184189

185190
super.paintComponent(g);
186191

187-
paintNodesInBackground(g);
192+
g2.transform(getTransform());
188193

189-
Rectangle r = getBounds();
190-
int w2 = (int)(r.getWidth()/2.0);
191-
int h2 = (int)(r.getHeight()/2.0);
192-
AffineTransform tf = new AffineTransform();
193-
double dx=camera.x;
194-
double dy=camera.y;
195-
tf.scale(1.0/zoom, 1.0/zoom);
196-
tf.translate(dx*zoom,dy*zoom);
197-
g2.transform(tf);
194+
paintNodesInBackground(g);
198195

199196
for(Node n : model.getNodes()) paintNode(g2,n);
200197

201198
g2.setColor(CONNECTION_COLOR);
202199
for(NodeConnection c : model.getConnections()) paintConnection(g2,c);
203200

204-
int z = (int)(zoom*10);
205-
g2.drawOval(-z,-z,z*2,z*2);
201+
//paintCursor(g2);
202+
//paintOrigin(g2);
206203

204+
firePaintEvent(g2);
205+
}
206+
207+
private void paintCursor(Graphics2D g2) {
208+
g2.setColor(Color.WHITE);
209+
int z = (int)(zoom*10);
207210
Point transformed = transformMousePoint(previousMouse);
208211
g2.translate(transformed.x,transformed.y);
209-
g2.setColor(Color.WHITE);
210212
g2.drawOval(-z,-z,z*2,z*2);
211213
g2.translate(-transformed.x,-transformed.y);
214+
}
212215

213-
transformed.setLocation(transformMousePoint(new Point((int)dx,(int)dy)));
214-
g2.translate(transformed.x,transformed.y);
215-
g2.setColor(Color.ORANGE);
216+
private void paintOrigin(Graphics2D g2) {
217+
g2.setColor(Color.BLUE);
218+
int z = (int)(zoom*10);
216219
g2.drawOval(-z,-z,z*2,z*2);
217-
g2.translate(-transformed.x,-transformed.y);
218-
219-
firePaintEvent(g2);
220220
}
221221

222-
public Point transformMousePoint(Point point) {
222+
AffineTransform getTransform() {
223223
Rectangle r = getBounds();
224224
int w2 = (int)(r.getWidth()/2.0);
225225
int h2 = (int)(r.getHeight()/2.0);
226-
AffineTransform tf = new AffineTransform();
227-
double dx=camera.x;
228-
double dy=camera.y;
229-
tf.scale(1.0/zoom, 1.0/zoom);
230-
tf.translate(dx,dy);
226+
AffineTransform tx = new AffineTransform();
227+
double dx=camera.x-w2*zoom;
228+
double dy=camera.y-h2*zoom;
229+
tx.scale(1.0/zoom, 1.0/zoom);
230+
tx.translate(-dx,-dy);
231+
return tx;
232+
}
233+
234+
public Point transformMousePoint(Point point) {
235+
AffineTransform tf = getTransform();
231236
java.awt.geom.Point2D from = new java.awt.geom.Point2D.Double(point.x,point.y);
232-
java.awt.geom.Point2D to = tf.transform(from,null);
237+
java.awt.geom.Point2D to = new java.awt.geom.Point2D.Double();
238+
try {
239+
tf.inverseTransform(from,to);
240+
} catch (NoninvertibleTransformException e) {
241+
e.printStackTrace();
242+
}
233243

234244
return new Point((int)to.getX(),(int)to.getY());
235245
}
@@ -262,7 +272,6 @@ public void updatePaintAreaBounds() {
262272
this.setMinimumSize(d);
263273
this.setMaximumSize(d);
264274
this.setPreferredSize(d);
265-
//System.out.println("Bounds="+r.toString());
266275
}
267276

268277
/**

0 commit comments

Comments
 (0)