Help! Arrays.sort() doesn't work.

Discussion in 'Java' started by juliekcf@yahoo.com.hk, Oct 29, 2003.

  1. Guest

    I implement Comparable to the following class DisplayTile1 and then
    added a "sort tiles" button with mouselistener in the other class
    TilingDemo1. However, after pressing the sort button, the screen does
    not refresh. Anything wrong or missing? Pls help!

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

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

    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){
    String numStr;
    FontMetrics f = tile.getFontMetrics(tile.getFont());
    String display = ( (tileString.indexOf("-") != -1) ?
    tileString.substring(0,
    tileString.indexOf("-")).trim():
    tileString);
    numStr = display;
    int leftover = (tile.getWidth() - f.stringWidth(display)) /
    f.stringWidth(" ");
    for (int k = 0; k < leftover / 2; k++)
    display = " " + display;
    tile.setText("\n" + display);

    if (numStr.equals("One"))
    numCode = 1;
    else if (numStr.equals("Two"))
    numCode = 2;
    else if (numStr.equals("Three"))
    numCode = 3;
    else if (numStr.equals("Four"))
    numCode = 4;
    else if (numStr.equals("Five"))
    numCode = 5;
    else if (numStr.equals("Six"))
    numCode = 6;
    else if (numStr.equals("Seven"))
    numCode = 7;
    else if (numStr.equals("Eight"))
    numCode = 8;
    else if (numStr.equals("Nine"))
    numCode = 9;
    else if (numStr.equals("Ten"))
    numCode = 10;
    else if (numStr.equals("Eleven"))
    numCode = 11;
    else if (numStr.equals("Twelve"))
    numCode = 12;
    else if (numStr.equals("Thirteen"))
    numCode = 13;
    else
    numCode = 0;
    }

    public void setBackgroundColor(String colourString){
    if (colourString.endsWith("Red")) {
    tile.setBackground(Color.RED);
    colourCode = 1;
    }
    else if (colourString.endsWith("Blue")) {
    tile.setBackground(Color.BLUE);
    colourCode = 2;
    }
    else if (colourString.endsWith("Yellow")) {
    tile.setBackground(Color.YELLOW);
    colourCode = 3;
    }
    else if (colourString.endsWith("Orange")) {
    tile.setBackground(Color.ORANGE);
    colourCode = 4;
    }
    else {
    tile.setBackground(Color.WHITE);
    colourCode = 5;
    }
    }

    private int getColour() {
    return colourCode;
    }

    private int getNum() {
    return numCode;
    }

    public int compareTo(Object handTiles) {
    int comp=0;

    DisplayTile1 hTiles = (DisplayTile1) handTiles;
    if (this.getColour() == hTiles.getColour()) {
    if (this.getNum() == hTiles.getNum())
    comp=0;
    else if (this.getNum() < hTiles.getNum())
    comp=-1;
    else if (this.getNum() < hTiles.getNum())
    comp=1;
    }
    else if (this.getColour() < hTiles.getColour())
    comp=-1;
    else if (this.getColour() > hTiles.getColour())
    comp=1;

    return comp;
    }


    }
    ===============================================================


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

    /**
    * <p>Title: TileDemo1.java</p>
    * <p>Description: ** insert description** </p>
    * <p>Copyright: Copyright (c) 2003</p>
    * <p>Company: </p>
    * @author
    * @version 1.0
    */

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

    private JButton dealTilesButton, mixUpTilesButton, sortTilesButton;
    private DisplayTile1 displayTiles[][];
    private JLabel status;
    private TilePack myPack;
    private JPanel buttonPanel;
    private JPanel playerPanels[];
    private JPanel playerHandPanels[];
    private JPanel discardPanel;
    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 = new JPanel(new BorderLayout(5,5));
    playerHandPanels = new JPanel(new GridLayout(1,0,10,10));
    playerPanels.add(playerHandPanels, BorderLayout.CENTER);
    playerPanels.add(new JLabel("Player "+(i+1)),
    BorderLayout.NORTH);

    // add 13 discard buttons below the hand of tiles of player 2
    //if (i==1) {
    //discardPanel = new JPanel(new GridLayout(1,0,10,10));
    //for (int j = 0; j < TOTAL_TILES; j++){
    //discardPanel.add(new JButton("D"));
    //}
    //playerPanels.add(discardPanel, BorderLayout.SOUTH);
    //}
    mainPlayPanel.add(playerPanels);
    }

    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[j].setTile(dealTilest.toString());
    status.setText("Tile #: " +
    myPack.getCurrentTileNumber());
    }
    else {
    displayTiles[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);

    // add a button for player 2 to sort his hand of tiles
    sortTilesButton = new JButton("Sort Tiles");
    sortTilesButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    status.setText("SORTING.....");
    Arrays.sort(displayTiles[1]);
    status.setText("SORTED");
    }
    }
    );
    buttonPanel.add(sortTilesButton);

    displayTiles = new DisplayTile1[PLAYERS][TOTAL_TILES];
    for (int i = 0; i < PLAYERS; i++){
    for (int j = 0; j < TOTAL_TILES; j++) {
    displayTiles[j] = new DisplayTile1();
    playerHandPanels.add(displayTiles[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);
    }
    }
    );
    }
    }
     
    , Oct 29, 2003
    #1
    1. Advertising

  2. Jon Skeet Guest

    <> wrote:
    > I implement Comparable to the following class DisplayTile1 and then
    > added a "sort tiles" button with mouselistener in the other class
    > TilingDemo1. However, after pressing the sort button, the screen does
    > not refresh. Anything wrong or missing? Pls help!


    Yes, you haven't told the screen to refresh, nor is it immediately
    obvious from your code why refreshing the screen would change it -
    you've added the tiles individually, and there's nothing to change
    their positions just because the array itself has changed.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Oct 29, 2003
    #2
    1. Advertising

  3. Roedy Green Guest

    On 29 Oct 2003 03:46:52 -0800,
    () wrote or quoted :

    >Arrays.sort(displayTiles[1]);


    I did not look closely, but did you mean to write just plain
    displayTiles to sort the array. That form would be used to sort the
    second row of a an array of arrays.

    Finally you sorted the internal data. You need some sort of repaint
    on logic to put that back into Swing so it knows to redraw.

    Just sorting the data elements is not going to change where they
    appear on screen.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 29, 2003
    #3
  4. FISH Guest

    () wrote in message news:<>...
    > I implement Comparable to the following class DisplayTile1 and then
    > added a "sort tiles" button with mouselistener in the other class
    > TilingDemo1. However, after pressing the sort button, the screen does
    > not refresh. Anything wrong or missing? Pls help!
    >

    [Code snipped...]

    Your code isn't the most readable I've seen, and the lack of comments
    doesn't help, but from what I can make out you are creating an array
    of 'tile' panels, and then adding them one by one into containers (?)
    Just because you sort the array, there's no reason to suppose that the
    container referencing those panels will sort its contents too.

    Firstly you need to dump the contents of the sorted array, to ensure the
    sort indeed works. If it does, then you need to take a look at the
    coupling between the GUI and the contents of the array you sorted - if
    you merely add the array elements into a GUI container, the container
    will hold independent references to each object in the array. Sorting
    the references in the original array will not sort the referenced in the
    GUI container.

    -FISH- ><>
     
    FISH, Oct 29, 2003
    #4
  5. Guest

    Jon Skeet <> wrote in message news:<MPG.1a09c56c645b74549896d7@10.1.1.14>...
    > <> wrote:
    > > I implement Comparable to the following class DisplayTile1 and then
    > > added a "sort tiles" button with mouselistener in the other class
    > > TilingDemo1. However, after pressing the sort button, the screen does
    > > not refresh. Anything wrong or missing? Pls help!

    >
    > Yes, you haven't told the screen to refresh, nor is it immediately
    > obvious from your code why refreshing the screen would change it -
    > you've added the tiles individually, and there's nothing to change
    > their positions just because the array itself has changed.


    Thanks! I just forgot to refresh the screen!

    Julie
     
    , Oct 31, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Smowton

    Why doesn't Arrays.toList work?

    Chris Smowton, Jul 10, 2006, in forum: Java
    Replies:
    6
    Views:
    8,235
    Red Orchid
    Jul 11, 2006
  2. Steve Pinard

    sort() doesn't work on dist.keys() ?

    Steve Pinard, Jul 10, 2003, in forum: Python
    Replies:
    5
    Views:
    456
    Greg Fortune
    Jul 11, 2003
  3. Ken
    Replies:
    35
    Views:
    2,264
  4. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
  5. Navin
    Replies:
    1
    Views:
    710
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page