[英]Move Over a JPanel With Mouse Dragged
我該如何做才能在添加到JPanel的圖像上滾動? 我使用鼠標拖曳移動了JPanel,但是我不希望整個面板都移動,只是在圖像上滾動即可。
這是我的代碼:
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class WorldMap extends JFrame implements MouseMotionListener {
JPanel panel = new JPanel();
JLabel map = new JLabel();
Image image = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/map/map.jpg"));
ImageIcon icon;
public WorldMap() {
addMouseMotionListener(this);
icon = new ImageIcon(image);
map.setIcon(icon);
panel.add(map);
add(panel);
setTitle("World Map");
setSize(800, 800);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new WorldMap();
}
public void mouseDragged(MouseEvent e) {
e.translatePoint(e.getComponent().getLocation().x, e.getComponent()
.getLocation().y);
panel.setLocation(e.getX(), e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}
是的,您可以使用Component
實現此功能嗎,但是您需要在組件與其父組件之間進行轉換,並承擔布局管理器的職責。
一般來說,更容易“欺騙”並簡單地在組件自身內的不同偏移處繪制圖像,例如
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DraggableImage {
public static void main(String[] args) {
new DraggableImage();
}
public DraggableImage() {
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 ImagePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePane extends JPanel {
private BufferedImage img;
private Point offset = new Point(0, 0);
public ImagePane() {
try {
img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/MT015.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
MouseAdapter ma = new MouseAdapter() {
private Point startPoint;
@Override
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
startPoint.x -= offset.x;
startPoint.y -= offset.y;
}
@Override
public void mouseReleased(MouseEvent e) {
startPoint = null;
}
@Override
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
int x = p.x - startPoint.x;
int y = p.y - startPoint.x;
offset = new Point(x, y);
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();
if (offset == null) {
offset = new Point(0, 0);
}
g2d.drawImage(img, offset.x, offset.y, this);
g2d.dispose();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.