2
我有一個JPanel我正在繪製,我試圖獲得它,以便您可以使用鼠標滾輪通過修改變量「縮放」進出規模。現在會發生什麼,您可以放大或縮小,但是當您放大時,所有繪圖會向右和向下移動,然後在縮小時向後並向左移動。Java Graphics2D平移和縮放/翻譯和縮放
我想讓它,使它調整,如果你是在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.
.
.
.
}
此問題可能會幫助你[http://stackoverflow.com/questions/690871/affinetransform-scaling-a-shape-from-its-center](http://stackoverflow.com/questions/690871/affinetransform -scaling-A-形狀從 - 其中心) – Pierre 2009-09-22 08:03:30