我正在做一个小型课程作为学校作业。它相当简单;在JPanel中打开图像,然后可以使用拖动的鼠标进行缩放(输入或输出)和翻译。到目前为止我的翻译工作和缩放工作,但它没有居中。即使翻译完成后,我也想让缩放居中。到目前为止,这是我的班级:
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();
}
}
知道如何让这个工作吗?该项目是在使用各种编程模板(如Command和Singleton)的约束下完成的。因此,每个mousescroll和mousedrag事件都会调用zoom和translate方法。抱歉法国人!
感谢您的帮助。