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

J

juliekcf

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);
}
}
);
}
}
 
J

Jon Skeet

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.
 
R

Roedy Green

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.
 
F

FISH

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- ><>
 
J

juliekcf

Jon Skeet said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top