Java 在JPanel中拖动BuffereImage
我在JFrame中有JPanel,JPanel中有不止一个这样的BuffereImage。 现在我的问题是如何在JPanel中移动BuffereImage?更具体地说,如何将MouseEventhandler添加到缓冲图像?。虽然我可以从下面的代码中拖动JPanel,但我不知道如何在JPanel中拖动缓冲图像。谢谢你的帮助 我有三节这样的课 MainWindow.JavaJava 在JPanel中拖动BuffereImage,java,swing,Java,Swing,我在JFrame中有JPanel,JPanel中有不止一个这样的BuffereImage。 现在我的问题是如何在JPanel中移动BuffereImage?更具体地说,如何将MouseEventhandler添加到缓冲图像?。虽然我可以从下面的代码中拖动JPanel,但我不知道如何在JPanel中拖动缓冲图像。谢谢你的帮助 我有三节这样的课 MainWindow.Java public class MainWindow extends JFrame { public sta
public class MainWindow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
}
}
});
}
public MainWindow() {
JFrame frame = new JFrame();
.... // Other code of JFrame
JPanel panel_3 = new JPanel();
.... // other code of JPanel
frame.getContentPane().add(panel_3);
panel_3.setLayout(new BorderLayout(0, 0));
Drawing drawingObj = new Drawing();
panel_3.removeAll();
panel_3.add(drawingObj );
drawingObj .revalidate();
drawingObj .repaint();
BasicDragging drag= new BasicDragging();
panel_3.addMouseListener(drag);
panel_3.addMouseMotionListener(drag);
}
}
Drawing.java
public class Drawing extends JPanel {
private BufferedImage Image1 ;
private BufferedImage Image2;
private BufferedImage Image3;
public Drawing() {
try {
Image1 = ImageIO.read(new File("path of file"));
Image2 = ImageIO.read(new File("path of file"));
Image3 = ImageIO.read(new File("path of file"));
}
catch (IOException ex) {
// handle exception...
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(Image1 , 150, 10, null);
g.drawImage(Image2 , 150, 70,null);
// Other code of drawing images
}
}
}
BasicDragging.java
public class BasicDragging extends MouseInputAdapter{
Point location;
MouseEvent pressed;
public void mousePressed(MouseEvent me)
{
pressed = me;
}
public void mouseDragged(MouseEvent me)
{
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
我的第一个建议很简单 正如你所看到的,你通过坐标x和y来绘制这些图像。 你可以在JPanel中添加鼠标听器。这就是魔法已经发生的地方,如果你创建更多的对象,它将相当混乱 现在,您只需检查鼠标是否悬停在图像上方并按下。 您只需要计算大小以及点是否位于图像内部。 如果它在内部,则图像将沿鼠标方向移动。 你不应该重置位置,而应该移动它。 就是这样做的。 只需在单击并按下时设置图像的x和y 我将创建一个对象绘图,其中包含表示x和y及其大小的核心BuffereImage+坐标。此外,我还要添加一个函数,用于计算公共布尔值Ishoveringit x,int y。 应该会成功的。在绘图函数中使用x和y坐标
我希望您理解我想说的而不是使用图像然后编写代码来确定单击了哪个图像您可以使用带有图像图标的JLabel来显示图像。然后,您可以将鼠标侦听器添加到JLabel并在面板周围拖动标签 使用此方法,拖动组件的基本代码为:
public class DragListener extends MouseInputAdapter
{
Point location;
MouseEvent pressed;
public void mousePressed(MouseEvent me)
{
pressed = me;
}
public void mouseDragged(MouseEvent me)
{
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
DragListener drag = new DragListener();
component.addMouseListener( drag );
component.addMouseMotionListener( drag );
使用此类的代码为:
public class DragListener extends MouseInputAdapter
{
Point location;
MouseEvent pressed;
public void mousePressed(MouseEvent me)
{
pressed = me;
}
public void mouseDragged(MouseEvent me)
{
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - pressed.getX() + me.getX();
int y = location.y - pressed.getY() + me.getY();
component.setLocation(x, y);
}
}
DragListener drag = new DragListener();
component.addMouseListener( drag );
component.addMouseMotionListener( drag );
您还可以查看添加了一些更高级拖动功能的
您需要在面板上使用空布局,并设置标签的大小/位置。因此,您可能希望使用简化此过程的