Tlaloc 0 Newbie Poster

I have made an array of class cards which is a 4*14 grid (The 14th space is for the 4 grey non-cards).

So far i have made 2 methods for this. The first was an algorithm for figuring out where each card belongs which I painted on when they were simply ImageIcons. When I switched to a grid layout so that I could put the ImageIcon into a card type object (Basically a JLabel with a mouse listener) I found I had a beautiful way to add the cards, but nothing to tell me where they are on the grid so I can figure out whether or not it is a valid play.

Now I have to exchange the cards, moving them around the grid with certain paramaters (basically in a game called Montana) Aces can only go first, the 2 of the suit of the ace can go second behind it, etc. If a grey spot is behind a 7 of clubs, for example, only the eight of clubs can go there.

But I have no way of forcing the player to put the card on the grid. not only that, I have a jumping mouse problem; whenever someone clicks on the cards it jumps the mouse off the card.

Below you can see both versions of code. The first places the cards in order; the second randomizes the cards.

code # 1:

JPanel panel = new JPanel()
        {
            //paintComponent is called automatically by the JRE whenever
            //the panel needs to be drawn or redrawn
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                bg.paintIcon(this, g, 20, 20);
                //Alternate method to make a green field; h for horizontal, v for vertical
                /*for (int h=1;h<55;h++){
                    for (int v=1;v<20;v++){
                        green.paintIcon (this, g, h*20, v*20);
                    }
                }*/
                int v=10;
                for (int i=1;i<5;i++){
                     gray.paintIcon(this, g, 40, 30+v);
                     v=v+107;
                }
                //Using h, v, and the various pluses, we standardize card placement.
                //Each hplus is one card over, each vplus is one card down. 
                //Grey went into the first slot. 
                v=40;
                int h=40;
                int hplus=78;
                int vplus=107;
                cAS.paintIcon(this, g, h+hplus, v);
                c2S.paintIcon(this, g, h+2*hplus, v);
                c3S.paintIcon(this, g, h+3*hplus, v);
                //etc...

Jlabeled example.
//cS stands for card Spades, Ace=1 to King=13
        for (int i=0;i<13;i++){
            position[i]=cS[i];
            position[i+13]=cH[i];
            position[i+26]=cD[i];
            position[i+39]=cC[i];
}
        Collections.shuffle(Arrays.asList(position));
        //create a panel displaying the card image
        JPanel panel = new JPanel(new GridLayout(4,14,3,3))
        {
            //paintComponent is called automatically by the JRE whenever
            //the panel needs to be drawn or redrawn
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                bg.paintIcon(this, g, 0, 0);
            };
        };
        for (int j=0;j<4;j++) {
        	panel.add (gr[j]);
            for (int i=0;i<13;i++){
                panel.add (position[i+(j*13)]);
            }
        }

        //create & make visible a JFrame to contain the panel
        JFrame window = new JFrame("A card set is born...");
        window.add(panel);
        window.setPreferredSize(new Dimension(1125,450));
//This dimension makes the grid look good.
        window.pack();
        window.setVisible(true);

The cards class is as follows:

public class cards extends JLabel {

    public cards(ImageIcon Image) {
    	setIcon(Image);
        MouseHandler listener=new MouseHandler();
        this.addMouseListener (listener);
        this.addMouseMotionListener(listener);
    }

 /**
  * Makes the mouse move around the screen. It reacts when the mouse is clicked, dragged, and released.
  * @param  None
  * @return      
  */        
public class MouseHandler extends MouseInputAdapter
	{
	int startX = -1, startY = -1;
	Point origin;
//	@Override, not needed so removed
	/** This handles what happens when the mouse is pressed. The card is grabbed, 
	 * and moved as needed when first pressed
	 * @param  MouseEvent What happens when the mouse is manipulated in some way (in this case, pressed)
	 * @return      None
	 */

	public void mousePressed (MouseEvent e)
	{
	cards movingthing=(cards) e.getComponent();
	origin=movingthing.getLocation();
	JPanel panel= (JPanel) movingthing.getParent();
	panel.setComponentZOrder(movingthing,0);
	panel.repaint();
	}
	/**
	 * This handles when the mouse is dragged around. The card is grabbed, 
	 * and moved as needed until it is released.
	 * @param  MouseEvent What happens when the mouse is manipulated in some way (in this case, pressed)
	 * @return      None
	 */
    public void mouseDragged (MouseEvent e)
    	{
    	cards movingthing=(cards) e.getComponent();
    	origin=movingthing.getLocation();
    	JPanel panel= (JPanel) movingthing.getParent();
    	movingthing.setLocation(MouseInfo.getPointerInfo().getLocation());
    	//final ImageIcon gray = new ImageIcon("cardImages/gray.gif");
    	//cards gr=new cards(gray);
    	//gr.setLocation(origin);
    	}
    /**
     * This is not used, but may be included in future versions of the program.
     *
    public void mouseReleased (MouseEvent e)
    {   
    	cards movingthing=(cards) e.getComponent();
    	origin=movingthing.getLocation();
    	JPanel panel= (JPanel) movingthing.getParent();
        final ImageIcon gray = new ImageIcon("cardImages/gray.gif");
    	cards gr=new cards(gray);
    	gr.setLocation(origin);
    	movingthing.setLocation(MouseInfo.getPointerInfo().getLocation());
    }*/
    }
}

Possible solution to part of it if add worked the same way paintIcon did:

for (int j=0;j<4;j++) {
                	gr.add(this,g,40,40+(j*107)));
                    for (int i=0;i<13;i++){
                    	int hplus=78;
                    	int vplus=107;
                        position[i+(j*13)].add(this,g,40+(i*hplus),40+(j*vplus));
                    }
                }