DropListener that retrieves X & Y from JButton[X][Y] after a Drag and Drop onto JButton[][] Grid

StackOverflow https://stackoverflow.com/questions/21715533

Frage

I am creating a battleship game inside a GUI using Java Swing. For now I am just trying to get this grid working so that a user may drag and drop the battleship from the bottom of the grid onto a spot within the grid above. I would like to save the X & Y integer values inside a DropListener class so that after a button is dropped on the grid I may retrieve the X & Y values and automatically place the rest of the ship buttons (pieces of the ship) accordingly by adding 1,2,and 3 to the X value respectively.

You will need to put in pictures for the Battleship, the water, and the red marker for this to work.

public class ButtonGrid extends JFrame

   JFrame frame=new JFrame(); //creates frame
   JButton[][] grid; //names the grid of buttons
    ImageIcon waterImage = new ImageIcon( "Water.png" );
    ImageIcon redImage = new ImageIcon( "Red.png" );
    ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
    ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
    ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
    ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

   public ButtonGrid(int width, int length) //constructor
   {
           frame.setLayout(new GridLayout(width,length)); //set layout
           grid=new JButton[width][length]; //allocate the size of grid
           for(int y=0; y<length; y++)
           {
                   for(int x=0; x<width-1; x++)
                   {
                           grid[x][y]=new JButton( waterImage ); //creates new button
                            grid[x][y].setBackground(Color.BLUE);
                            grid[x][y].setContentAreaFilled(false);
                            grid[x][y].setOpaque(true);
                            grid[x][y].setRolloverIcon( redImage );
                            grid[x][y].setFocusable(false);         
                            grid[x][y].setTransferHandler(new TransferHandler("icon"));

                            new DropListener(grid[x][y], x, y);

                           frame.add(grid[x][y]); //adds button to grid
                   }
            }

            //=================================================================BattleShip

                    grid[10][0]=new JButton( battleship1 ); //creates new button
                    grid[10][0].setBackground(Color.YELLOW);
                    grid[10][0].setContentAreaFilled(false);
                    grid[10][0].setOpaque(true);
                    MouseListener listener1 = new DragMouseAdapter();
                    grid[10][0].addMouseListener(listener1);
                    grid[10][0].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][0]); //adds button to grid

                    grid[10][1]=new JButton( battleship2 ); //creates new button
                    grid[10][1].setBackground(Color.YELLOW);
                    grid[10][1].setContentAreaFilled(false);
                    grid[10][1].setOpaque(true);
                    MouseListener listener2 = new DragMouseAdapter();
                    grid[10][1].addMouseListener(listener2);
                    grid[10][1].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][1]); //adds button to grid

                    grid[10][2]=new JButton( battleship3 ); //creates new button
                    grid[10][2].setBackground(Color.YELLOW);
                    grid[10][2].setContentAreaFilled(false);
                    grid[10][2].setOpaque(true);
                    MouseListener listener3 = new DragMouseAdapter();
                    grid[10][2].addMouseListener(listener3);
                    grid[10][2].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][2]); //adds button to grid

                    grid[10][3]=new JButton( battleship4 ); //creates new button
                    grid[10][3].setBackground(Color.YELLOW);
                    grid[10][3].setContentAreaFilled(false);
                    grid[10][3].setOpaque(true);
                    MouseListener listener4 = new DragMouseAdapter();
                    grid[10][3].addMouseListener(listener4);
                    grid[10][3].setTransferHandler(new TransferHandler("icon"));                                
                    frame.add(grid[10][3]); //adds button to grid

            //=================================================================BattleShip



           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.pack(); //sets appropriate size for frame
           frame.setVisible(true); //makes frame visible
    } // end ButtonGrid constructor

    class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {               
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }

    } // end DragMouseAdapter

    class DropListener extends  EventListener, DropTargetListener
    {

        public DropListener(JButton[][] grid, int x, int y)
        {
            System.out.print( "X = " + x + " Y = " + y );
        }

        //void dropPerformed(DropEvent event, int x, int y)
        //{
        //  System.out.print( "X = " + x + " Y = " + y );
        //}
    }

   public static void main(String[] args)
   {
        new ButtonGrid(11,10);//makes new ButtonGrid with 2 parameters
    }

} // end ButtonGrid class
War es hilfreich?

Lösung

First, pick a API. You are crossing over Drag'n'Drop APIs. You're trying to use the TransferHandler API and the core Drag'n'Drop APIs, they don't mix well in this way (the TransferHandler API uses the core Drag'n'Drop APIs to perform their actions).

Take a look at Drag and Drop and Data Transfer and How to drag and drop with Java 2, Part 1 for an introduction into both.

In your case, personally, you're not so much moving components, but the data they represent, this makes me think you want to use the TransferHandler API.

This means you need to create a TransferHandler implementation that export and import the data you are trying to move.

The basic idea would be to create a TransferHandler that knew how to "export" the ship and a TransferHandler that knew how to "import" the ship data (for the grid).

You create a "import" handler for each grid button, providing the information you need in order to support the import process, such as the JButton and it's X/Y grid information.

For the "export" handler, you would simply need to supply the "ship" information you need to ensure you know how that ship would be implemented within the grid.

Read through the first tutorial linked above for the details ;)

Andere Tipps

Sorry the code doesn't compile you just need to comment out

new DropListener(grid[x][y], x, y);

along with the DropListener class. But I made a little progress, here it is. Instead of using

grid[x][y]=new JButton( waterImage ); //creates new button

to create my buttons I made my own Button class and use this to create each button

grid[x][y]=new GridButtonClass( x, y );

which allows me to pass the x and y coordinates to the constructor and saves them! Here is the GridButtonClass

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GridButtonClass extends JButton implements ActionListener{

int xValue;
int yValue;

ImageIcon waterImage = new ImageIcon( "Water.png" );
ImageIcon redImage = new ImageIcon( "Red.png" );
ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

public GridButtonClass(int x, int y){
    xValue = x;
    yValue = y;
    setIcon( waterImage );
    this.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
    System.out.println( "X Value = " + xValue + " Y Value = " + yValue );
    setIcon( redImage );
}

}

When you click on a Grid Button the picture will change to the redImage and the X and Y values of its position will appear on the Terminal. Here is the other file in case any one wants to execute it all.

public class ButtonGrid extends JFrame //, JButton implements ActionListener
{

   JFrame frame=new JFrame(); //creates frame
   JButton[][] grid; //names the grid of buttons
    ImageIcon waterImage = new ImageIcon( "Water.png" );
    ImageIcon redImage = new ImageIcon( "Red.png" );
    ImageIcon battleship1 = new ImageIcon( "BattleShip1.png" );
    ImageIcon battleship2 = new ImageIcon( "BattleShip2.png" );
    ImageIcon battleship3 = new ImageIcon( "BattleShip3.png" );
    ImageIcon battleship4 = new ImageIcon( "BattleShip4.png" );

   public ButtonGrid(int width, int length) //constructor
   {
           frame.setLayout(new GridLayout(width,length)); //set layout
           grid=new JButton[width][length]; //allocate the size of grid
           for(int y=0; y<length; y++)
           {
                   for(int x=0; x<width-1; x++)
                   {
                        grid[x][y]=new GridButtonClass( x, y );
                        //grid[x][y]=new JButton( waterImage ); //creates new button
                        grid[x][y].setBackground(Color.BLUE);
                        grid[x][y].setContentAreaFilled(false);
                        grid[x][y].setOpaque(true);
                        grid[x][y].setRolloverIcon( redImage );
                        grid[x][y].setFocusable(false);         
                        grid[x][y].setTransferHandler(new TransferHandler("icon"));

                        //new DropTargetListener();

                        frame.add(grid[x][y]); //adds button to grid
                   }
            }

            //=================================================================BattleShip

            grid[10][0]=new JButton( battleship1 ); //creates new button
            grid[10][0].setBackground(Color.YELLOW);
            grid[10][0].setContentAreaFilled(false);
            grid[10][0].setOpaque(true);
            MouseListener listener1 = new DragMouseAdapter();
            grid[10][0].addMouseListener(listener1);
            grid[10][0].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][0]); //adds button to grid

            grid[10][1]=new JButton( battleship2 ); //creates new button
            grid[10][1].setBackground(Color.YELLOW);
            grid[10][1].setContentAreaFilled(false);
            grid[10][1].setOpaque(true);
            MouseListener listener2 = new DragMouseAdapter();
            grid[10][1].addMouseListener(listener2);
            grid[10][1].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][1]); //adds button to grid

            grid[10][2]=new JButton( battleship3 ); //creates new button
            grid[10][2].setBackground(Color.YELLOW);
            grid[10][2].setContentAreaFilled(false);
            grid[10][2].setOpaque(true);
            MouseListener listener3 = new DragMouseAdapter();
            grid[10][2].addMouseListener(listener3);
            grid[10][2].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][2]); //adds button to grid

            grid[10][3]=new JButton( battleship4 ); //creates new button
            grid[10][3].setBackground(Color.YELLOW);
            grid[10][3].setContentAreaFilled(false);
            grid[10][3].setOpaque(true);
            MouseListener listener4 = new DragMouseAdapter();
            grid[10][3].addMouseListener(listener4);
            grid[10][3].setTransferHandler(new TransferHandler("icon"));                                
            frame.add(grid[10][3]); //adds button to grid

            //=================================================================BattleShip



           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.pack(); //sets appropriate size for frame
           frame.setVisible(true); //makes frame visible
    } // end ButtonGrid constructor

    class DragMouseAdapter extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {               
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);
        }

    } // end DragMouseAdapter

   public static void main(String[] args)
   {
        new ButtonGrid(11,10);//makes new ButtonGrid with 2 parameters
    }

} // end ButtonGrid class

Make sure you add your own images in place of mine. What you should see when you execute the program is a Grid of Buttons 10X10 and under that Grid there are 4 Buttons that on mine make up one Battleship ( I cropped a Battleship image from online, I wish I could post pictures for you to see what I mean ) You can drag and drop each individual ship piece anywhere on the Grid as many times as you want. When you click anywhere on the Grid the picture changes to red and the X and Y values are displayed on the Terminal.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top