Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30390

Could this be done better

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Could this be done better
Organization Decebal Computing
Date 2014-11-24 12:16 +0100
Message-ID <87zjbg97c5.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


I have mostly programmed back-ends. But I now want also to do some
front-end programming.

I wrote the following simple program. It does nothing useful, it just
displays a description in different colours with the colour appended
to the description.

I was just wondering if this could be done better. Especially
‘updateColour’. First I did not removed the text, but then I could see
the colour changing before the text was changed. This way I find more
pleasing and I do not notice that the text disappears.

    import java.awt.*;
    import java.util.*;

    import javax.swing.*;

    @SuppressWarnings("serial")
    public class ColourCycling extends JFrame {
        // public functions
        public ColourCycling() {
            java.util.Timer colourTimer = new java.util.Timer();

            getContentPane().setBackground(background);
            setLayout(new GridBagLayout());
            label.setFont(new Font("serif", Font.BOLD, 25));
            add(label);
            pack();
            setSize(getWidth(), 3 * getHeight());
            label.setBackground(background);
            updateColour(getCurrentColour());
            colourTimer.schedule(new SwitchColour(), delay, delay);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle(description);
            setVisible(true);
        }

        public static void main(String[] args) {
            new ColourCycling();
        }

        // private classes
        private class Colour {
            // public
            public Colour(Color color, String description) {
                this.color          = color;
                this.description    = description;
            }

            public Color getColor() {
                return color;
            }

            public String getDescription() {
                return description;
            }

            // private
            private Color   color;
            private String  description;
        }

        private class SwitchColour extends TimerTask {
            public void run() {
                updateColour(getNextColour());
            }
        }

        //private functions
        private Colour getCurrentColour() {
            return colours[currentColour];
        }

        private Colour getNextColour() {
            currentColour = (currentColour + 1) % colours.length;
            return getCurrentColour();
        }

        private void updateColour(Colour newColour) {
            label.setText("");
            label.setForeground(newColour.getColor());
            label.setText(description + " : " + newColour.getDescription());
        }

        // private variables
        private Color       background      = Color.lightGray;
        private Colour[]    colours         = new Colour[] {
            new Colour(Color.blue,                  "Blue"),
            new Colour(Color.red,                   "Red"),
            new Colour(Color.orange,                "Orange"),
            new Colour(Color.yellow,                "Yellow"),
            new Colour(Color.green,                 "Green"),
            new Colour(Color.cyan,                  "Cyan"),
            new Colour(Color.magenta,               "Magenta"),
            new Colour(new Color(0x9C, 0x5D, 0x52), "Brown"),
        };
        private int         currentColour   = 0;
        private int         delay           = 4000;
        private String      description     = "JTextArea Color Change Example";
        private JLabel      label           = new JLabel(description + " : " + "XXXXXXXXXXXXXXX");

    }

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-24 12:16 +0100
  Re: Could this be done better "Qu0ll" <Qu0llSixFour@gmail.com> - 2014-11-24 22:49 +1100
    Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-24 17:54 +0100
      Re: Could this be done better Knute Johnson <eternal@knutejohnson.com> - 2014-11-24 09:37 -0800
        Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-24 19:27 +0100
  Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-24 09:23 -0500
    Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-24 17:45 +0100
      Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-24 13:42 -0500
        Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-25 00:12 +0100
    Re: Could this be done better Knute Johnson <eternal@knutejohnson.com> - 2014-11-24 09:19 -0800
      Re: Could this be done better Knute Johnson <eternal@knutejohnson.com> - 2014-11-24 09:41 -0800
        Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-25 00:55 +0100
      Re: Could this be done better Cecil Westerhof <Cecil@decebal.nl> - 2014-11-25 00:11 +0100
        Re: Could this be done better Knute Johnson <eternal@knutejohnson.com> - 2014-11-24 16:26 -0800
  Re: Could this be done better Brixomatic <brixomatic@yahoo.com> - 2014-11-25 19:58 +0100
    Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-25 14:36 -0500
      Re: Could this be done better Brixomatic <brixomatic@yahoo.com> - 2014-11-26 01:58 +0100
        Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-26 08:48 -0500
          Re: Could this be done better Brixomatic <brixomatic@yahoo.com> - 2014-11-29 19:43 +0100
            Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-29 15:44 -0500
              Re: Could this be done better Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-29 15:47 -0500

csiph-web