请教怎么样能用鼠标拖动已经载入到swing组件中的图片?

解决方案 »

  1.   

    做一个时间处理,在鼠标拖动的时候设置图片的坐标,OK了 挺简单的,LZ仔细想想
      

  2.   

    其实可以不用时间的
    在MouseDragged事件里不断把图片放在新的坐标
    然后repaint就可以了啊~
    呵呵~~
    搂主愿意的话可以试试看~~
      

  3.   

    这个是我做的一个拖动图片的例子楼主看下吧 嘿嘿 最好能给分哦嘻嘻 
    class MyPanel extends JPanel implements MouseListener,MouseMotionListener,Runnable,KeyListener
    {
    private Picture activePic;
    private Vector pv= new Vector(10,5);
    private int width=0;
    private int height=0;
    private BufferedImage bi=null;
    Thread thread;
    public MyPanel(int w,int h)
    {
    this.width=w;
    this.height=h;
    this.setFocusable(true);
    this.setVisible(true);
    this.addKeyListener(this);
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
    this.bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
    thread= new Thread(this);
    thread.start();
    }
    public void run()
    {
    while(true)
    {
    repaint();
    try
    {
    Thread.currentThread().sleep(100);
    }
    catch (Exception e)
    {
    System.out.println("sleep error e="+e);
    }
    }
    }
    public void loadPic(String [] name)
    {
    for(int i=0;i<name.length;i++)
    {
    Picture pic = new Picture(new ImageIcon(name[i]).getImage(),0,0);
    pv.addElement(pic);
    pic=null;
    }
    activePic=(Picture)pv.elementAt(pv.size()-1);
    }
    public void keyPressed(KeyEvent e)
    {
    System.out.println("key code="+e.getKeyCode());
    int keyCode = e.getKeyCode();
    switch(keyCode)
    {
    case 16: //左侧shift键 切换激活图片
    for(int i=0;i<pv.size();i++)
    {
    if(((Picture)pv.elementAt(i)).equals(activePic))
    {
    if(i<pv.size()-1)
    activePic=(Picture)pv.elementAt(i+1);
    else
    activePic=(Picture)pv.elementAt(0);
    break;
    }
    }
    case 37: //左
    if(activePic.x>0)
    activePic.x--;
    break;
    case 38: //上
    if(activePic.y>0)
    activePic.y--;
    break;
    case 39:  //右
    if(activePic.x+activePic.width<this.width)
    activePic.x++;
    break;
    case 40: //下
    if(activePic.y+activePic.height<this.height)
    activePic.y++;
    break;
    }
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void keyTyped(KeyEvent e)
    {

    }
    public void mouseClicked(MouseEvent e)
    {
    }
    public void mouseEntered(MouseEvent e)
    {
    }
    public void mouseExited(MouseEvent e)
    {//System.out.println("mouse Exitedvalue="+e.getButton());

    }
    public void mousePressed(MouseEvent e)
    {
    if(pv.isEmpty())
    return;
    System.out.println("mouse clicked x="+e.getX()+" y="+e.getY()+"in activePic?="+(e.getX()>activePic.x&&e.getX()<activePic.x+activePic.width&&
    e.getY()>activePic.y&&e.getY()<activePic.y+activePic.height));
    for(int i=this.pv.size()-1;i>=0;i--)
    {
    if(e.getX()>((Picture)pv.elementAt(i)).x&&e.getX()<((Picture)pv.elementAt(i)).x+((Picture)pv.elementAt(i)).width&&
    e.getY()>((Picture)pv.elementAt(i)).y&&e.getY()<((Picture)pv.elementAt(i)).y+((Picture)pv.elementAt(i)).height)
    {
    this.activePic=(Picture)pv.elementAt(i);
    break;
    }
    }
    if(e.getX()>activePic.x&&e.getX()<activePic.x+activePic.width&&
    e.getY()>activePic.y&&e.getY()<activePic.y+activePic.height)
    {
    activePic.anchorX=e.getX();
    activePic.anchorY=e.getY();
    activePic.focused=true;
    }
    }
    public void mouseReleased(MouseEvent e)
    {
    if(pv.isEmpty())
    return;
    System.out.println("mouse released");
    activePic.anchorX=0;
    activePic.anchorY=0;
    activePic.anchorX=e.getX();
    activePic.anchorY=e.getY();
    activePic.x=activePic.x+(e.getX()-activePic.anchorX);
    activePic.y=activePic.y+(e.getY()-activePic.anchorY);
    System.out.println("activePic.anchorX="+activePic.anchorX);
    System.out.println("activePic.anchorY="+activePic.anchorY);
    System.out.println("activePic x="+activePic.x);
    System.out.println("activePic y="+activePic.y);
    activePic.focused=false;
    }
    public void mouseDragged(MouseEvent e)
    {
    if(pv.isEmpty())
    return;
    System.out.println("e.getX="+e.getX());
    System.out.println("e.getY="+e.getY());
    if(activePic.focused)
    {
    if(activePic.x>=0&&activePic.x+activePic.width<=width&&activePic.y>=0&&activePic.y+activePic.height<=height)
    {
    activePic.x=activePic.x+(e.getX()-activePic.anchorX);
    activePic.y=activePic.y+(e.getY()-activePic.anchorY);
    activePic.anchorX=e.getX();
    activePic.anchorY=e.getY();
    }
    if(activePic.x<0)
    {
    activePic.x=0;
    activePic.anchorX=e.getX();
    }
    if(activePic.x+activePic.width>width)
    {
    activePic.x=width-activePic.width;
    activePic.anchorX=e.getX();
    }
    if(activePic.y<0)
    {
    activePic.y=0;
    activePic.anchorY=e.getY();
    }
    if(activePic.y+activePic.height>height)
    {
    activePic.y=height-activePic.height;
    activePic.anchorY=e.getY();
    }
    //System.out.println("activePic.x="+activePic.x);
    //System.out.println("activePic.y="+activePic.y);
    }
    }
    public void mouseMoved(MouseEvent e)
    {//System.out.println("mouse Movedvalue="+e.getButton());
    }
    public void paintComponent(Graphics g)
    {
    Graphics bg=bi.getGraphics();
    bg.setClip(0,0,width,height);
    bg.setColor(Color.BLACK);
    bg.fillRect(0,0,width,height);
    for(int i=0;i<pv.size();i++)
    {
    if(!activePic.equals(((Picture)pv.elementAt(i))))
    ((Picture)pv.elementAt(i)).paint(bg);
    }
    if(!pv.isEmpty())
    {
    bg.setColor(Color.RED);
    bg.setClip(activePic.x-1,activePic.y-1,activePic.width+3,activePic.height+3);
    bg.drawRect(activePic.x-1,activePic.y-1,activePic.width+1,activePic.height+1);
    activePic.paint(bg);
    }
    g.drawImage(this.bi,0,0,null);

    }

    }class Picture
    {
    Image img;
    boolean focused=false;
    int x;
    int y;
    int width;
    int height;
    int anchorX=0;
    int anchorY=0;
    public  Picture(Image img,int x,int y)//,int width,int height)
    {
    this.img=img;
    this.x=x;
    this.y=y;
    this.width=img.getWidth(null);
    this.height=img.getHeight(null);
    }
    public void paint(Graphics g)
    {
    g.setClip(x,y,width,height);
    g.drawImage(this.img,x,y,null);
    }
    }
      

  4.   

    太长继续贴
    class   FileTransferHandler   extends   TransferHandler   {   
        
    MyPanel panel;
        public   FileTransferHandler(MyPanel p)   {   
               this.panel=p;
        }       public   boolean   importData(JComponent   c,   Transferable   t)   
        {   System.out.println("entry importData");
                try   {   
                List   files   =   (List)t.getTransferData(DataFlavor.javaFileListFlavor);  
                String []s= new String[files.size()];
                for(int i=0;i<files.size();i++)
                {
                 System.out.println("files["+i+"]="+files.get(i).toString());
                 s[i]=files.get(i).toString();
                } 
                System.out.println("prepare to loadPic");
                panel.loadPic(s);
                return   true;   
                }   
                catch   (UnsupportedFlavorException   ufe)   
                {   
                ufe.printStackTrace();   
                }   
                catch   (IOException   e)   
                {   
                 e.printStackTrace();   
                }   
                return   false;   
        }       public   boolean   canImport(JComponent   c,   DataFlavor[]   flavors)   
        {   System.out.println("entry canImport");
            for   (int   i   =   0;   i   <   flavors.length;   i++)   
            {   
                if   (DataFlavor.javaFileListFlavor.equals(flavors[i]))   
                {   
                        return   true;   
                }   
            }   
            return   false;   
        } 
    }
    class Console
    {
    public static String title( Object o)
    {
    String t=o.getClass().toString();
    if(t.indexOf("class")!=-1)
    t = t.substring(6);
    return t;
    }
    public static void run(JFrame frame , int width , int height)
    {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
    }
    public static void run(JApplet applet,int width ,int height)
    {
    JFrame frame = new JFrame(title(applet));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width,height);
    applet.init();
    applet.start();
    frame.setVisible(true);
    }
    public static void run (JPanel panel,int width, int height)
    {
    JFrame frame=new JFrame(title(panel));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.setSize(width,height);
    frame.setVisible(true);
    }
    }
      

  5.   

    这个是主类
    public class Editor extends JFrame {

    private static int width =500;
    private static int height =300;

    private MyPanel panel;
    public Editor()
    {
    super();
    panel=new MyPanel(width,height);
    panel.setTransferHandler(new FileTransferHandler(panel));
    Container c=this.getContentPane();
    c.add(panel);
    this.pack();
    // c.addMouseListener(panel);
    // c.addMouseMotionListener(panel);
    //c.addKeyListener(panel);

    }

    public static void main(String [] args)
    {
    Console.run(new Editor(),width,height);
    }
    }