Hi,
Im creating something that requires I pan and zoom some graphical data.
So below is the code I have come up with which almost works but Im really struggling to get my head around this whole thing.
So the idea of these methods is that I pop in some absolute coordinates and it gives me the position in which to draw the coordinate in the display (a JPanel).
Im sure you can guess what xScale and yScale are for and xOrigin and yOrigin are used to offset the drawing in relation to panning.
public double getRelativeX(double x){
double relativeX = (x* xScale) + xOrigin +(getWidth()/2);
return relativeX;
}
public double getRelativeY(double y){
double relativeY = (y * -yScale) - yOrigin+(getHeight()/2);
return relativeY;
}
At the moment it does the job but when I zoom it zooms about the absolute point 0,0 when I want it to do it about the centre of the screen, any ideas how I need to change it?
Also this is my zooming/panning code:
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isMiddleMouseButton(e)){
// capture starting point
lastOffsetX = e.getX();
lastOffsetY = e.getY();
}
}
public void mouseDragged(MouseEvent e) {
if(SwingUtilities.isMiddleMouseButton(e)){
// new x and y are defined by current mouse location subtracted
// by previously processed mouse location
int newX = e.getX() - lastOffsetX;
int newY = e.getY() - lastOffsetY;
// increment last offset to last processed by drag event.
lastOffsetX += newX;
lastOffsetY += newY;
// update the canvas locations
xOrigin += newX;
yOrigin -= newY;
// schedule a repaint.
repaint();
}
}
[quote]public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
xScale -= (mouseSensitivity * e.getWheelRotation());
yScale -= (mouseSensitivity * e.getWheelRotation());
xScale = Math.max(0.001, xScale);
yScale = Math.max(0.001, yScale);
repaint();
}
}
[/quote]
One last important note, you may notice the X and Y methods are slightly different, thats because I want the coordinate system to start from the bottom left and not the top left corner and increase as you move upwards not downwards.
Thanks for any help,
Ken.