So I've been trying to make this game 1010! for a school project. It's not going to be an application it's going to be a swing program. As you can see in this short video you have to drag and drop shapes into a grid. I Currently have a 5x5 grid and a 10x10 grid made out of Tiles this is the class Tile:
public class Tile extends JPanel{
private specialObservable observable;
private boolean isEmpty;
private Color gridColor;
private int width = 40;
private int height = 40;
public Tile(Color gridColor){
this.gridColor = gridColor;
observable = new specialObservable();
isEmpty = false;
Border border = new MatteBorder(1, 1, 1, 1, Color.white);
this.setBorder(border);
this.setBackground(gridColor);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(width - 1, height - 1);
}
This tile is used by the Grid class which makes a two dimensional array of Tiles. This is then passed onto GridPanel and ShapeBox to create a gridbaglayout of these grids.
public class Grid implements Observer{
private Tile[][] grid;
Tile tile;
public Grid(int row, int col){
grid = new Tile[row][col];
}
public JPanel createGrid(Color gridColor){
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int row = 0; row < grid.length; row++) {
for (int col= 0; col < grid[row].length; col++) {
gbc.gridx = row;
gbc.gridy = col;
Tile tile = new Tile(gridColor);
grid[row][col] = tile;
panel.add(tile, gbc);
}
}
return panel;
}
public Tile[][] getGrid() {
return grid;
}
public void setGrid(int row, int col, Color color) {
grid[row][col].setBackground(color);
}
}
this is GridPanel ( the main grid )
public class GridPanel extends JPanel{
private static Grid grid;
private static final int height = 450;
private final Color gridColor = Color.LIGHT_GRAY;
public GridPanel(int width, Color mainColor){
this.setLayout(new FlowLayout());
this.setPreferredSize(new Dimension(width, height));
this.setBackground(mainColor);
setGrid(new Grid(10,10));
JPanel gridPanel = getGrid().createGrid(gridColor);
add(gridPanel);
}
public static Grid getGrid()
{
return grid;
}
public static void setGrid(Grid grid)
{
GridPanel.grid = grid;
}
}
and there is the ShapeBox also containing a grid
@SuppressWarnings("serial")
public class ShapeBox extends JPanel{
private Grid grid;
private final int height = 125;
private final int width = 125;
public ShapeBox(){
this.setPreferredSize(new Dimension(width,height));
grid = new Grid(5,5);
add(grid.createGrid(Color.DARK_GRAY));
}
public void setShape(){
grid.setGrid(0, 0, Color.red);
}
}
Now my Question is: How can i drag a shape from the ShapeBox grid into the main GridPanel grid? I have tried making a new object once the grid is clicked and set it's bounds to the mouse cursor coordinates however I couldn't get that to work. I would love to get any idea's cause i've ran out them. I'm sorry if this is a poorly formatted stackoverflow question i'm rather new to the whole concept.
If you'd like all my code for this project it's on my Github