001    package breakout;
002    
003    import java.awt.*;
004    import java.awt.image.MemoryImageSource;
005    import javax.swing.*;
006    import javax.swing.border.*;
007    import java.util.*;
008    
009    /** Defines behavior for a JPanel built to display a Board. 
010      * <BR><BR>
011      * A BoardPanel is one of the building blocks that make up the
012      *  {@link breakout.BreakoutUI} JFrame.
013    **/
014    public class BoardPanel extends JPanel {
015            /** Default width **/
016            public static final int PREF_WIDTH = 350;
017            /** Default height **/
018            public static final int PREF_HEIGHT = 220;
019            
020            private BreakoutUI ui;
021            private Cursor transCursor;
022            private Cursor defCursor;
023            private boolean transified;
024            /** Creates a new BoardPanel in a particular BreakoutUI. 
025              * @param bui the BreakoutUI this BoardPanel belongs to. 
026            **/
027            public BoardPanel(BreakoutUI bui) {
028                    Dimension mySize = new Dimension(BoardPanel.PREF_WIDTH, BoardPanel.PREF_HEIGHT);
029                    this.setSize(mySize.width,mySize.height);
030                    this.setPreferredSize(mySize);
031                    this.setMaximumSize(mySize);
032                    this.setMinimumSize(mySize);
033                    this.setBorder(new EtchedBorder());
034                    
035                    this.ui = bui;
036                    
037                    /* This is how we build a "hidden" cursor:
038                     *  - Create an Image with no alpha -- essentially transparent
039                     *  - Create and store a custom cursor based on that image.
040                     */
041                    try {
042                        Toolkit tk = Toolkit.getDefaultToolkit();
043                        Image img = this.createImage(new MemoryImageSource(16, 16, new int[16*16], 0, 16));
044                        this.transCursor = tk.createCustomCursor(img, new Point(0,0), "trans");
045                        this.defCursor = new Cursor(Cursor.DEFAULT_CURSOR);
046                    } catch(IndexOutOfBoundsException ioobe) { System.err.println(ioobe.getMessage());
047                    } catch(HeadlessException he) { System.err.println(he.getMessage());
048                    }
049                    this.transified=false;
050            }
051            
052    
053            /** Switches the cursor for this BoardPanel between default and "hidden"
054            **/
055            public void transifyCursor() {
056                this.setCursor(this.transCursor);
057            }
058            
059            /** Switches the cursor for this BoardPanel between "hidden" and default.
060            **/
061            public void deTransifyCursor() {
062                this.setCursor(this.defCursor);
063            }
064            
065            /** Paints the BoardPanel and all the Board components.
066              *
067              * <BR><BR>How it works: A new Graphics is created for each BreakoutComponent, 
068              *  then translated to the BreakoutComponent's location.  The BreakoutComponent's
069              *  <code>paint(Graphics)</code> method is then called with the translated Graphics.
070              *
071              * @param g Graphics object for this panel's coordinate frame.
072            **/
073            public void paint(Graphics g) {
074                    super.paint(g);
075                    g.setColor(Color.white);
076                    g.fillRect(0,0,this.getSize().width, this.getSize().height);
077                    if(this.ui.getWorld()!=null) {
078                            Board board = this.ui.getWorld().getBoard();
079                            Iterator it = board.getAllComponents();
080                            while(it.hasNext()) {
081                                    BreakoutComponent bc = (BreakoutComponent)it.next();
082                                    Graphics gtemp = g.create();
083                                    gtemp.translate(bc.getLocation().x, bc.getLocation().y);
084                                    gtemp.setColor(Color.black);
085                                    bc.paint(gtemp);
086                            }
087                    }
088            }
089    }