Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #1781
| From | "Philipp" <philipp@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Can a jPanel's backgr |
| Message-ID | <1181468093_2183@sicinfo3.epfl.ch> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <737b62c1b91a9@uwe> |
| Date | 2011-04-27 15:35 +0000 |
| Organization | TDS.net |
To: comp.lang.java.gui
Andrew Thompson a |-crit :
> JT wrote:
>>> In any case, it is possible to *extend* JPanel (or jPanel,
>>> I am guessing) to provide a method that will do that.
>> Sometimes the fonts on my machine make jPanel and JPanel look the same,
>> especially if it's late at night.
>
> Please either get a better font, or better excuses..
>
>> ...But, to answer your question, I am
>> looking for a way to make my "flashing green" light flash. I looked in
>> the API but could not find anything which would do what I wanted,
>
> I find that completely unsurprising. I cannot imagine
> it is often that a GUI requires *flashing* panels.
> (muses) Perhaps slightly more common than a
> requirement for panels with polka dots, but not
> much so.
>
>> ...so I
>> thought someone might have done something similar in the past and would
>> be willing to share their ideas.
>
> I haven't, but a slight tweak of my earlier code should
> point *a* way to go about it. Note that this is still using
> AWT (just because I'm feeling perverse) and uses the
> centred circle (rather than the background) to change
> color. Further, no logic is performed to ensure that only
> one signal is flashing at any instant.
>
> Adapt as needed ('batteries not included').
>
> <sscce>
> import java.awt.*;
> import java.awt.event.*;
>
> class TrafficLight {
>
> public static void main(String[] args) {
> Frame f = new Frame("Lights");
> Panel lightboard = new Panel( new GridLayout(0,1) );
>
> lightboard.add( new TrafficSignal(Color.green) );
> lightboard.add( new TrafficSignal(Color.yellow) );
> lightboard.add( new TrafficSignal(Color.red) );
>
> f.add( lightboard );
> f.pack();
> f.addWindowListener( new WindowAdapter() {
> public void windowClosing(WindowEvent we) {
> System.exit(0);
> }
> } );
> f.setLocationRelativeTo(null);
> f.setVisible(true);
> }
> }
>
> class TrafficSignal extends Panel implements Runnable {
>
> Color on;
> int radius = 55;
> int border = 10;
> boolean active;
>
> boolean flashing = false;
> long startFlash = 0;
> /** It should flash for 10 seconds. */
> long lengthFlash = 10000;
>
>
> TrafficSignal(Color color) {
> on = color;
> active = true;
> this.addMouseListener( new MouseAdapter() {
> public void mouseClicked(MouseEvent me) {
> setFlashing(true);
> }
> } );
> Thread t = new Thread(this);
> t.start();
> }
>
> public Dimension getPreferredSize() {
> int size = (radius+border)*2;
> return new Dimension( size, size );
> }
>
> public void run() {
> while (true) {
> if ( flashing ) {
> active = !active;
> repaint();
> if ( System.currentTimeMillis()-startFlash>lengthFlash ) {
> flashing = false;
> }
> }
> try {
> Thread.sleep(400);
> } catch (InterruptedException ie) {
> // wake and continue
> }
> }
> }
>
> public void setFlashing(boolean flash) {
> flashing = flash;
> startFlash = System.currentTimeMillis();
> }
>
> public void paint(Graphics g) {
> g.setColor( Color.black );
> g.fillRect(0,0,getWidth(),getHeight());
>
> if (active) {
> g.setColor( on );
> } else {
> g.setColor( on.darker().darker().darker() );
> }
> g.fillOval( border,border,2*radius,2*radius );
> }
> }
> </sscce>
Shouldn't the GUI be updated from the EDT thread? How would this be done?
If I understand your SSCCE correctly, the frame is shown from the "main"
thread. And the blinking is updated from your "TrafficLight" thread.
Thanks for your insight
Phil
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can a jPanel's background "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "JT" <jt@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
Re: Can a jPanel's backgr "Philipp" <philipp@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
csiph-web