package class5_1_DuckFactories_v8; import javax.swing.JFrame; import java.awt.Color; /** * This class implements a virtual duck pond as a JFrame * window that contains JComponent-derived Ducks. * @author hornick */ public class DuckPond extends JFrame { public static final int PONDSIZE=500; // size of the duck pond (width & height) private static final long serialVersionUID = 1L; // needed if this class will be serializable /** * constructor - each call creates a separate TableTop * @param title window title */ public DuckPond(String title) { createUI(title); // create the user interface setVisible(true); // make it visible } /** * main UI JFrame initialization - size, title, layout, etc * @param title window title */ private void createUI(String title) { // create the containing window and set its size etc setTitle(title); setSize(PONDSIZE, PONDSIZE); // initial window size setResizable(true); // disallow resizing getContentPane().setBackground( Color.BLUE ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Configure the content pane for absolute layout getContentPane().setLayout(null); } /** * Adds a Ball (a type of JComponent) to the JFrame's content pane. * @param d reference to a Ball object to be added to the content pane. * @note JFrames maintain in internal List of JComponent-derived elements, * which are automatically displayed whenever the JFrame needs to refresh * itself. Since Ball is a JComponent, the Ball image is automatically * painted in the window. */ public void addDuck( Duck d ) { getContentPane().add(d); // adds a JComponent to the content pane of this JFrame repaint(); // update the JFrame after the new Ball is added } }