0

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.

JulioQc
  • 310
  • 1
  • 4
  • 20
  • 1
    Translate to center the center of zoom at [0, 0] -- now do your scaling operation -- then translate back, although the translate back distance will be changed proportional to the amount of scaling performed. – Hovercraft Full Of Eels Jul 25 '14 at 21:31
  • Here's an [example](http://stackoverflow.com/a/3420651/230513) of@HovercraftFullOfEels' suggestion. – trashgod Jul 25 '14 at 21:42
  • yeah... that didnt work out at all... I does keep the picture centred for a zoom or two but then it moves in circles around the panel – JulioQc Jul 26 '14 at 12:35
  • Got it working using the same algorithm proposed, only I had to go thru setTransform instead of translate. The second would not set the X or Y translation to the proper value due to the "state" of the AffineTransform. Anyways got it working :) – JulioQc Jul 26 '14 at 16:24

0 Answers0