I'm trying to build a Scrabble game to help get more familiar with building GUIs and practice java skills in general. The board is mainly composed of a JLayeredPane with JPanels to include the board image and board spaces. I'm trying to be able to drag and drop the Tile objects (extends JLabel) around the board by adding the tile to the drag_layer and then moving it from there, similar to the following example: dragging a jlabel around the screen. The mouse press is working and correctly adding the tile to the drag layer, but after that, it seems like the mouse stops listening completely. I tried just printing "Dragging" in the mouseDragged override method, but it won't even print that. There are two relevant classes - Console and MouseInput, which I'll show below, but I'll also add the link to the GitHub repo at the end if you want to pull the whole project.
Console:
public Console () throws IOException{
// Create gameConsole
gameConsole = new JFrame();
final int frameWidth = 850;
final int frameHeight = 950;
gameConsole.setPreferredSize(new Dimension(frameWidth, frameHeight));
gameConsole.setTitle("Scrabble");
gameConsole.setLayout(new GridBagLayout());
gameConsole.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.ipadx = 810;
c.ipady = 950;
// Create center console panel and add to gameConsole
centerConsole = new JPanel();
centerConsole.setLayout(new BorderLayout());
gameConsole.add(centerConsole,c);
// Create layered pane that holds the board and playerbox
gameContainer = new JLayeredPane();
gameContainer.setBounds(0,0,810,950);
gameContainer.addMouseListener(new MouseInput(gameContainer));
gameContainer.addMouseMotionListener(new MouseInput(gameContainer));
centerConsole.add(gameContainer, BorderLayout.CENTER);
// Create board image label and add to JPanel
BufferedImage scrabbleImage = ImageIO.read(Console.class.getResource("/board.jpg"));
JLabel background = new JLabel(new ImageIcon(scrabbleImage));
boardImage = new JPanel();
boardImage.setBounds(0, 0, 810, 810);
boardImage.add(background);
boardImage.setOpaque(true);
// create JPanel with gridBagLayout
boardGrid = new JPanel();
boardGrid.setBounds(0, 3, 810, 810);
boardGrid.setLayout(new GridBagLayout());
boardGrid.setOpaque(false);
// Create panels to add to boardGrid
spaces = new BoardSpace [15][15];
BoardSpace.setBoardSpaces(spaces);
// Set grid constraints
GridBagConstraints cGrid = new GridBagConstraints();
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
// panel constraints
cGrid.gridx = i; // grid x location
cGrid.gridy = j; // grid y location
cGrid.gridheight = 1; // spans 1 row
cGrid.gridwidth = 1; // spans 1 column
cGrid.weightx = 0.0;
cGrid.weighty = 0.0;
cGrid.fill = GridBagConstraints.BOTH; // Resize veritically & horizontally
// Set size of board space and add to grid
spaces[i][j].setOpaque(false);
spaces[i][j].setPreferredSize(new Dimension((int) Info.GRIDSIZE,(int) Info.GRIDSIZE));
boardGrid.add(spaces[i][j], cGrid);
}
}
// Add to layeredPane
gameContainer.add(boardImage, new Integer(0),0);
gameContainer.add(boardGrid, new Integer(1),0);
// Create player box panel
playerPanel = new JPanel();
playerPanel.setLayout(new GridBagLayout());
playerBox = new JPanel();
playerBox.setLayout(new GridLayout(1,7, 10, 0));
// Create player box constraints
GridBagConstraints cp = new GridBagConstraints();
cp.ipadx = 50;
cp.ipady = 50;
// Create playerBox spaces
playerSpaces = new BoardSpace [1][7];
BoardSpace.setPlayerSpaces(playerSpaces);
// Add playerSpaces to playerBox
for (int j = 0; j < 7; j++) {
// panel constraints
cGrid.gridx = 0; // grid x location
cGrid.gridy = j; // grid y location
cGrid.gridheight = 1; // spans 1 row
cGrid.gridwidth = 1; // spans 1 column
cGrid.weightx = 0.0;
cGrid.weighty = 0.0;
cGrid.fill = GridBagConstraints.BOTH; // Resize veritically & horizontally
// Set size of board space and add to grid
playerSpaces[0][j].setOpaque(false);
playerSpaces[0][j].setPreferredSize(new Dimension((int) Info.GRIDSIZE,(int) Info.GRIDSIZE));
playerBox.add(playerSpaces[0][j], cGrid);
}
// Add player box to south panel
playerPanel.add(playerBox, cp);
// Add player box to bottom of layeredPane
playerPanel.setBounds(0,825,810,75);
gameContainer.add(playerPanel, new Integer(0),0);
gameConsole.pack();
// Make gameConsole visible
gameConsole.setVisible(true);
}
Mouse Adapter:
public class MouseInput extends MouseAdapter {
/**
*
*/
private Component mouseArea;
private boolean dragging;
private Tile draggingTile;
private BoardSpace currentSpace;
private BoardSpace startingSpace;
private Component selectedObject;
Component [] panelObjects;
private JLayeredPane dragLayer;
private Point dragPoint;
private int dragWidth;
private int dragHeight;
public MouseInput(Component mouseArea) {
// TODO Auto-generated constructor stub
super();
mouseArea = this.mouseArea;
}
void eventOutput(String eventDescription, MouseEvent e) {
Point p = e.getPoint();
System.out.println(eventDescription
+ " (" + p.getX() + "," + p.getY() + ")"
+ " detected on "
+ e.getComponent().getClass().getName()
+ "\n");
}
public void mouseMoved(MouseEvent e) {
//eventOutput("Mouse moved", e);
}
public void mouseDragged(MouseEvent e) {
if (dragging) {
System.out.println("Dragging");
}
if (!dragging) {
return;
} else {
System.out.println("Dragging " + Tile.getLetter(draggingTile));
int x = e.getPoint().x - dragWidth;
int y = e.getPoint().y - dragHeight;
draggingTile.setLocation(x, y);
draggingTile.repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
//eventOutput("Mouse Clicked", e);
}
@Override
public void mouseEntered(MouseEvent e) {
/*
dragPoint = e.getPoint();
selectedObject = e.getComponent().getComponentAt(dragPoint);
// Get the current space
while (!selectedObject.getClass().getSimpleName().equals("BoardSpace")) {
try {
dragPoint = selectedObject.getMousePosition();
selectedObject = selectedObject.getComponentAt(dragPoint);
} catch (NullPointerException illegalSpace){
currentSpace = startingSpace;
break;
}
}
if (selectedObject.getClass().getSimpleName().equals("BoardSpace")) {
currentSpace = (BoardSpace) selectedObject;
System.out.println(BoardSpace.getID(currentSpace));
} */
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//eventOutput("Mouse Exited", e);
}
@Override
public void mousePressed(MouseEvent e) {
dragLayer = (JLayeredPane) e.getSource();
dragPoint = e.getPoint();
selectedObject = e.getComponent().getComponentAt(dragPoint);
// Get the current space
while (!selectedObject.getClass().getSimpleName().equals("BoardSpace")) {
try {
dragPoint = selectedObject.getMousePosition();
selectedObject = selectedObject.getComponentAt(dragPoint);
} catch (NullPointerException illegalSpace){
return;
}
}
currentSpace = (BoardSpace) selectedObject;
startingSpace = currentSpace;
// If the boardspace has a tile, remove Tile from boardspace and add to dragging layer
if (BoardSpace.Taken(currentSpace)) {
// get dragging tile
draggingTile = BoardSpace.getTile(currentSpace);
dragging = true;
// remove tile and repaint space
BoardSpace.removeTile(currentSpace, draggingTile);
currentSpace.revalidate();
currentSpace.repaint();
// Add tile to dragging layer
dragWidth = draggingTile.getWidth() / 2;
dragHeight = draggingTile.getHeight() / 2;
int x = e.getPoint().x - dragWidth;
int y = e.getPoint().y - dragHeight;
draggingTile.setLocation(x, y);
dragLayer.add(draggingTile, JLayeredPane.DRAG_LAYER);
draggingTile.revalidate();
draggingTile.repaint();
System.out.println("Selected Tile " + Tile.getLetter(draggingTile));
} else {
return;
}
}
@Override
public void mouseReleased(MouseEvent e) {
/*
// TODO Auto-generated method stub
if (!BoardSpace.Taken(currentSpace)) {
return;
} else {
dragging = false;
BoardSpace.setTile(currentSpace, draggingTile);
draggingTile = null;
currentSpace.repaint();
currentSpace.revalidate();
} */
}
}
Full code can be pulled from: https://github.com/jowarren13/scrabble.git