hey, I have a drag and drop grid. I have an image that fits nicely into the grid. I want to change it such that the image can fill one, two or three blocks such that when i select a block all three images will move with it. here is my code now, any siggestions or tips where to start?
rogerwilco
public class GameBoard extends JLayeredPane implements MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
JPanel gameBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
JLabel piece;
public GameBoard()
{
Dimension boardSize = new Dimension(200, 200);
this.setPreferredSize( boardSize );
this.addMouseListener(this);
this.addMouseMotionListener( this );
gameBoard = new JPanel();
this.add(gameBoard, JLayeredPane.DEFAULT_LAYER);
gameBoard.setLayout( new GridLayout(6, 6) );
gameBoard.setPreferredSize( boardSize );
gameBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 36; i++)
{
JPanel cell = new JPanel( new BorderLayout() );
gameBoard.add( cell );
cell.setBorder(BorderFactory.createLineBorder(Color.black));
}
/***here is where i add the label to the grid, but I want to make them be able to range from size one to three!!!!!!!****/
JLabel piece = new JLabel( new ImageIcon("poop.gif") );
JPanel panel = (JPanel)gameBoard.getComponent( 0 );
panel.add( piece );
}
public void mousePressed(MouseEvent e)
{
chessPiece = null;
Component c = gameBoard.findComponentAt(e.getX(), e.getY());
//System.out.println(c);
//if select a piece other than a game piece
if (c instanceof JPanel) return;
Point parentLocation = c.getParent().getLocation();
System.out.println(parentLocation);
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
System.out.println(xAdjustment);
System.out.println(yAdjustment);
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
gameBoard.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
/*
** Move the chess piece around
*/
public void mouseDragged(MouseEvent me)
{
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
/*
** Drop the chess piece back onto the chess board
*/
public void mouseReleased(MouseEvent e)
{
if (chessPiece == null) return;
chessPiece.setVisible(false);
Component c = gameBoard.findComponentAt(e.getX(), e.getY());
//can not overlap pieces
if (c instanceof JLabel)
{
System.out.println("here");
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else
{
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}