Drag and Drop Implementation in Java

I am working on a board game which is an 8x8 grid with 5 images in different colors. Objects can change if they are next to each other in a row or column. If the switch causes 3 or more objects to be the same in a row or column, they will be removed. If the switch does not result in a match, the switch will not execute. When an object is removed, the board shrinks so that objects above the distant ones fall down to take their place, and new ones fall from above to fill in the gaps.

I would like to know a good way to implement drag and drop operation of objects. Should I use a layered panel?

Any ideas will help me.

+3


source to share


1 answer


  • Drag and drop operation: Create a hashmap that tells the color index (say there are 5 different colors in total in an 8x8 grid) and a rectangle object. Rectangular and colored rack respectively for key and value. You double-click each time two adjacent boxes (rectangles), get the two matching colors and swap them. Then check the color distribution again if there is currently at least one block with 3 or more 3 same colors next to the line. If not, replace the colors back.

  • Fall: After finding at least one set of blocks (more than 3, even contiguous in one row), subtract all colored blocks above the deleted rectangles for each rectangle height * the number of deleted rectangles in the current column . Then check the current color distribution over and over until there is more block in the line with more than three equal colors.

Thank. How you create a drop in object sensations for the user. Do I need to use some kind of graphics or timer to create such an effect?

A timer would be a good choice. After finding at least one set of blocks (more than 3, even contiguous in one line), a timer schedule is called (TimerTask, long delay, long period) , in which you need to define a TimerTask run () that does nothing, only calls JComponent / JFrame repaint (int x, int y, int width, int height) defined in the main thread draw the falling part. In your JComponent / JFrame paint (Graphics g) you need to paint different phases of your falling part. You also need to use a loop in the TimerTask run () so that every time after the ms period a different JComponent / JFrameredraws (x, y, width, height) . If you want the loop to be 5 times, just use TimerTask cancel () on the 5th loop and the timer will end. If you're not overly concerned with the flicker effect, just use repaint () and paint the entire 8x8 grid image each time in your paint (Graphics g) . The reason I used JComponent / JFrame is because you can either draw directly to the JFrame or draw the JComponent based on the JFrame.



Thank. I have an idea to implement drop and drag effect. Right now I am stuck in my program and cannot find a way out. I will be grateful if I can get some help. My program so far I have created a panel with an 8x8 gridlayout in the frame. I created a class with ImageID (row and column number), Image (JLabel), row and column. I have created 64 objects which are stored in a 2-dimensional array stored in a GridLayout.A mouselistener and mousemotionlistener are attached to each object. When user clicks on grid i get image object and inturn get image row and column by clicking - newjav yesterday
The problem I am stuck is when the mouse is pressed, dragged and released. The object I get when the mouserelease or mousedrag object is the object that was clicked. So I don't get the row and column of the grid where the mouse was released. How to calculate the location of the mesh where the mouse was drawn. I'm stuck here and can't go any further

If you use mousePressedListener and mouseReleasedListener without mouseMotionListener, you can get source and target objects separately. But I think your goal is to use MouseMotionListener. So I have two options: 1. Shortly after clicking on mesh obj, you will get mesh obj (ie, you will get the boundaries of this mesh obj). for example you clicked on grid obj in second row and column then you can get grid items obj row = 1 and coloum= 1. Suppose the width and height of each grid is 10 (pixels). Then you get the bounds of this obj grid: (10, 10) and (20, 20). Now you want to drag to another grid. So you need to add a condition to this obj mouseReleased () function: if the current freed mouse position is outside of this grid, one of its neighbors is now the target network obj. e.g. if (e. getX ()> 20 & e.getY ()> 10 & e.getY () <20) {// then the target mesh obj is on the original mesh on the right side, and its borders are (20, 10) , (30, 10), i.e. row = 1, column = 2} With row and column, you can find the target grid obj from your 2D array. 2.But if I were you, I would not assign every obj mesh to the mouseListener and mouseMotionListener, but only assign the panel, including those 64 grids, to the mouseListener and mouseMotionListener objects. Every time you click, drag and release, you get the corresponding position from which you can easily convert to row and column. With row and column, you can find the corresponding obj grid from your 2D array. It. Hope this is what you want.

0


source







All Articles