I'm doing a small program as a school assignment. Its rather simple; open an image in a JPanel and then be able to zoom (in or out) and translate using the mouse dragged. I got the translation working so far and the zoom works but its not centered. I would like the zoom to be centered, even after a translation was done. Here is my class so far:
package modele;
import java.awt.geom.AffineTransform;
import java.util.Observable;
import java.io.Serializable;
import vue.JPanelImage;
public class Perspective extends Observable implements Serializable {
private static final long serialVersionUID = 1L;
private JPanelImage panneauSource;
private AffineTransform transformation;
private double zoom = 1;
public Perspective(JPanelImage panneauImage) {
this.panneauSource = panneauImage;
this.transformation = new AffineTransform();
this.addObserver(panneauImage);
}
public AffineTransform getTransformation() {
return transformation;
}
public void setTransformation(AffineTransform transformation) {
this.transformation = transformation;
setChanged();
notifyObservers();
}
public void zoom(double wheelRotation) {
zoom = 1;
zoom += (.1 * -wheelRotation);
System.out.println("Zoom: " + zoom);
transformation.scale(zoom, zoom);
setChanged();
notifyObservers();
}
public void translate(double[] coordSouris) {
double newX = coordSouris[2] - coordSouris[0];
double newY = coordSouris[3] - coordSouris[1];
transformation.translate(newX*3, newY*3);
setChanged();
notifyObservers();
}
}
Any idea how to get this working? This project is made under the constraint of using various programming templates such as Command and Singleton. So the methods zoom and translate will be called at each mousescroll and mousedrag event. And sorry for the french!
Thanks for the help.