Solved

implement mouselistener  or another work around

Posted on 2003-10-31
10
195 Views
Last Modified: 2010-03-31
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);
        }
      }
    );
  }
}
0
Comment
Question by:sayd
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
Comment Utility
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
 

Author Comment

by:sayd
Comment Utility
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
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
 
LVL 15

Accepted Solution

by:
jimmack earned 350 total points
Comment Utility
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
 

Author Comment

by:sayd
Comment Utility
sorry i dont understand and how do i add another(2nd) implement to the class.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 350 total points
Comment Utility
public class DisplayTile1 extends JPanel implements Comparable, MouseListener {
.
.

Then implement the required methods.

Personally, I'd use a MouseAdapter
0
 

Author Comment

by:sayd
Comment Utility
Y a MouseAdapter?
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 350 total points
Comment Utility
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
 

Author Comment

by:sayd
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now