Results 1 to 1 of 1

Thread: Drag image

  1. #1
    Dean29126 is offline Member
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Exclamation Drag image

    This is part of my project so far. I am supposed to allow user to drag choices available via images to a specific location. However, i managed to only use the paintcomponent and draw these choices out. How am i suppose to replace these choices with an image instead?

    Here are my codes below:









    import java.awt.GridBagLayout;
    import javax.swing.JPanel;

    import java.awt.Image;
    import java.awt.Scrollbar;
    import java.awt.GridBagConstraints;
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.Dimension;

    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;


    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class DnD extends JPanel implements MouseListener, MouseMotionListener{


    private static final int w = 84;
    private static final int h = 15;

    private static final long serialVersionUID = 1L;



    private boolean _canDrag = false;

    private int _labelX = 605; // x coord - set from drag
    private int _labelY = 77; // y coord - set from drag


    private int _dragFromX = 0;
    private int _dragFromY = 0;
    /**
    * This is the default constructor
    */
    public DnD() {
    super();
    initialize();
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
    }

    /**
    * This method initializes this
    *
    * @return void
    */
    private void initialize() {


    this.setSize(720, 480);
    this.setLayout(null);
    this.setBackground(Color.white);


    public void paintComponent(Graphics g) {
    super.paintComponent(g); // Required for background.
    g.drawRect(_labelX, _labelY, w, h);


    }//end paintComponent

    public void mouseEntered(MouseEvent e){

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    int x = e.getX(); // Save the x coord of the click
    int y = e.getY(); // Save the y coord of the click

    if (x >= _labelX && x <= (_labelX + w)
    && y >= _labelY && y <= (_labelY + h)) {
    _canDrag = true;
    _dragFromX = x - _labelX; // how far from left
    _dragFromY = y - _labelY; // how far from top
    } else {
    _canDrag = false;
    }
    }

    public void mouseDragged(MouseEvent e){
    if (_canDrag) { // True only if button was pressed inside label.
    //--- label pos from mouse and original click displacement
    _labelX = e.getX() - _dragFromX;
    _labelY = e.getY() - _dragFromY;

    //--- Don't move the label off the screen sides
    _labelX = Math.max(_labelX, 0);
    _labelX = Math.min(_labelX, getWidth() - w);

    //--- Don't move the label off top or bottom
    _labelY = Math.max(_labelY, 0);
    _labelY = Math.min(_labelY, getHeight() - h);

    this.repaint(); }


    }
    public void mouseClicked(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e){
    _canDrag = false;
    }

    public void mouseMoved(MouseEvent e){

    }

    }
    Last edited by Dean29126; 01-13-2011 at 07:08 AM.

Similar Threads

  1. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 07:30 PM
  2. How to drag and drop the copy of image?
    By LianaN in forum AWT / Swing
    Replies: 15
    Last Post: 08-26-2010, 08:31 PM
  3. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 10:18 AM
  4. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 03:10 PM
  5. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •