使用仿射变换放大 [英] Zoom in with affinetransform swing

查看:187
本文介绍了使用仿射变换放大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAVA swing开展我的个人项目。

I'm working on my personal project using JAVA swing.

该项目是关于在窗口上绘制地图。

The project is about drawing a map on a window.

地图可以使用仿射变换进行缩放。
我在这里遇到的问题是每当我放大或缩小地图时,也会移动而不是放大/缩小位于屏幕中心的地图上的点

The map is zoomable using affinetransform. The problem I'm having here is whenever I zoom in or out the map also shifts instead of zooming in/out on the point of the map that is at the center of the screen

private void updateAT()
{
    Dimension d = panel.getSize();
    int panelW = d.width;
    int panelH = d.height;

    Rectangle2D r = regionList[0].get("Victoria").getShape().getBounds2D();

    scaleX = (panelW/r.getWidth()) * zoom;
    scaleY = (panelH/r.getHeight()) * zoom;

    AffineTransform goToOrigin = AffineTransform.getTranslateInstance(-r.getMinX(), -r.getMinY());

    AffineTransform pan = AffineTransform.getTranslateInstance(panX, panY);

    AffineTransform scaleAndFlip = AffineTransform.getScaleInstance(scaleX, -scaleY);

    //AffineTransform mirror_y = new AffineTransform(1, 0, 0, -1, 0, panelH);

    AffineTransform centre = AffineTransform.getTranslateInstance(panelW/2, panelH/2);
    centre.translate(-((r.getWidth()*scaleX)/2), ((r.getHeight()*scaleY)/2));

    world2pixel.setToIdentity();
    //world2pixel.concatenate(mirror_y);
    world2pixel.concatenate(pan);
    world2pixel.concatenate(centre);
    world2pixel.concatenate(scaleAndFlip);
    world2pixel.concatenate(goToOrigin);
}


推荐答案

示例居中并缩放形状 p3 ,by

This example centers and scales a Shape, p3, by


  • 将对称点转换为原点,

  • translating a point of symmetry to the origin,

缩放多边形然后

转换回面板中心。

请注意,连接操作是在声明顺序的(明显)反向执行,如上所述这里

Note that the concatenated operations are performed in the (apparent) reverse of the declaration order, as discussed here.

at.setToIdentity();
at.translate(w / 2, h / 2);
at.scale(scale, scale);
at.translate(-p3x[5] + 10, -p3y[5]); 

这篇关于使用仿射变换放大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆