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){

}

}