【发布时间】:2018-01-11 05:24:00
【问题描述】:
我有一个正在绘制的 JPanel,我正在尝试获取它,以便您可以使用鼠标滚轮通过修改可变比例来“放大”和缩小。现在发生的情况是您可以放大或缩小,但是当您放大时所有绘图都会向右和向下移动,然后在缩小时向上和向左移动。
我想让它调整,就好像你在放大 JPanel 中心的点一样,但我不知道如何计算正确的偏移量来翻译……
有人知道如何做到这一点,或者更简洁的方法来实现整个平移和缩放功能吗?
我基本上是在绘制一堆坐标,这些坐标来自一个系统,其中 0,0 位于左下角并且位于边界之间:
xMin = 661208
xMax = 662618
yMin = 4291657
yMax = 4293285
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double scale = details.getScale();
double xOffset = details.getxOffset();
double yOffset = details.getyOffset();
g2.scale(scale, -scale);
// Dividing offset by scale makes panning 1:1 with the cursor still. yMax to account for the
// fact we needed to flip around the y axis to make it right-side up.
g2.translate((-xMin + xOffset / scale), (-yMax + yOffset / scale));
// Code to draw stuff goes here. It uses coordinates between xMin-xMax and yMin-yMax to draw.
.
.
.
}
【问题讨论】:
标签: java graphics scale translate