1

Basically, I've created a board filled with boardFields extending Jbuttons, implementing ActionListeners. After adding a "Unit" to a boardField, I cannot figure out a method to move it to another boardField. Here's the code.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class BoardField extends JButton implements ActionListener,
    MouseListener {

public Boolean isOccupied;
public Unit occupiedBy;
public Integer locationOnBoard;
public Unit beingMoved;
public Boolean clicked = false;

public BoardField(Integer locationOnBoard, Unit occupiedBy,
        Boolean isOccupied) {
    super();
    this.locationOnBoard = locationOnBoard;
    this.occupiedBy = occupiedBy;

    this.addActionListener(this);
    this.addMouseListener(this);

}

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("button");
    if (clicked == false) {
        setBeingMoved(occupiedBy);
        occupiedBy = null;
        clicked = true;
    } else {
        setOccupiedBy(beingMoved);
        clicked = false;
        beingMoved = null;
    }

}

@Override
public void mouseClicked(MouseEvent arg0) {
    System.out.println("clicked");

}

@Override
public void mouseEntered(MouseEvent arg0) {
    System.out.println("entered");

    if (occupiedBy != null) {
        setText(" " + occupiedBy.getUnitType().getName() + ": "
                + occupiedBy.getQuantity());
    } else {
        setText("null");
    }

}`

I'm sorry for code formatting but even though i pressed ctrl+K it looks like this. Thanks for any help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

0 Answers0