import java.awt.*; import javax.swing.*; public class BallGraphicsView extends JPanel implements SimView { private static final long serialVersionUID = 1L; // instance variables private SimModel model; // the SimModel we are viewing /** * Initialize this as a viewer linked to the given model. * @param model The model we are watching */ public BallGraphicsView(SimModel model) { this.model = model; } /** * React when notified of a change by a model by repainting this view */ public void notifyViewer() { repaint(); } /** * Repaint the simulation by requesting each item to repaint itself * @param g the graphics context where the painting should take place */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); // clear frame to background color (white, in this case) Rectangle bounds = getBounds(); g.clearRect(0, 0, bounds.width, bounds.height); java.util.List things = model.getThings(); for (SimThing thing: things) { thing.paintThing(g); } } }