basketball4567 0 Newbie Poster

I need to be able to choose one of the three boxes and have then drag around the screen, i can only get 1 of them, kinda of. I want to be able to drag the red one, or the green. I can kinda get the green to drag but it works if i click anywhere on the screen. Any help would be great.

Thanks

-Basketball4567

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class SwingPaintDemo3 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? "+
        SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }
}

class MyPanel extends JPanel {

    private int squareX = 10;
    private int squareY = 10;
    private int squareW = 20;
    private int squareH = 20;

    private int square1X = 10;
	private int square1Y = 60;
	private int square1W = 20;
    private int square1H = 20;

    private int square2X = 10;
	private int square2Y = 110;
	private int square2W = 20;
    private int square2H = 20;

    public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

      // addMouseListener(new MouseAdapter() {
      //     public void mousePressed(MouseEvent e) {
      //          moveSquare(e.getX(),e.getY());
      //      }
      //  });

       addMouseMotionListener(new MouseAdapter() {
           public void mouseDragged(MouseEvent e) {
               moveSquare(e.getX(),e.getY());
           }
       });

    }

    private void moveSquare(int x, int y) {
        int OFFSET = 1;
        if ((square1X!=x) || (squareY!=y)) {
            repaint(square1X,square1Y,square1W+OFFSET,square1H+OFFSET);
            square1X=x;
            square1Y=y;
            repaint(square1X,square1Y,square1W+OFFSET,square1H+OFFSET);
        }
    }


    public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawRect(squareX,squareY,squareW,squareH);

        g.setColor(Color.green);
        g.fillRect(square1X,square1Y,square1W,square1H);
        g.setColor(Color.BLACK);
        g.drawRect(square1X,square1Y,square1W,square1H);

        g.setColor(Color.blue);
		g.fillRect(square2X,square2Y,square2W,square2H);
		g.setColor(Color.BLACK);
        g.drawRect(square2X,square2Y,square2W,square2H);

    }


}