import java.awt.*; import java.util.*; /** * A portal is a both scene's view and its controller. It must: * 1) display the scene * 2) translate user input into transformation operations */ public abstract class Portal extends Canvas implements Observer { protected Scene _scn; /** * The constructor. */ public Portal(Scene scn) { if (scn == null) throw new NullPointerException("null scene"); _scn = scn; _scn.addObserver(this); } /** * The observable's update callback. */ public void update(Observable obs, Object obj) { if (obs == _scn) repaint(); } /** * Automatically handle double-buffering. */ private Image _im = null; private Graphics _gr = null; private Dimension _dim = null; public void update(Graphics gr) { Dimension dim = size(); if (_im == null || _gr == null || _dim == null || _dim.height != dim.height || _dim.width != dim.width) { _dim = dim; _im = createImage(_dim.width, _dim.height); _gr = _im.getGraphics(); } _gr.setColor(getBackground()); _gr.fillRect(0, 0, _dim.width, _dim.height); _gr.setColor(getForeground()); paintScene(_gr); gr.drawImage(_im, 0, 0, this); } public void paint(Graphics gr) { update(gr); } /** * Define this. */ protected abstract void paintScene(Graphics gr); }