Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help! Arrays.sort() doesn't work.

Reply
Thread Tools

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

 
 
juliekcf@yahoo.com.hk
Guest
Posts: n/a
 
      10-29-2003
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[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);

// 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[i].add(discardPanel, BorderLayout.SOUTH);
//}
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);

// 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[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);
}
}
);
}
}
 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      10-29-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-29-2003
On 29 Oct 2003 03:46:52 -0800, (E-Mail Removed)
((E-Mail Removed)) 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.
 
Reply With Quote
 
FISH
Guest
Posts: n/a
 
      10-29-2003
(E-Mail Removed) ((E-Mail Removed)) wrote in message news:<(E-Mail Removed). com>...
> 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- ><>
 
Reply With Quote
 
juliekcf@yahoo.com.hk
Guest
Posts: n/a
 
      10-31-2003
Jon Skeet <(E-Mail Removed)> wrote in message news:<MPG.1a09c56c645b74549896d7@10.1.1.14>...
> (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments