001    package breakout;
002    
003    import java.awt.*;
004    import java.awt.event.*;
005    import javax.swing.*;
006    import java.awt.geom.*;
007    
008    /**
009     * A Bumper is a mouse-controlled component that the user
010     * uses to keep balls in play.
011     *
012     * @see breakout.World
013     */
014    public class Bumper extends DefaultBreakoutComponent
015                  implements MouseMotionListener, MouseListener {
016    
017        public Bumper(Point location, Dimension size, World w) {
018            super(location,size,w);
019        }
020        
021        /**
022         * Bumpers render as white rectangles.
023         *
024         * @param g Graphics used to render Bumper.
025         */
026        public void paint(Graphics g) {
027            g.setColor(Color.white);
028            Dimension size = getSize();
029            g.fillRect(0,0,size.width,size.height);
030        }
031        
032        /**
033         * Bumpers don't do anything when hit by balls.
034         *
035         * @param striker BreakoutComponent that just escaped death.
036         */
037        public void hitBy(BreakoutComponent striker) {
038        }
039        
040        public void mouseDragged(MouseEvent e) {
041        }
042        
043        /**
044         * Bumper centers on the X coordinate of the mouse, while
045         * staying within the bounds of the world.
046         *
047         * @param e MouseEvent used to discover mouse location.
048         */
049        public void mouseMoved(MouseEvent e) {
050            synchronized (this.world) {
051                Point loc = getLocation();
052                Dimension size = getSize();
053                loc.x = e.getX()-size.width/2;
054                if (loc.x+size.width > World.MAXX)
055                    loc.x = World.MAXX-size.width;
056                else if (loc.x < 0)
057                    loc.x = 0;
058                setLocation(loc);
059            }
060        }
061        
062        public void mouseClicked(MouseEvent e) {
063        }
064    
065        public void mouseEntered(MouseEvent e) {
066        }
067    
068        public void mouseExited(MouseEvent e) {
069        }
070    
071        public void mousePressed(MouseEvent e) {
072        }
073    
074        public void mouseReleased(MouseEvent e) {
075        }
076    }
077