java 实现图片的任意拖动

import  javax.swing. * ;
import  java.awt. * ;
import  java.awt.event. * ;
public   class  PanelTest  extends  JPanel
{
    
static int frame_width = 600;
    
static int frame_height = 300;
        
    PicPanel pic 
= null;
    
private int pic_x;
    
private int pic_y;
    
    
//前一个位置
    int begin_x = 0;
    
int begin_y = 0;
    
    
boolean inThePic = false;
    

    
public PanelTest()
    
{
        pic 
= new PicPanel("pic.jpg");
        setLayout(
null);
        add(pic);
        
        pic_x 
= (int)((frame_width - pic.getWidth())/2);
        pic_y 
= (int)((frame_height - pic.getHeight())/2);
        
        pic.setLocation(pic_x, pic_y);  
//定位
        
        
//鼠标动作 监听器 注册
        addMouseListener(
            
new MouseAdapter()
            
{
                
public void mousePressed(MouseEvent e)
                
{
                    
//检测 落点 是否在图片上,只有落点在图片上时 才起作用
                    if(inPicBounds(e.getX(), e.getY()))
                    
{
                        begin_x 
= e.getX();
                        begin_y 
= e.getY();
                        inThePic 
= true;
                    }

                    
//记录当前坐标
                }

                
//释放
                public void mouseReleased(MouseEvent e)
                
{
                    inThePic 
= false;
                }

            }

        );
        
        
//鼠标移动 监听器 注册
        addMouseMotionListener(
            
new MouseMotionAdapter()
            
{
                
public void mouseDragged(MouseEvent e)
                
{
                    
if(inThePic && checkPoint(e.getX(),e.getY()))
                    
{
                        
//边界 检查
                        pic_x =pic_x - (begin_x - e.getX());
                        pic_y 
=pic_y - (begin_y - e.getY());
                        
//System.out.println("pic_x=" + pic_x);
                        begin_x = e.getX();
                        begin_y 
= e.getY();
                        pic.setLocation(pic_x, pic_y);
                    }

                }

            }

        );
    }

    
//-------------帮助方法-----------------//
    
//检测 点(px,py) 是否在图片上
    private boolean inPicBounds(int px,int py)
    
{
        
if(px >= pic_x && px <= pic_x + pic.getWidth() &&
                            py 
>= pic_y && py <= pic_y + pic.getHeight())
            
return true;
        
else
            
return false;
    }

    
    
//越界 检查
    private boolean checkPoint(int px, int py)
    
{
        
if(px <0 || py <0)
            
return false;
        
if(px >getWidth() || py > getHeight())
            
return false;
        
return true;
    }

        
    
public static void main(String[] args)
    
{
        JPanel jpanel 
= new PanelTest();
        JFrame jframe 
= new JFrame("图片拖动");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.getContentPane().add(jpanel);
        jframe.setSize(frame_width, frame_height);
        jframe.setVisible(
true);
    }

}

// 图片面板,只是用来放置图片
class  PicPanel  extends  JPanel
{
    
int p_width = 0;
    
int p_height = 0;
    Image im 
= null;
    
    
int i = 0//temp var
    public PicPanel(String picName)
    
{
        ImageIcon imageIcon 
= new ImageIcon(picName);
        im 
= imageIcon.getImage();
        
        p_width 
= imageIcon.getIconWidth();
        p_height 
= imageIcon.getIconHeight();
        setBounds(
0,0,p_width, p_height);
    }

    
    
public void paint(Graphics g)
    
{
        g.drawImage(im,
0,0,p_width,p_height,null);
    }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值