Drag operation:
Create a hashmap, which reports a color index(say totally 5 different colors in the 8x8 grid) and a rectangle object. Rect and color stand respectively for key and value. You click each time twice separately on 2 adjacent blocks(rects), get these two appropriate colors and exchange them. Then check again your color distribution, if there is now at least one block set with 3 or more than 3 same colors adjacent on a line. If not, exchange the colors back.
Falling down:
After having found at least one block set(more than 3 even adjacent on the same line), deduct all the color blocks above the removed rectangles by each rectangle's height * the number of the removed rects in the current column. Afterwards check the current color distribution again and again until there is no more block set with more than 3 equal colors on a line.
Thanks. How do you create the falling down of the objects feeling for the user. Do I need to use some graphics or timer to create such an affect?
A timer will be a good choice. After having found at least one block set(more than 3 even adjacent on the same line) invoke Timer's schedule(TimerTask task, long delay, long period), in which you need to define TimerTask's run(), which does nothing only invokes your JComponent/JFrame's repaint(int x, int y, int width, int height) defined in main thread to draw the falling part. In your JComponent/JFrame's paint(Graphics g) you need draw different phases of your falling part. You also need use a loop in your TimerTask's run(), so that each time after period ms, another JComponent/JFrame's repaint(x, y, width, height) will be invoked. If you want loop it 5 times, so just use TimerTask's cancel() in the 5th loop and the timer will be ended. If you don't concern the flicker effect too much, just use repaint() instead and draw each time a whole 8x8 grid image in your paint(Graphics g). The reason I used JComponent/JFrame is that you can either draw direct on the JFrame or draw on the JComponent based on a JFrame.
Thanks. I have got some idea to implement the falling and dragging effect. Right now I am stuck in my program and can't find my way out. I will appreciate if I get some help.My program so far is I have created a panel with 8x8 gridlayout in a frame. I have created a class with ImageID(string with row and column no.), Image(JLabel), row and column. I have created a 64 objects stored in 2 dimensional array stored in GridLayout.A mouselistener and mousemotionlistener is attached to each object.when a user clicks on a grid, I get the image object and inturn get row and column of the image clicked – newjav yesterday
The problem I am stucked is when mouse is pressed, dragged and released. The object I get when the mouserelease or mousedrag is the object that was pressed. So I don't get the row and column of the grid where the mouse was released. How do I calculate the grid location where the mouse was released. I am stuck here and can't go further
If you only use mousePressedListener and mouseReleasedListener without mouseMotionListener, then you can get your source and target grid objects separately. But I think your aim is to use MouseMotionListener. So I have two variants:
1. Shortly after having pressed a grid obj, you get the grid obj(i.e. you get the bounds of this grid obj). e.g. You clicked on the grid obj in the 2nd row and column, then you can get the grid obj's members row = 1 and coloum = 1. Assume the width and height of each grid are both 10(pixels). Then you get the bounds of this grid obj: (10, 10) and (20, 20). Now you want to drag into another grid. So you need to add a condition in this grid obj's mouseReleased() function: if the current released mouse position is out of this grid obj's bounds, one of his neighbours is now the target grid obj. e.g. if(e. getX() > 20 && e.getY() > 10 && e.getY() < 20) {// then the target grid obj is on the source grid obj's right side, and his bounds is (20, 10), (30, 10), i.e. row = 1, column = 2} With the row and column you can find the target grid obj from your 2D array.
2. But If I were you, I would not assign each grid obj a mouseListener and mouseMotionListener, but only assign the panel including these 64 grid objs a mouseListener and a mouseMotionListener. Each time you click, drag and then release, you will get an appropriate position, with which you easily convert into a row and column. With the row and column you can find your appropriate grid obj from your 2D array. That's it. Hopefully this is what you want.