implement mouselistener or another work around

Ok from the following code i want to be able to discard a tile from the hand of player 2 (diplayTiles1[1][0 to 12]) doesnt matter where the tile goes at the mo but if u can keep it in a discard array it would be helpful. Then move all the tiles along leaving the displaytile[1][12] empty and put a new tile from the main pack in its place.

Is it possible to have 2 implements on 1 class?

DisplayTile1.java
import javax.swing.*;
import java.awt.*;

public class DisplayTile1 extends JPanel implements Comparable{
  private JTextArea tile;

  public DisplayTile1() {
    super(new FlowLayout());
    tile = new JTextArea(3,5);
    tile.setFont(new Font("Arial", Font.BOLD, 10));
    tile.setEditable(false);
    this.add(tile);
  }

  public void setTile(String tileString){
    setBackgroundColor(tileString);
    setText(tileString);
  }

  public void setText(String tileString){
    FontMetrics f = tile.getFontMetrics(tile.getFont());
    String display = ( (tileString.indexOf("-") != -1) ?
                        tileString.substring(0, tileString.indexOf("-")).trim():
                        tileString);
    int leftover = (tile.getWidth() - f.stringWidth(display)) / f.stringWidth(" ");
    for (int k = 0; k < leftover / 2; k++)
      display = " " + display;
    tile.setText("\n" + display);
  }
 
  public void setBackgroundColor(String colourString){
    if (colourString.endsWith("Red")) tile.setBackground(Color.RED);
    else if (colourString.endsWith("Blue")) tile.setBackground(Color.BLUE);
    else if (colourString.endsWith("Yellow")) tile.setBackground(Color.YELLOW);
    else if (colourString.endsWith("Orange")) tile.setBackground(Color.ORANGE);
    else tile.setBackground(Color.WHITE);
  }
}
}

TilingDemo1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TilingDemo1 extends JFrame{
  public final static int PLAYERS = 2;
  public final static int TOTAL_TILES = 13;

  private JButton dealTilesButton, mixUpTilesButton;
  private DisplayTile1 displayTiles[][];
  private JLabel status;
  private TilePack myPack;
  private JPanel buttonPanel;
  private JPanel playerPanels[];
  private JPanel playerHandPanels[];
  private JPanel mainPlayPanel;
  private JPanel statusPanel;

  public TilingDemo1(){
    super("Tiles");
    myPack = new TilePack();
    myPack.mixUpTiles();
    buttonPanel = new JPanel(new FlowLayout());
    mainPlayPanel = new JPanel(new GridLayout(PLAYERS,1,10,50));
    playerPanels = new JPanel[PLAYERS];
    playerHandPanels = new JPanel[PLAYERS];
    for (int i = 0; i < PLAYERS; i++) {
      playerPanels[i] = new JPanel(new BorderLayout(5,5));
      playerHandPanels[i] = new JPanel(new GridLayout(1,0,10,10));
      playerPanels[i].add(playerHandPanels[i], BorderLayout.CENTER);
      playerPanels[i].add(new JLabel("Player "+(i+1)), BorderLayout.NORTH);
      mainPlayPanel.add(playerPanels[i]);
    }
    statusPanel = new JPanel(new FlowLayout());
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    dealTilesButton = new JButton("Deal Tiles");
    dealTilesButton.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < PLAYERS; i++) {
          for (int j = 0; j < TOTAL_TILES; j++) {
            Tile dealTilest = myPack.getNextTile();
            if (dealTilest != null) {
              displayTiles[i][j].setTile(dealTilest.toString());
              status.setText("Tile #: " + myPack.getCurrentTileNumber());
            }
            else {
              displayTiles[i][j].setTile("");
              status.setText("Press Mix Tiles Button to continue...");
              dealTilesButton.setEnabled(false);
            }
          }
        }
      }
    }
    );
    buttonPanel.add(dealTilesButton);

    mixUpTilesButton = new JButton("Mix Tiles");
    mixUpTilesButton.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        status.setText("MIXING.....");
        myPack.mixUpTiles();
        status.setText("MIXED");
        dealTilesButton.setEnabled(true);
      }
    }
    );
    buttonPanel.add(mixUpTilesButton);

    displayTiles = new DisplayTile1[PLAYERS][TOTAL_TILES];
    for (int i = 0; i < PLAYERS; i++){
      for (int j = 0; j < TOTAL_TILES; j++) {
        displayTiles[i][j] = new DisplayTile1();
        playerHandPanels[i].add(displayTiles[i][j]);
      }
    }
    status = new JLabel("Hit a button to begin");
    statusPanel.add(status);

    c.add(buttonPanel, BorderLayout.NORTH);
    c.add(mainPlayPanel, BorderLayout.CENTER);
    c.add(statusPanel, BorderLayout.SOUTH);

    pack();
    show(); // show the window

  }

  public static void main(String[] args) {
    TilingDemo1 td1 = new TilingDemo1();

    td1.addWindowListener(
      new WindowAdapter(){
        public void windowClosing(WindowEvent e){
          System.exit(0);
        }
      }
    );
  }
}
saydAsked:
Who is Participating?
 
jimmackConnect With a Mentor Commented:
If you really want to keep the discarded tiles in an array, create you discardedTiles array, create a curDiscTile integer then you can use the above code, modified as follows:

int player = // current player
int curTile = // the tile that is to be discarded

discardedTiles[curDiscTile] = displayTiles[curTile];
curDiscTile++;

while (tileToDiscard < TOTAL_TILES - 1)
{
    displayTiles[curTile] = displayTiles[curTile + 1];
    curTile++;
}

displayTiles[curTile] = myPack.getNextTile();
0
 
CEHJConnect With a Mentor Commented:
You should probably do the following:

1. keep the tiles in a collection instead of an array
2. keep the two players' tiles separate
3. have a 'display' flag in the class to determine whether it's to be displayed
0
 
saydAuthor Commented:
Id prefer to keep it in the array as it is. Unless you can show me how to write the code but only if its easier to do so.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jimmackCommented:
You can implement muliple interfaces (I think it's up to 256), it's only extending that is limited to one.

Implement the listener you want and include the following code:

int player = // current player
int curTile = // the tile that is to be discarded

while (tileToDiscard < TOTAL_TILES - 1)
{
    displayTiles[curTile] = displayTiles[curTile + 1];
    curTile++;
}

displayTiles[curTile] = myPack.getNextTile();
0
 
saydAuthor Commented:
sorry i dont understand and how do i add another(2nd) implement to the class.
0
 
CEHJCommented:
If you don't need to remove elements from the array, you may get away with using an array. Logically speaking, I would have thought it more appropriate to keep separate containers for each player.

Anyway, just flag each element as displayable or not and when you create a view of the elements, refer to that flag.
0
 
jimmackConnect With a Mentor Commented:
public class DisplayTile1 extends JPanel implements Comparable, MouseListener {
.
.

Then implement the required methods.

Personally, I'd use a MouseAdapter
0
 
saydAuthor Commented:
Y a MouseAdapter?
0
 
jimmackConnect With a Mentor Commented:
MouseListener has 5 methods, all of which must be overridden in your code.  You're probably only interested in mouseClicked().

A MouseAdapter contains "default" implementations for each of these, so you only need to code the ones you're interested in.

0
 
saydAuthor Commented:
yes basically if possible i would like to click on 1 of my tiles to discard it. Can u show me how to write it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.