Solved

implement mouselistener  or another work around

Posted on 2003-10-31
10
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 9657727
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
ID: 9657752
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
ID: 9657861
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
jimmack earned 350 total points
ID: 9657874
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
ID: 9657941
sorry i dont understand and how do i add another(2nd) implement to the class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9657963
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
ID: 9657989
public class DisplayTile1 extends JPanel implements Comparable, MouseListener {
.
.

Then implement the required methods.

Personally, I'd use a MouseAdapter
0
 

Author Comment

by:sayd
ID: 9658086
Y a MouseAdapter?
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 350 total points
ID: 9658152
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
ID: 9658172
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

710 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