放大和缩小jPanel [英] Zoom in and out of jPanel
问题描述
我想知道使用面板的paintComponent进行缩放后,检索鼠标clic位置的最佳方法是什么? (相对于此缩放的位置).
I am wondering what is the best way to retrieve the location of a mouse clic after a zoom was made using paintComponent of a panel? (The location relative to this zoom).
这是缩放代码:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(zoomer==true){
at.scale(zoom,zoom);//at = AffineTransform...
zoomer=false;
}
g2.transform(at);
在我的主类中,我使用鼠标侦听器私有类并重写mouseRelase方法:
In my main class, I use a mouse listener private class and override the mouseRelase method :
@Override
public void mouseReleased(MouseEvent e)
e.getX();//Location of the mouse on the X axis
我正在绘制不同的形状,例如点.每个点都存储有其像素"位置,因此,如果在X:20和Y:30进行创建,则此点永远不会改变.当我调用paintComponent时,它会缩放得更大或更小.我的问题是,我可以把握这些点在面板上的位置,它可以在一些textFields中为我提供一些有关单击的点的信息.如果面板已放大/缩小,则clic的位置仍然相同,因此,假设我放大了,形状将不会位于x:20和y:30,而是基于比例尺的随机位置.我该如何调整我的老巢位置?
I am drawing different shapes such as dots. Every dot has it's Pixels locations stored so if a create it at X:20 and Y:30 this never changes. When I call paintComponent, it is scaled either bigger or smaller. My problem is, I can clic on the location of these dots on the panel and it gives me some information in some textFields about the dots I clicked on. If the panel has been zoomed in/out, the location of the clic is still the same so let's say I zoom in, the shapes won't be at x:20 and y:30 but at a random location based on the scale. How can I ajust my clic location to this scale?
不确定我是否清楚自己的意思,如果您需要更多信息,请不要犹豫.
Not sure if I am making myselft clear so do not hesitate if you need more information.
谢谢
推荐答案
好吧,我整夜都在工作,终于找到了解决方法.
Alright, I have been working all night long and finally figured out how to do this.
首先,这是当我滚动鼠标滚轮时发生的事情:
First of all here's what happens when I roll the mouse wheel :
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
//Zoom in
if(e.getWheelRotation()<0){
mydrawer.setZoomFactor(1.1*mydrawer.getZoomFactor());
mydrawer.repaint();
}
//Zoom out
if(e.getWheelRotation()>0){
mydrawer.setZoomFactor(mydrawer.getZoomFactor()/1.1);
mydrawer.repaint();
}
}
setZoomFactor方法执行此操作:
The setZoomFactor method does this :
public void setZoomFactor(double factor){
if(factor<this.zoomFactor){
this.zoomFactor=this.zoomFactor/1.1;
}
else{
this.zoomFactor=factor;
}
this.zoomer=true;
}
之后,当我调用repaint()时,我将Graphics2D对象转换为如下所示的缩放比例:
After, when I call repaint() I transform my Graphics2D object to zoom like this :
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(zoomer==true){
at = new AffineTransform();
at.scale(zoomFactor,zoomFactor);
zoomer=false;
g2.transform(at);
}
}
在这一点上,没有什么新的...我的问题是,当我在缩放后单击绘制的对象时,我需要调整相对于缩放系数的clic位置.就我而言,我需要将Y轴的0设置为默认情况下的左下角而不是左上角,这就是为什么我必须根据面板高度反转该值的原因:
At this point, nothing really new... My problem was when I clicked on a drawn objet after zoom, I needed to ajust the clic location relative to the zoom factor. In my case, I needed the 0 of the Y axis to be at the bottom left corner instead of top left by default which is why I had to reverse the value based on the panel height :
@Override
public void mouseReleased(MouseEvent e) {
int myXLocationWithoutZoom = e.getX()*(1/myDrawer.getZoomFactor());
int myYLocationWithoutZoom = myPanel.getSize().height-((e.getY())*(1/myDrawer.getZoomFactor()));
如果您想出另一种方法来做这件事,或者认为我没有做这件事的好方法,请告诉我.
If you figure out another way to do this or think I did not do it the good way, let me know.
谢谢
这篇关于放大和缩小jPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!