4
JScrollPane 내부에서 JPanel의 크기를 조정하고 마우스가 현재 위치하는 JPanel의 포인트가 크기 변경 후 JScrollPane (확대/축소 할 때 Google지도처럼 사용)JScrollPane에서 JPanel의 크기를 조정할 때 상대적인 마우스 위치 유지
JPanel에서 마우스 위치를 찾았습니다. 뷰포트가 다양한 위치에있는 것을 처리 할 수 있습니다. 확대/축소 인수를 곱하여 포인트가 스케일링 된 후의 위치를 알 수 있습니다. 그런 다음 ScrollPane에서 마우스의 위치를 뺀 다음 볼 수있는 영역과 관련된 점의 위치를 확인합니다. 그러나 나는 잘못된 것을하고 있으며, 나는 무엇을 볼 수 없습니다.
예제 코드 :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Test
{
public static void main(String[] in)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
public Test()
{
final JFrame frame = new JFrame();
final ScalablePanel child = new ScalablePanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(child, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class ScalablePanel
extends JScrollPane
implements MouseWheelListener
{
final double ZOOM_IN_FACTOR = 1.1;
final double ZOOM_OUT_FACTOR = 0.9;
final JPanel zoomPanel = new JPanel();
public ScalablePanel()
{
final javax.swing.JLabel marker = new javax.swing.JLabel("Testing the mouse position on zoom");
marker.setHorizontalAlignment(javax.swing.JLabel.CENTER);
zoomPanel.setLayout(new BorderLayout());
zoomPanel.add(marker,BorderLayout.CENTER);
getViewport().setView(zoomPanel);
setPreferredSize(new Dimension(300,300));
addMouseWheelListener(this);
}
public void mouseWheelMoved(final MouseWheelEvent e)
{
if (e.isControlDown())
{
if (e.getWheelRotation() < 0)
zoomIn(e);
else
zoomOut(e);
e.consume();
}
}
public void zoomIn(final MouseWheelEvent e)
{
// Get the mouse position with respect to the zoomPanel
final Point pointOnZoomPanel = SwingUtilities.convertPoint(
e.getComponent(), e.getPoint(), zoomPanel);
// Resize panel
final Dimension currSize = zoomPanel.getSize();
zoomPanel.setPreferredSize(
new Dimension(
(int)(currSize.width * ZOOM_IN_FACTOR),
(int)(currSize.height * ZOOM_IN_FACTOR)));
// Find out where our point on the zoom panel is now that we've resized it
final Point newViewPos = new Point();
newViewPos.x = (int)(ZOOM_IN_FACTOR * pointOnZoomPanel.x - e.getPoint().x);
newViewPos.y = (int)(ZOOM_IN_FACTOR * pointOnZoomPanel.y - e.getPoint().y);
// Move the viewport to the new position to keep the area our mouse was in the same spot
getViewport().setViewPosition(newViewPos);
zoomPanel.revalidate();
}
public void zoomOut(final MouseWheelEvent e)
{
// Get the mouse position with respect to the zoomPanel
final Point pointOnZoomPanel = SwingUtilities.convertPoint(
e.getComponent(), e.getPoint(), zoomPanel);
// Resize panel
final Dimension currSize = zoomPanel.getSize();
zoomPanel.setPreferredSize(
new Dimension(
(int)(currSize.width * ZOOM_OUT_FACTOR),
(int)(currSize.height * ZOOM_OUT_FACTOR)));
// Find out where our point on the zoom panel is now that we've resized it
final Point newViewPos = new Point();
newViewPos.x = (int)(ZOOM_OUT_FACTOR * pointOnZoomPanel.x - e.getPoint().x);
newViewPos.y = (int)(ZOOM_OUT_FACTOR * pointOnZoomPanel.y - e.getPoint().y);
// Move the viewport to the new position to keep the area our mouse was in the same spot
getViewport().setViewPosition(newViewPos);
zoomPanel.revalidate();
}
}