0

I'm trying to make the game Dots and I have already figured out a way to generate random dots using an int[][]. I've also created a method to remove dots if they are next to each other and have the same colour when selected. My playfield is 7 by 7 and is filled with JButtons (which could also be JLabels). Now I need to find a way to drag over a few dots and select them if they are the same colour as the initial dot, but whenever I try to do something like that, I always end up either selecting the initial dot itself and send that button's coordinates whenever I move the mouse, or select the x and y values for the pointer on the screen.

Here is the relevant code:

for (int i = 0; i < 7; i++) {
    for (int j = 0; j < 7; j++) {
        final int finalJ = j;
        final int finalI = i;
        playField[i][j].addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                try {
                    while (true) {
                        dotsController.beginZet(finalI, finalJ);
                    }
                } catch (DotsException e1) {
                    e1.printStackTrace();
                }
            }
        });
        playField[i][j].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                try {
                    dotsController.doeZet();
                } catch (DotsException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
Nic
  • 6,211
  • 10
  • 46
  • 69
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Have you gone through [Drag and Drop and Data Transfer](http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html)? 3) *"Keep in mind I'm a beginner."* Keep in mind that GUIs are an advanced topic. Not really the type of coding task a beginner should try to tackle. – Andrew Thompson Mar 04 '15 at 13:25
  • Aren't MCVEs and SSCCEs basically the same thing in different words? – Nic Mar 04 '15 at 13:31
  • Yes I know but I need to use it for my project I have to program for school so I dont really have a choice. Also this is the code causing the problem the problem, because this is what I have and this code has to become the part where a button/label is detected and sent to the controller – Captain Ezmay Mar 04 '15 at 13:34
  • @QPaysTaxes Aren't MCVEs and SSCCEs basically the same thing in different words - :-) I'm sure than not "-), SSCCE is collected, incorporated good practicies by Andrew Thompson, and MCVE is hiding (create local culture) for his ideas on SO, – mKorbel Mar 04 '15 at 15:19
  • Multiple selection is illustrated in the example cited [here](http://stackoverflow.com/a/11944233/230513). – trashgod Mar 04 '15 at 17:40

0 Answers0