J
juliekcf
I am new to Java. Can anyone give me some help, please?
There are 2 classes: DisplayTile1 and TilingDemo1
How can I implement Comparable in DisplayTile1 so that I can sort
tiles by colour and then by number?
What should be done in TilingDemo1 accordingly?
Julie
import javax.swing.*;
import java.awt.*;
//**********************************DisplayTile1.java
public class DisplayTile1 extends JPanel{
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 = 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);
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);
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);
}
}
);
}
}
There are 2 classes: DisplayTile1 and TilingDemo1
How can I implement Comparable in DisplayTile1 so that I can sort
tiles by colour and then by number?
What should be done in TilingDemo1 accordingly?
Julie
import javax.swing.*;
import java.awt.*;
//**********************************DisplayTile1.java
public class DisplayTile1 extends JPanel{
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 = 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);
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);
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);
}
}
);
}
}