//I want to make a program so that i can move a pawn around a board. It only has 4 positions. I am wondering how i should
//go about the action listener, as in should i check using a for loop?
//The pawn begins in position 0 and depending on what button i click can move in 8 directions

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


public class chessBoard extends JFrame implements ActionListener
{



    JButton button[]=new JButton[4];

    Icon pawn = new ImageIcon("pawn.png");
    JButton clickMe[]= new JButton[8];
    String letters[] = {"Left", "Right", "Up", "Down", "Left up", "Right up", "Left Down","Right Down"};
    JButton reset= new JButton("Reset Game");
    JButton exit= new JButton("Exit");




    chessBoard()
    {

    super("Cheess Board Game");
    Container c = getContentPane();

    JPanel buttons = new JPanel();
    buttons.setBackground(Color.BLACK);
    JPanel board = new JPanel(new GridLayout(2, 2));
    board.setPreferredSize(new Dimension(4*100, 4*100));

    c.add(board,BorderLayout.SOUTH);
    c.add(buttons,BorderLayout.CENTER);

    exit.setBackground(Color.RED);
    reset.setBackground(Color.GREEN);
    buttons.add(reset);
    buttons.add(exit);

    reset.addActionListener(this);
    exit.addActionListener(this);

     for (int counter =0; counter<clickMe.length; counter++)
        {



            clickMe[counter] = new JButton(letters[counter]);
            clickMe[counter].addActionListener(this);
            buttons.add(clickMe[counter]);

        }




     for(int counter =0; counter < button.length; counter++)
            {
                button[counter]=new JButton();
                board.add(button[counter]);


            }

              button[0].setBackground(Color.WHITE);
              button[1].setBackground(Color.BLACK);
              button[2].setBackground(Color.BLACK);
              button[3].setBackground(Color.WHITE);
              button[0].setIcon(pawn);


        board.setVisible(true);
        board.setSize(600,600);







}

    public static void main (String[]args)
    {

    chessBoard object = new chessBoard();
    object.setSize(500,500);
    object.setVisible(true);
    object.setResizable(false);

    }


    public void actionPerformed (ActionEvent e)
    {

    if (e.getActionCommand().equals("Reset"))
    {
        button[0].setIcon(pawn);
        button[1].setIcon(null);
        button[2].setIcon(null);
        button[3].setIcon(null);

    }
    if (e.getActionCommand().equals("Exit"))
    {
        System.exit(0);
    }










    }





}

why an actionListener? seems to me like you have to 'drag' the piece to the next place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.