/*===============================================================*/ /* MyPanel.java */ /*===============================================================*/ /** *
 * MyPanel.java: A simple graphical engine to create a triple
 *                 buffered lightweight container with double
 *                 buffered lightweight components.
 *
 * Revisions:    1.1 Fixed background problems, minimum sizing
 *                   problems with lightweights
 *
 *               1.0  July 15, 1998,
 *                  Created class MyPanel.java
 *
 * 
* * @author David Dagon * @version Version 1.0, August 17, 1998. */ /*===============================================================*/ import java.awt.*; import java.applet.Applet; import java.net.URL; import java.awt.image.*; class MyPanel extends Panel { /*===============================================================*/ /* INSTANCE VARIABLES */ /*===============================================================*/ /* */ /* scope datatype variable name */ /*---------------------------------------------------------------*/ private LightContainer container; private LightComponent[][] Cell; public Simple applet; public Image sheet; public static int w = 50, h = 50; public static int BlankCellW=3, BlankCellH=3; /* Note: the variables w, h, BlankCellW, and BlankCellH are specific to the shuffle board game. The variables container, cell, applet and sheet, while modified to support the game, are essential to any meaningful instance of this class. */ /*===============================================================*/ /* CONSTRUCTORS */ /*===============================================================*/ /** *
*
*    Constructor -- This constructor takes in a reference to the
*        applet that created it, and uses this reference to load
*        images.  In turn, these images are used to create light
*        weight components.
*
*    Precondition: An applet has called this constructor, passing
*        in a reference to itself.
*
*    Postcondition: The lightweight components are created, and
*        placed inside a lightweight container, which is then
*        added to this container.
*
*  
* */ public MyPanel(Simple applet) { /* select background color */ setBackground(Color.lightGray); /* store the applet reference, and create a URL reference */ this.applet = applet; URL b = applet.getCodeBase(); /* load the image. */ sheet = applet.getImage(b, "test.gif"); /* create the lightweight container */ container = new LightContainer(); /* create an array of images */ Cell = new LightComponent[4][4]; /* crop the large image "sheet" into 15 smaller images */ for (int i=0;i<4;i++) for (int j=0; j<4; j++) { /* leave on square blank */ if (j==3 && i ==3) break; /* crop the image */ ImageFilter f = new CropImageFilter (i*w, j*h, w, h); ImageProducer p = new FilteredImageSource(sheet.getSource(), f); Image Im = createImage(p); /* create a new lightweight component */ /* Here, the image 'Im' is passed in, along with a reference to this container. The 'false' argument means that the image is not draggable, but must be clicked to be moved. The ints i, j are specific to the shuffle board game */ Cell[i][j] = new LightComponent (Im, this, i, j, false); /* add the lightweight component to the lightweight container */ container.add(Cell[i][j]); /* set the bounds of the lightweight */ Cell[i][j].setBounds(i*w, j*h, w, h); } /* add the lightweight container to this container */ setLayout(new BorderLayout()); add(container, "Center"); } /*===============================================================*/ /** *
*
*     update -- overridden to smooth out some flickering
*
*       Precondition: The container is in need of updating
*
*       Postcondition: The container is not redrawn; instead,
*           repaint is called
*
*   
* * @see this#paint() -- called to complete drawing * * @param Graphics g -- the container's graphics context */ public void update (Graphics g) { paint (g); } // update /*===============================================================*/ /* ASSORTED ACCESSORS/MODIFIERS */ /* */ /* The following methods are particular to the shuffle */ /* puzzle program */ /*===============================================================*/ public boolean validateMove(int LocW, int LocH) { return !( (Math.abs(LocW-BlankCellW)) + (Math.abs(LocH-BlankCellH)) > 1); } /*===============================================================*/ public int getBlankLocH() { return BlankCellH;} /*===============================================================*/ public int getBlankLocW() { return BlankCellW;} /*===============================================================*/ public int getW() { return w; } /*===============================================================*/ public int getH() { return h;} /*===============================================================*/ public Simple getApplet() { return applet; } /*===============================================================*/ public void setBlankLocH(int BlankCellH) { this.BlankCellH = BlankCellH;} /*===============================================================*/ public void setBlankLocW(int BlankCellW) { this.BlankCellW = BlankCellW;} /*===============================================================*/ public void checkCompletion() { return; } }