Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30405
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!newsfeed.fsmpi.rwth-aachen.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder03.ams.xsnews.nl!frontend-F10-13.ams.news.kpn.nl |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Could this be done better |
| Organization | Decebal Computing |
| References | <87zjbg97c5.fsf@Equus.decebal.nl> <m4vf1f$j03$1@dont-email.me> <m4vpb5$9ue$1@dont-email.me> <m4vqju$fnv$1@dont-email.me> |
| X-Face | "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E |
| X-Homepage | http://www.decebal.nl/ |
| Date | Tue, 25 Nov 2014 00:55:52 +0100 |
| Message-ID | <871tos6tmf.fsf@Equus.decebal.nl> (permalink) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) |
| Cancel-Lock | sha1:5u9sFT8iaYX78LjQ0qJclGTa/uI= |
| MIME-Version | 1.0 |
| Content-Type | text/plain |
| Lines | 181 |
| NNTP-Posting-Host | 77.171.42.144 |
| X-Trace | 1416873536 news.kpn.nl 21138 77.171.42.144@kpn/77.171.42.144:54149 |
| Xref | csiph.com comp.lang.java.programmer:30405 |
Show key headers only | View raw
Op Monday 24 Nov 2014 18:41 CET schreef Knute Johnson:
>
> // 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();
> }
>
> As much as I like your class Colour, it is probably easier and
> simpler to use an enum in this case.
>
> import java.awt.*;
>
> public class test {
> enum Colours {
> Red(Color.RED),Blue(Color.BLUE),Green(Color.GREEN);
>
> private final Color value;
>
> Colours(Color value) {
> this.value = value;
> }
>
> public Color value() {
> return value;
> }
> }
>
> public static void main(String... args) {
> System.out.println(Colours.Red);
> System.out.println(Colours.Red.value());
>
> System.out.println();
>
> for (Colours colour : Colours.values())
> System.out.println(colour+":"+colour.value());
> }
> }
>
> C:\Users\Knute Johnson>java test
> Red
> java.awt.Color[r=255,g=0,b=0]
>
> Red:java.awt.Color[r=255,g=0,b=0]
> Blue:java.awt.Color[r=0,g=0,b=255]
> Green:java.awt.Color[r=0,g=255,b=0]
I did not know that you could use enums in that way.
But I see two problems.
1. You can not change the list of colours. (Not a problem in my case.)
2. Officially enums should be all caps.
The code is now:
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 = new JLabel(description + " : " + "XXXXXXXXXXXXXXX");
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) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ColourCycling();
}
});
}
// private classes
private enum Colours {
Blue(Color.BLUE),
Red(Color.RED),
Orange(Color.ORANGE),
Yellow(Color.YELLOW),
Green(Color.GREEN),
Cyan(Color.CYAN),
Magenta(Color.MAGENTA),
Brown(new Color(0x9C, 0x5D, 0x52)),
;
private final Color value;
Colours(Color value) {
this.value = value;
}
public Color value() {
return value;
}
}
private class SwitchColour extends TimerTask {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateColour(getNextColour());
}
});
}
}
//private functions
private Colours getCurrentColour() {
return Colours.values()[currentColour];
}
private Colours getNextColour() {
currentColour = (currentColour + 1) % Colours.values().length;
return getCurrentColour();
}
private void updateColour(Colours newColour) {
label.setForeground(newColour.value());
label.setText(description + " : " + newColour);
}
// private variables
private Color background = Color.LIGHT_GRAY;
private int currentColour = 0;
private int delay = 4000;
private String description = "JTextArea Color Change Example";
private JLabel label;
}
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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