gpt4 book ai didi

java - 鼠标拖动滚动JScrollPane(Java swing)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:07:01 25 4
gpt4 key购买 nike

我正在为我正在开发的游戏制作 map 编辑器。 JScrollPane中有一个JPanel,显示要编辑的 map 。我想要做的是当用户按住空格键并在 JPanel 中拖动鼠标时,JScrollPanel 将随着拖动一起滚动。这是我目前所拥有的:

panelMapPanel.addMouseMotionListener(new MouseMotionListener(){

@Override
public void mouseDragged(MouseEvent e) {
//Gets difference in distance x and y from last time this listener was called
int deltaX = mouseX - e.getX();
int deltaY = mouseY - e.getY();
mouseX = e.getX();
mouseY = e.getY();
if(spacePressed){
//Scroll the scrollpane according to the distance travelled
scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getValue() + deltaY);
scrollPane.getHorizontalScrollBar().setValue(scrollPane.getHorizontalScrollBar().getValue() + deltaX);
}
}

});

目前它可以工作,但滚动一点也不流畅。一次移动鼠标很多是可以的,但小的拖动会使滚动 Pane 变得疯狂。

有什么改进方法吗?

对于那些喜欢视觉的人有帮助,这里是编辑:

Map Editor

补充说明(编辑):

  • 我已经尝试过 scrollPane.getViewport().setViewPosition(new Point(scrollPane.getViewport().getViewPosition().x + deltaX, scrollPane.getViewport().getViewPosition().y + deltaY));
  • 慢速移动鼠标时拖动比较烦躁,大移动时比较流畅
  • 我试过使用 scrollRectToVisible 但运气不好

最佳答案

好吧,这最终比我想的要简单得多......

首先,不要乱用 JViewport ,而是使用 JComponent#scrollRectToVisible直接在充当 JScrollPane 内容的组件上, 在其上 MouseListener应附上。

下面的例子简单地计算了用户点击的点和他们拖动的量之间的差异。然后它将此增量应用于 JViewportviewRect并使用 JComponent#scrollRectToVisible更新可视区域,简单:)

enter image description here

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JLabel map;

public TestPane() {
setLayout(new BorderLayout());
try {
map = new JLabel(new ImageIcon(ImageIO.read(new File("c:/treasuremap.jpg"))));
map.setAutoscrolls(true);
add(new JScrollPane(map));

MouseAdapter ma = new MouseAdapter() {

private Point origin;

@Override
public void mousePressed(MouseEvent e) {
origin = new Point(e.getPoint());
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseDragged(MouseEvent e) {
if (origin != null) {
JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, map);
if (viewPort != null) {
int deltaX = origin.x - e.getX();
int deltaY = origin.y - e.getY();

Rectangle view = viewPort.getViewRect();
view.x += deltaX;
view.y += deltaY;

map.scrollRectToVisible(view);
}
}
}

};

map.addMouseListener(ma);
map.addMouseMotionListener(ma);
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

}

关于java - 鼠标拖动滚动JScrollPane(Java swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com