我正在尝试通过单击并拖动它来制作滚动视图滚动。它确实滚动但不是很好,它看起来像是在正确的方向滚动和在相反的方向滚动之间快速交替。无论如何,这是我到目前为止所得到的 (这是在自定义鼠标适配器中):
point p;
JViewport viewport;
@Override
public void mousePressed(MouseEvent e)
{
p.setLocation(e.getX(),e.getY());
}
@Override
public void mouseDragged(MouseEvent e)
{
Point ep = new Point();
ep.setLocation(e.getX(),e.getY());
double changex = e.getX() - p.getX();
double changey = e.getY() - p.getY();
Point q = viewport.getViewPosition();
q.setLocation(q.getX()-changex, q.getY()-changey);
p.setLocation(ep);
viewport().setViewPosition(q);
}
答案 0 :(得分:0)
好的,所以我对这个想法有点玩耍。
基本上,当您单击时,我会计算您单击的位置和当前视口位置的偏移量。
当拖动鼠标时,我基本上用点击偏移来抵消拖动位置,在进行一些边界检查后,我只需设置视口位置......
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestScrollPane101 {
public static void main(String[] args) {
new TestScrollPane101();
}
public TestScrollPane101() {
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 JScrollPane(new TestPane()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage img;
public TestPane() {
try {
img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/_cg_836___Tilting_Windmills___by_Serena_Clearwater.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
MouseAdapter ma = new MouseAdapter() {
private Point offset;
@Override
public void mouseDragged(MouseEvent e) {
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport view = (JViewport) parent;
Dimension size = view.getViewRect().getSize();
Point mp = e.getPoint();
Point vp = new Point();
vp.x = mp.x - offset.x;
vp.y = mp.y - offset.y;
System.out.println("Guess = " + vp);
if (vp.x < 0) {
System.out.println("Under x");
vp.x = 0;
} else if (vp.x + size.width >= getWidth()) {
System.out.println("Over x - " + (vp.x + size.width) + " >= " + getWidth());
vp.x = getWidth() - size.width;
}
if (vp.y < 0) {
System.out.println("Under Y");
vp.y = 0;
} else if (vp.y + size.height >= getHeight()) {
System.out.println("Over Y");
vp.y = getHeight() - size.height;
}
System.out.println("Want = " + vp);
view.setViewPosition(vp);
}
}
@Override
public void mouseReleased(MouseEvent e) {
offset = null;
}
@Override
public void mousePressed(MouseEvent e) {
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport view = (JViewport) parent;
Point vp = view.getViewPosition();
Point mp = e.getPoint();
offset = new Point();
offset.x = mp.x - vp.x;
offset.y = mp.y - vp.y;
}
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
g2d.dispose();
}
}
}
}