// BasicDrawingPanel.java
//
// Simple drawing Panel - one is created for each animation panel
// - Off screen graphics buffer should be created in constructor!
//
package ciips.animation;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.*;
/**
* BasicDrawingPanel
is the graphical panel attached to the
* animation frame AlgAnimFrame
. It is used for predecessor
* panels.
*
* This class is NOT TO BE MODIFIED. *
* * @see AlgAnimFrame#getDrawingPanel * @see DrawingObj */ public class BasicDrawingPanel extends Panel { private static int pref_height = 150; private static int pref_width = 250; protected Dimension pref_size = new Dimension( pref_width, pref_height ); protected Dimension offscreensize = null; protected Image offscreen = null; protected Graphics offGraphics = null; /* Link the panels together in a list, so that images can be shuffled down */ protected BasicDrawingPanel predecessor = null; private int index = 0; private static int seq = 0; private static final boolean DEBUG = false; /** * Creates a panel with white background */ public BasicDrawingPanel() { setBackground( Color.white ); predecessor = null; index = seq++; } // DrawingPanel() /** * Constructor which allows the default height and width to be * overridden */ public BasicDrawingPanel( int width, int height ) { this(); pref_size = new Dimension( width, height ); makeOffScreenBuffer( pref_size ); } /** * Returns the initial preferred size of the drawing panel. * This method is called by the layout manager during the creation * of the corresponding drawing frame. * @return The dimension of the drawing panel. */ public Dimension getPreferredSize() { return pref_size; } /** Set the preferred size */ public void setPreferredSize( int w, int h ) { pref_size.width = w; pref_size.height = h; } public void setPredecessor( BasicDrawingPanel pre ) { System.out.println("BasicDrawingPanel:setPredecessor " + index + "->" + ((pre==null)?(-1):pre.index) ); predecessor = pre; } /** * Get the image associated with this panel. * Used for shuffling the panels down */ public Image getImage() { return offscreen; } public void setImage( Image img ) { offscreen = img; } /** * Calls repaint() followed by delay(). Since these two methods * are being called very frequently in order, the redraw() method * is constructed to save typing. */ public void redraw() { repaint(); } /* Make sure that an offscreen buffer exists */ protected void makeOffScreenBuffer( Dimension d ) { if( DEBUG ) System.out.print("BDP(" + index + "):makeOSBuffer " + d ); // Create a new offscreen buffer if necessary if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) { offscreen = createImage( d.width, d.height ); if( offscreen == null ) { System.out.println("\nBDP(" + index + "):makeOSBuffer - off NULL??" ); } } else { if( DEBUG ) System.out.println(" - existing buffer"); } if( offscreen != null ) { if( DEBUG ) System.out.println("BDP(" + index + "):makeOSBuffer - off OK" ); offscreensize = d; offGraphics = offscreen.getGraphics(); } } public void clear() { Dimension d = size(); makeOffScreenBuffer( d ); offGraphics.setColor( Color.white ); offGraphics.fillRect( 0, 0, d.width, d.height ); } /** * Method to draw objects on the drawing panel. */ public void paint( Graphics g ) { int panel_height = size().height; int panel_width = size().width; g.setColor( Color.blue ); g.drawRect( 1, 1, panel_width-2, panel_height-2 ); if ( offscreen != null ) { g.drawImage( offscreen, 0, 0, null ); } else { System.out.println("BasicDrawingPanel:paint - null offscreen" ); } } // paint() public Graphics getOffScreenGraphics() { /* Make sure the off screen buffer exists */ makeOffScreenBuffer( size() ); return offGraphics; } public void drawFromOffscreen() { Graphics g = getGraphics(); g.drawImage( offscreen, 0, 0, null ); } /** Shuffle the images down from this panel to the previous one **/ public void shuffleDown() { if( DEBUG ) System.out.println("BasicDrawingPanel:shuffleDown " + index ); BasicDrawingPanel current = this, pre; Image cur_img, prev_img; prev_img = current.offscreen; while( (pre = current.predecessor) != null ) { if( DEBUG ) { System.out.print("**SD cur (" + current.index + ") + off "); if ( current.offscreen == null ) System.out.print("null"); else System.out.println( (current.offscreen).hashCode() ); System.out.print(" .. pre (" + pre.index + ")"); if ( pre.offscreen == null ) System.out.print("null"); else System.out.println( (pre.offscreen).hashCode() ); } Graphics g = pre.getGraphics(); cur_img = prev_img; prev_img = pre.offscreen; pre.offscreen = cur_img; if ( cur_img != null ) { g.drawImage( cur_img, 0, 0, null ); } current = pre; if( DEBUG ) System.out.println("BasicDrawingPanel:shuffleDown -> pre (" + current.index + ")"); } this.offscreen = null; // Force a new makeImage } } // class BasicDrawingPanel