2

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

Bacteria
  • 8,406
  • 10
  • 50
  • 67
  • 1
    Determine what you want to transfer, it's eaiser to transfer data, components not so much. For [example](http://stackoverflow.com/questions/16478413/move-component-after-drag-and-drop/16481711#16481711), [example](http://stackoverflow.com/questions/11201734/java-how-to-drag-and-drop-jpanel-with-its-components/11443501#11443501), [example](http://stackoverflow.com/questions/28844574/drag-and-drop-from-jbutton-to-jcomponent-in-java/28844969#28844969) – MadProgrammer Jun 17 '15 at 12:19
  • 1
    There are three basic ways, you can do it yourself with `MouseListener`s and what not, but its messy; You can use the core DnD API which is powerful, but complicated; You can use the transfer API which simplifies the core DnD API, but which might not work with what you are trying to achieve – MadProgrammer Jun 17 '15 at 12:21
  • Thanks for your quick response! I'll try and implement these in an hour but i have to say it looks very promising thank you so much :D – Max Beekmans Jun 17 '15 at 12:22
  • @MadProgrammer Well i've been going over your suggestion and i've been trying to implement this one: [link](http://stackoverflow.com/questions/16478413/move-component-after-drag-and-drop/16481711#16481711) however I can't quiet figure out what this code is suppose to do. The main thing that confuses me is the fact that it uses a list of images where my intention is to just have one shape / Image that can be draged. – Max Beekmans Jun 17 '15 at 18:24
  • *"The main thing that confuses me is the fact that it uses a list of images where my intention is to just have one shape / Image"* Have you ever heard the phrase "Figure how to do it for one, then how to do it for many should be obvious"? Well this is, "see how to do it for many, and doing it for one should be a cinch"! Don't expect people to tailor code to your exact requirements, you'll need to do some work on your own. SO is a Q&A site rather than a help desk. – Andrew Thompson Jun 17 '15 at 18:33
  • @AndrewThompson I'm sorry I didn't mean to offend anyone I just have trouble reading the code. – Max Beekmans Jun 17 '15 at 19:04
  • I think in your case, using the Transfer API could be easier, but I'll need to do some playing. DnD is very complex and has a lot of "black magic" involved – MadProgrammer Jun 17 '15 at 22:17

0 Answers0