java swing help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

java swing help

redhanzredhanz Posts: 50Member
hello,
i have a couple questions regarding java swing. I have some code here and I want to make it such that when I move icons on the board they arent allowed to overlap and secondaly, some icons can be two or three grid lengths long. Rightnow i can load an image that fits nicely into one grid and when I move one object over another it disappears.
If anyone wold be able to help me or guide me in the right direction it would be great.


jerm

code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
JPanel gameBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;

public ChessBoard()
{
Dimension boardSize = new Dimension(200, 200);

// Use a Layered Pane for this this application

layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize( boardSize );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );

// Add a chess board to the Layered Pane

gameBoard = new JPanel();
layeredPane.add(gameBoard, JLayeredPane.DEFAULT_LAYER);
gameBoard.setLayout( new GridLayout(8, 8) );
gameBoard.setPreferredSize( boardSize );
gameBoard.setBounds(0, 0, boardSize.width, boardSize.height);




for (int i = 0; i < 64; i++)
{
JPanel cell = new JPanel( new BorderLayout() );
gameBoard.add( cell );
cell.setBorder(BorderFactory.createLineBorder(Color.black));
}


JLabel piece = new JLabel( new ImageIcon("poop.gif") );
JPanel panel = (JPanel)gameBoard.getComponent( 0 );
panel.add( piece );
piece = new JLabel( new ImageIcon("poop.gif") );
panel = (JPanel)gameBoard.getComponent( 15 );
panel.add( piece );
}

/*
** Add the selected chess piece to the dragging layer so it can be moved
*/
public void mousePressed(MouseEvent e)
{
chessPiece = null;
Component c = gameBoard.findComponentAt(e.getX(), e.getY());

if (c instanceof JPanel) return;

Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.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());

if (c instanceof JLabel)
{
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) {}

public static void main(String[] args)
{
JFrame frame = new ChessBoard();
frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable( false );
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}



Sign In or Register to comment.