Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Problem with 'setcolor'

Thread view: 
Ed - 25 Mar 2006 17:31 GMT
I have written a game which involves making a simulsted deck of playing
cards where each card is represented by a number and a symbol of its suit.
Where the suit is "Hearts" or "Diamonds" I want to make the number and
symbol red.

The deck is made up by the following code in Class Deck:

public String[] PlayDeck(int deckSize, int numOfSuits, int cardsInSuit)
  {
       deck = new String[deckSize];
       int k = 0;
       int a = -1;
       for (int i = 1; i <= numOfSuits; i++)
       {
           a++;
           for (int j = 1; j <= cardsInSuit; j++)
          {
             String q = String.valueOf(j);
              deck[k] = q + suit[a];
              k++;
          }
     }
     cardsLeft = deckSize - 1;
     return deck;
  }

These declariations were made earlier:

  public String[] deck;
  char suit[] = {'\u2661', '\u2663', '\u2662', '\u2660'};

I've been trying to insert the following before the line  "k++":

if (suit[a] == '\u2661') || (suit[a] == '\u2662')
           {
              deck[k].setColor(Color.red);
           }

I've tried several variations of this and have concluded I don't know what
I'm doing. So can somewne please help me?

Thanks.
Ed
Rhino - 25 Mar 2006 19:07 GMT
>I have written a game which involves making a simulsted deck of playing
>cards where each card is represented by a number and a symbol of its suit.
[quoted text clipped - 36 lines]
> I've tried several variations of this and have concluded I don't know what
> I'm doing. So can somewne please help me?

You can't set a color on a String: it's that simple. A String has no color
so you can't alter the color of a String or an element in an array of
Strings, like 'deck', either.

However, when you display a String by showing its value in a GUI, you can
make sure that it has a given color. Therefore, if you want to display the
values of a card in a JLabel or JList, you can paint the String a given
color. For example, if I want to create a JLabel that says "Hello World!"
where the text is red and the background white, I can do it this way:

   JLabel myLabel = new JLabel("Hello World!");
   myLabel.setForeground(Color.RED);
   myLabel.setBackground(Color.WHITE);

Of course you also need to add 'myLabel' to your GUI and display the GUI in
a JFrame or other component before you actually see your colored text.

--
Rhino
Roedy Green - 25 Mar 2006 19:34 GMT
>if (suit[a] == '\u2661') || (suit[a] == '\u2662')

This is unmaintainable code.  The first thing you want to do is either
introduce an enum for your 4 suits (suites?) see
http://mindprod.com/jgloss/enum.html

or at least make them named constants e.g.
private static final char HEARTS = '\u2665';
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 25 Mar 2006 19:43 GMT
>  deck = new String[deckSize];
>        int k = 0;
[quoted text clipped - 9 lines]
>           }
>      }

Naming you variables precisely or at least labelling them with what
you intend them to do will go a long way to solving this sort of
problem.

k is range 0..51. It represents a card in the deck you are generating.
Lets call it cardNo or cardIndex.

i is repesents a suit. 0..3. Lets call it suitIndex. (with an enum you
could write that

for ( Suit suit: Suit.values() }

I have no idea what you are trying to do with "a".

j represents the rank of the card in the suit, 1..13
so call it rank

There is no need for q. Just inline.

you can collapse
deck [cardNo] = xxx;
cardNo++;
to
deck [cardno++] = xxx;
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 25 Mar 2006 19:47 GMT
> public String[] deck;
...
>               deck[k].setColor(Color.red);

Strings don't have colours.   Labels and JLabels do.
"information" as a bald String does not have a colour, but when you
display it in a JLabel it has both a foreground and background colour,
and a font. You assign colours to your Strings at the time you display
them. They are not built-into the Strings.

This is all to the more reason to set up an enum.  You can have
properties of your enum such as color, ordinal, name...

When you need the color to decorate a Label for example, you can get
it with suit.color().
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 25 Mar 2006 20:51 GMT
>deck[k].setColor(Color.red);

When you have problem like this, look at the Javadoc.

deck is a String.  So read up on the String.setColor method. You will
quickly discover there isn't one.  You have to make your soup from the
ingredients at hand.

see http://mindprod.com/jgloss/string.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ed - 26 Mar 2006 17:24 GMT
>I have written a game which involves making a simulsted deck of playing
>cards where each card is represented by a number and a symbol of its suit.
[quoted text clipped - 39 lines]
> Thanks.
> Ed

Thanks to both Rhino and Roedy for help.

I had hoped to be able to set color at the same time I create the deck but,
as you say, strings don't support color. Setting color for each display is a
bit more complicated but that may be the way I need to do it.

I know nothing about enem's but I'll do some studying (probably a lot) and
see if I can understand how to use them.

Thanks again.

Ed
Roedy Green - 26 Mar 2006 20:01 GMT
>I know nothing about enem's but I'll do some studying (probably a lot) and
>see if I can understand how to use them.

Everything you need for this particular problem is covered at
http://mindprod.com/jgloss/enum.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ed - 27 Mar 2006 15:45 GMT
>>I know nothing about enem's but I'll do some studying (probably a lot) and
>>see if I can understand how to use them.
>
> Everything you need for this particular problem is covered at
> http://mindprod.com/jgloss/enum.html

Yes Roedy, I noticed that, but I'm slow in my old age so I have to study a
'bit.

Thanks again for all your help.

Ed
Ed - 30 Mar 2006 16:40 GMT
>>I have written a game which involves making a simulsted deck of playing
>>cards where each card is represented by a number and a symbol of its suit.
[quoted text clipped - 52 lines]
>
> Ed

Before getting into "enum's" I want to understand how to accomplish the task
in my display as Rhino suggested.

The code for my display follows - including my proposed addition to color
RED any card representation with a "Heart" or "Diamond". I've been over this
many times and unable to find any problem with it but it doesn't work. Is it
in the wrong place or what?

Ed

class PublishStats extends JFrame
{
  JTextField wager1 = new JTextField();
  JTextField cards1 = new JTextField();
  JTextField sum1 = new JTextField();
  JTextField bank1 = new JTextField();
  JTextField wager2 = new JTextField();
  JTextField cards2 = new JTextField();
  JTextField sum2 = new JTextField();
  JTextField bank2 = new JTextField();
  JTextField wager3 = new JTextField();
  JTextField cards3 = new JTextField();
  JTextField sum3 = new JTextField();
  JTextField bank3 = new JTextField();
  JTextField wager4 = new JTextField();
  JTextField cards4 = new JTextField();
  JTextField sum4 = new JTextField();
  JTextField bank4 = new JTextField();
  JTextField wager5 = new JTextField();
  JTextField cards5 = new JTextField();
  JTextField sum5 = new JTextField();
  JTextField bank5 = new JTextField();

  public void UpdateFields(String[] thePlayers, String[][] tabDat)
  {
     int wgr = 0;
     int crds = 1;
     int sm = 2;
     int bnk = 3;
     int csn = 4;

     String cf = "";
     StringTokenizer cd;

     Font f = new Font("Arial Unicode MS", Font.PLAIN, 14);

     this.wager1.setText(tabDat[wgr][0]);
     this.cards1.setText(tabDat[crds][0]);

//START OF NEW TEST CODE
  cd = new StringTokenizer(tabDat[crds][0], ",");
  while (cd.hasMoreTokens())
  {
     cf = cd.nextToken();
     if ((cf.charAt(cf.length() - 1) == '\u2665') ||

(cf.charAt(cf.length() - 1) == '\u2666'))
     {
        JLabel myLabel = new JLabel (cf);
        myLabel.setForeground(Color.RED);
     }
  }
//END OF NEW TEST CODE

     this.cards1.setFont(f);
     this.sum1.setText(tabDat[sm][0]);
     this.bank1.setText(tabDat[bnk][0]);
     this.wager2.setText(tabDat[wgr][1]);
     this.cards2.setText(tabDat[crds][1]);
     this.cards2.setFont(f);
     this.sum2.setText(tabDat[sm][1]);
     this.bank2.setText(tabDat[bnk][1]);
     this.wager3.setText(tabDat[wgr][2]);
     this.cards3.setText(tabDat[crds][2]);
     this.cards3.setFont(f);
     this.sum3.setText(tabDat[sm][2]);
     this.bank3.setText(tabDat[bnk][2]);
     this.wager4.setText(tabDat[wgr][3]);
     this.cards4.setText(tabDat[crds][3]);
     this.cards4.setFont(f);
     this.sum4.setText(tabDat[sm][3]);
     this.bank4.setText(tabDat[bnk][3]);
     this.wager5.setText(tabDat[wgr][4]);
     this.cards5.setText(tabDat[crds][4]);
     this.cards5.setFont(f);
     this.sum5.setText(tabDat[sm][4]);
     this.bank5.setText(tabDat[bnk][4]);
  }

  void BuildConstraints(GridBagConstraints gbc, int gx, int gy, int

gw, int gh, int wx, int wy)
  {
     gbc.gridx = gx;
     gbc.gridy = gy;
     gbc.gridwidth = gw;
     gbc.gridheight = gh;
     gbc.weightx = wx;
     gbc.weighty = wy;
  }

  public PublishStats(String[] thePlayers, String[][] tabDat)
  {
     super("BJ");
     setBounds(150, 37, 550, 200);

     GridBagLayout gridbag = new GridBagLayout();
     GridBagConstraints constraints = new GridBagConstraints();
     JPanel pane = new JPanel();
     pane.setLayout(gridbag);

       // Heading label-1
       BuildConstraints(constraints, 0, 0, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.CENTER;
       JLabel head1 = new JLabel("Player's Names", JLabel.LEFT);
       gridbag.setConstraints(head1, constraints);
       pane.add(head1);

       // Heading label-2
       BuildConstraints(constraints, 1, 0, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.CENTER;
       JLabel head2 = new JLabel("Wagers", JLabel.LEFT);
       gridbag.setConstraints(head2, constraints);
       pane.add(head2);

       // Heading label-3
       BuildConstraints(constraints, 2, 0, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.CENTER;
       JLabel head3 = new JLabel("Cards", JLabel.LEFT);
       gridbag.setConstraints(head3, constraints);
       pane.add(head3);

       // Heading label-4
       BuildConstraints(constraints, 3, 0, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.CENTER;
       JLabel head4 = new JLabel("Sums", JLabel.LEFT);
       gridbag.setConstraints(head4, constraints);
       pane.add(head4);

       // Heading label-5
       BuildConstraints(constraints, 4, 0, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.CENTER;
       JLabel head5 = new JLabel("Bank Roll", JLabel.LEFT);
       gridbag.setConstraints(head5, constraints);
       pane.add(head5);

       // Name1
       BuildConstraints(constraints, 0, 1, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.EAST;
       JLabel name1 = new JLabel(thePlayers[0] + ": ",JLabel.LEFT);
       gridbag.setConstraints(name1, constraints);
       pane.add(name1);

       // Wager1 text field
       BuildConstraints(constraints, 1, 1, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(wager1, constraints);

       // Cards1 text field
       BuildConstraints(constraints, 2, 1, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(cards1, constraints);

       // Sum1 text field
       BuildConstraints(constraints, 3, 1, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(sum1, constraints);

       // Bank1 text field
       BuildConstraints(constraints, 4, 1, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(bank1, constraints);

       // Content Pane
       setContentPane(pane);

       // Name2
       BuildConstraints(constraints, 0, 2, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.EAST;
       JLabel name2 = new JLabel(thePlayers[1] + ": ",JLabel.LEFT);
       gridbag.setConstraints(name2, constraints);
       pane.add(name2);

       // Wager2 text field
       BuildConstraints(constraints, 1, 2, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(wager2, constraints);

       // Cards2 text field
       BuildConstraints(constraints, 2, 2, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(cards2, constraints);

       // Sum2 text field
       BuildConstraints(constraints, 3, 2, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(sum2, constraints);

       // Bank2 text field
       BuildConstraints(constraints, 4, 2, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(bank2, constraints);

       // Content Pane
       setContentPane(pane);

       // Name3
       BuildConstraints(constraints, 0, 3, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.EAST;
       JLabel name3 = new JLabel(thePlayers[2] + ": ",JLabel.LEFT);
       gridbag.setConstraints(name3, constraints);
       pane.add(name3);

       // Wager3 text field
       BuildConstraints(constraints, 1, 3, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(wager3, constraints);

       // Cards3 text field
       BuildConstraints(constraints, 2, 3, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(cards3, constraints);

       // Sum3 text field
       BuildConstraints(constraints, 3, 3, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(sum3, constraints);

       // Bank3 text field
       BuildConstraints(constraints, 4, 3, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(bank3, constraints);

       // Content Pane
       setContentPane(pane);

       // Name4
       BuildConstraints(constraints, 0, 4, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.EAST;
       JLabel name4 = new JLabel(thePlayers[3] + ": ",JLabel.LEFT);
       gridbag.setConstraints(name4, constraints);
       pane.add(name4);

       // Wager4 text field
       BuildConstraints(constraints, 1, 4, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(wager4, constraints);

       // Cards4 text field
       BuildConstraints(constraints, 2, 4, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(cards4, constraints);

       // Sum4 text field
       BuildConstraints(constraints, 3, 4, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(sum4, constraints);

       // Bank4 text field
       BuildConstraints(constraints, 4, 4, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(bank4, constraints);

       // Content Pane
       setContentPane(pane);

       // Name5
       BuildConstraints(constraints, 0, 5, 1, 1, 9, 20);
       constraints.fill = GridBagConstraints.NONE;
       constraints.anchor = GridBagConstraints.EAST;
       JLabel name5 = new JLabel(thePlayers[4] + ": ",JLabel.LEFT);
       gridbag.setConstraints(name5, constraints);
       pane.add(name5);

       // Cards5 text field
       BuildConstraints(constraints, 2, 5, 1, 1, 34, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(cards5, constraints);

       // Sum5 text field
       BuildConstraints(constraints, 3, 5, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(sum5, constraints);

       // Bank5 text field
       BuildConstraints(constraints, 4, 5, 1, 1, 19, 20);
       constraints.fill = GridBagConstraints.HORIZONTAL;
       pane.add(bank5, constraints);

       // Content Pane
       setContentPane(pane);
  }
}

class ExitWindow extends WindowAdapter
{
   public void windowClosing(WindowEvent e)
   {
       System.exit(0);
   }
}
Roedy Green - 30 Mar 2006 19:13 GMT
>   JTextField wager1 = new JTextField();
>   JTextField cards1 = new JTextField();
>   JTextField sum1 = new JTextField();
>   JTextField bank1 = new JTextField();

Consider simplifying this code with:

JTextfield[] wager;
JTextField[] cards;
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 30 Mar 2006 19:16 GMT
>   JTextField wager1 = new JTextField();
>   JTextField cards1 = new JTextField();
>   JTextField sum1 = new JTextField();
>   JTextField bank1 = new JTextField();

Another simplifying possibility is to invent a JPanel that shows one
wage, cards, sum, bank, and have an array of JPanels.

Whenever you find yourself writing repetitive code, recall that
handling repetitive drudgery is what computers are for.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ed - 30 Mar 2006 20:56 GMT
>>   JTextField wager1 = new JTextField();
>>   JTextField cards1 = new JTextField();
[quoted text clipped - 6 lines]
> Whenever you find yourself writing repetitive code, recall that
> handling repetitive drudgery is what computers are for.

That's a good suggestion and I will work on it. I also have a list of some
other ideas for simplification which I intend to work on. When I write code
it sometimes helps to string it out so I can see better just how it works. I
also want to get into the matter of enum's which you suggested earlier.

In the meantime I would like to understand why I have not been able to solve
the problem I'm working on now with the JTabel, can you help me with that?

Ed
Roedy Green - 30 Mar 2006 23:02 GMT
>In the meantime I would like to understand why I have not been able to solve
>the problem I'm working on now with the JTabel, can you help me with that?

You have so much code it makes your head hurt. Solve a simpler
problem.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.