Home
Email
Butnz!

How to Use Colours in Applets

One way to customise your applets and make them look a little different for hardly any effort is to change the background and foreground colours. I originally needed this when I wanted my applet to fit into the background colour of a page.

Now it's easy enough to pass the colours in with parameters, but I'll show you below. The parser for an arbitrary colour was a little more interesting, and I decided to stick in the names for all the standard java.awt.Color colours as well. There is probably an easier way to make a colour from a string of hex, but I think my method is most portable. You'll need to cut and paste method parseColour into your applet.

 public static Color parseColour(String col) {
    if (col == null) return null;
    if (col.equals("black")) return Color.black;
      else if (col.equals("blue")) return Color.blue;
      else if (col.equals("cyan")) return Color.cyan;
      else if (col.equals("darkGray")) return Color.darkGray;
      else if (col.equals("gray")) return Color.gray;
      else if (col.equals("green")) return Color.green;
      else if (col.equals("lightGray")) return Color.lightGray;
      else if (col.equals("magenta")) return Color.magenta;
      else if (col.equals("orange")) return Color.orange;
      else if (col.equals("pink")) return Color.pink;
      else if (col.equals("red")) return Color.red;
      else if (col.equals("white")) return Color.white;
      else if (col.equals("yellow")) return Color.yellow;
    Color c = Color.getColor(col);
    if (c != null) return c;
    if (col.startsWith("#")) {
        col = col.substring(1);
        }
    if (col.length() != 6) return null;
    try {
        int r = Integer.parseInt(col.substring(0,2),16);
        int g = Integer.parseInt(col.substring(2,4),16);
        int b = Integer.parseInt(col.substring(4,6),16);
        return new Color(r,g,b);
        }
    catch (NumberFormatException nfe) {
        return null;
        }
    }
Now typically you would want to set the foreground and background colours of your applets. On the other hand, at least in the Windows implementation, all the buttons and controls of various types inherit the gungy Windows gray colour that the user can configure. So be warned, the colours you choose may not look good with everybody's colour scheme. Here's a function which will look at your applet tag and read the background colour out of parameter "bgcolor", and the foreground colour out of parameter "fgcolor".
 private void setAppletColours() {
    // set up colours
    Color bg = parseColour(getParameter("bgcolor"));
    if (bg != null) setBackground(bg);
    Color fg = parseColour(getParameter("fgcolor"));
    if (fg != null) setForeground(fg);
    }
Finally, you need to call setAppletColours() in the init() method of your applet. I always put it as the first statement, as it doesn't depend on anything else that you might want to set up.

Here is an example of how you would set the parameters for the applet:

<APPLET CODE="SimpleSlide.class" WIDTH=250 HEIGHT=250 ARCHIVE="classes.zip"> <PARAM NAME="bgcolor" VALUE="red"> <PARAM NAME="fgcolor" VALUE="ff99cc"> </APPLET>

This page hosted by Get your own Free Home Page.
This page uses Verdana fonts which Windows users can download from Microsoft for free.
Background pattern by Net Creations.

Friendless 1