001    package breakout;
002    
003    import java.awt.*;
004    import javax.swing.*;
005    
006    /**
007     * a little component that queries the World and Connector to
008     * display the number of balls remaining and the connected status
009     *
010     * @see breakout.World
011     * @see breakout.Connector
012     *
013     * @author benmv@olin.edu
014     * @version <tt>$Id: StatusComponent.java,v 1.2 2004/03/26 20:39:33 gus Exp $</tt>
015     */
016    public class StatusComponent extends JComponent implements Runnable {
017        public static final int REFRESH_INTERVAL = 200;
018        World world;
019        Connector connect;
020        
021        public StatusComponent(World w, Connector c) {
022            this.world = w;
023            this.connect = c;
024            
025            setPreferredSize(new Dimension(20,10));
026            setMinimumSize(new Dimension(20,10));
027            
028            Thread t = new Thread(this);
029            t.start();
030        }
031        
032        public void run() {
033            while (true) {
034                try {
035                    Thread.sleep(REFRESH_INTERVAL);
036                } catch (InterruptedException e) {
037                    // no response to being interrupted
038                    // probably should.. acting faster
039                }
040                repaint();
041            }
042        }
043        
044        public void paintComponent(Graphics gr) {
045            Graphics2D g = (Graphics2D)gr.create();
046            g.setPaint(Color.white);
047            g.draw(getBounds());
048            int balls = this.world.getBallsLeft();
049            g.setPaint(balls > 0 ? Color.green : Color.red);
050            g.drawString(String.valueOf(balls),0,10);
051            if ( this.connect != null ) {
052              g.setPaint(this.connect.isConnected() ? Color.green : Color.red);
053            }
054            else {
055              g.setPaint( Color.red );
056            }
057            g.fillOval(10,0,10,10);
058        }
059    }
060