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.