Personal Java & Information Appliances, Part I
by Jaison Dolvane and Kumanan Yogaratnam


Listing One
public class SimpleIconItem extends TextItem {
    private Image m_img;
    private int m_gap;
    /** Construct an ImageItem.
     * @param imgb An image bundle.
     * @param name A name for the icon.
     * @param gap The space b/w the text and image.
     */ 
    public SimpleIconItem (Image img, String name, int gap) {
        super (name);
        m_img = img;
        m_gap = gap;
    }
    /* Simple cloning. */   
    public Object clone () {
        try {
            SimpleIconItem t = (SimpleIconItem) super.clone ();
            t.m_img = m_img;
            t.m_gap = m_gap;
            return t;
        }
        catch (Exception x) {
            return null;
        }
    }
    /* This method should return true if the item knows to draw differently
     * when selected.
     * @return Always true */
    public boolean canDrawSelected () {
        return true;
    }
    /* This method can be used to change the image associate with the icon.
     * @param img An image. */
    public void setImage (Image img) {
        m_img = img;
    }
    /* This method can be used to retrieve image associated with the icon.
     * @return An instance of Image, or null. */
    public Image getImage () {
        return m_img;
    }
    /* This method can be used to change the gap b/w image and text
     * @param gap An integer */
    public void setGap (int gap) {
        m_gap = gap;
    }
    /* This method can be used to retrieve the gap value. */
    public int getGap () {
        return m_gap;
    }
    /* This method returns the optimal dimensions of the item. */
    public Dimension getPreferredSize (Component owner, boolean sel) {
        Dimension d = super.getPreferredSize (owner, sel);
        if (m_img != null) {
            int imgw = m_img.getWidth (owner);
            int imgh = m_img.getHeight (owner);
            int gap = (getName() == null) ? 0 : m_gap;
            d.width += imgw + gap + 4;
            d.height = FixedMath.max (imgh, d.height) + 4;
        }
        return d;
    }
    /* This method draws the image item. */
    public void drawItem (Graphics g, Component owner, Rectangle 
                                                         box, boolean sel) {
        int bx = box.x;
        int by = box.y;
        int bw = box.width;
        int bh = box.height;
        
        // position and draw image if any
        if (m_img != null) {
            int imw = m_img.getWidth (owner);
            int imh = m_img.getHeight (owner);
            g.drawImage (m_img, bx, ((bh - imh) / 2)+by, owner);
            bx += imw + m_gap;
            bw -= imw + m_gap;
        }
        if (getName() != null) {
            // now position and draw the text w/o using Item
            FontMetrics fm = g.getFontMetrics ();
            String str = getName ();
            // now draw the text
            int txh = fm.getAscent () + fm.getDescent ();
            int txy = ((bh - txh) / 2) + by;
            
            if (sel) {
                g.setColor (ESystem.FOCUSED_BLUE);
                int txw = FixedMath.min (fm.stringWidth (str), bw-2);
                g.fillRect (bx-1, txy-1, txw+2+2, txh+2);
                g.setColor (Color.white);
            }
            g.drawString (str, bx+2, txy+fm.getAscent());
        }
    }
}

Listing Two
import espial.awt.*;
import espial.awt.layout.*;
import espial.awt.borders.*;
import java.awt.*;

public class ScrollbarLayoutDemo extends Frame {
    ScrollbarLayoutDemo () {
        super ("ScrollbarLayout Demo");
        setBackground (Color.gray);
        buildUI ();
    }
    public Dimension getPreferredSize () {
        return new Dimension (340, 450);
    }
    void buildUI () {
        BaseBorder bo;
        EContainer p = new EContainer ();
        p.setBorder (bo = new BaseBorder ());
        bo.setOuterInsets (25, 25, 25, 25);
        
        p.setLayout (new ScrollbarLayout (5, 5));
        
        // the buttons are laid out at the 9 possible positions
        p.add (new EButton (" NW "), ScrollbarLayout.NORTHWEST);
        p.add (new EButton (" NE "), ScrollbarLayout.NORTHEAST);
        p.add (new EButton (" SW "), ScrollbarLayout.SOUTHWEST);
        p.add (new EButton (" SE "), ScrollbarLayout.SOUTHEAST);
        p.add (new EButton (" North "), ScrollbarLayout.NORTH);
        p.add (new EButton (" South "), ScrollbarLayout.SOUTH);
        p.add (new EButton (" West "), ScrollbarLayout.WEST);
        p.add (new EButton (" East "), ScrollbarLayout.EAST);
        p.add (new EButton (" Center "), ScrollbarLayout.CENTER);
        
        setLayout (new GridLayout (1, 1));
        add (p);
    }
    public static void main (String [] args) {
        ScrollbarLayoutDemo f = new ScrollbarLayoutDemo ();
        f.pack ();
        f.show ();
    }
}

Listing Three
import espial.awt.*;
import espial.awt.layout.*;
import espial.awt.borders.*;
import espial.awt.item.*;
import java.awt.*;

class TableauLayoutDemo extends Frame {
    ETextField [] m_edInner;
    ETextField [] m_edOuter;
    EComboBox m_edImageKey;
    EComboBox m_edColor;
    ECheckbox m_edUseImage, m_edUseColor;
    
    TableauLayoutDemo () {
        super ("TableauLayout Demo");
        m_edInner = new ETextField [4];
        m_edOuter = new ETextField [4];
        setBackground (Color.lightGray);
        buildUI ();
    }
    public Dimension getPreferredSize () {
        return new Dimension (324, 240);
    }
    void buildUI () {
        EContainer p1, p2, p3;
        GroupBorder gbo;
        BaseBorder bo;
        int i;
        // the first tableau layout
        p1 = new EContainer ();
        p1.setLayout (new TableauLayout (5, 5, TableauLayout.LEFT, 
                                                    TableauLayout.TOP));
        p1.add (new ELabel ("Top"), new Point (1, 0));
        p1.add (new ELabel ("Left"), new Point (2, 0));
        p1.add (new ELabel ("Bot."), new Point (3, 0));
        p1.add (new ELabel ("Right"), new Point (4, 0));
        
        p1.add (new ELabel ("Inner Insets"), new Point (0, 1));
        p1.add (new ELabel ("Outer Insets"), new Point (0, 2));
        
      // inner inset fields
        p1.add (m_edInner [0] = new ETextField (4), new Point (1, 1));
        p1.add (m_edInner [1] = new ETextField (4), new Point (2, 1));
        p1.add (m_edInner [2] = new ETextField (4), new Point (3, 1));
        p1.add (m_edInner [3] = new ETextField (4), new Point (4, 1));
        
      // outer inset fields
        p1.add (m_edOuter [0] = new ETextField (4), new Point (1, 2));
        p1.add (m_edOuter [1] = new ETextField (4), new Point (2, 2));
        p1.add (m_edOuter [2] = new ETextField (4), new Point (3, 2));
        p1.add (m_edOuter [3] = new ETextField (4), new Point (4, 2));

        // the second tableau layout
        p2 = new EContainer ();
        p2.setLayout (new TableauLayout (5, 5, TableauLayout.LEFT, 
                                                       TableauLayout.TOP));
        ECheckboxGroup cg = new ECheckboxGroup ();
        
        p2.add (m_edUseColor = new ECheckbox ("Background Color", 
                                              false, cg), new Point (0, 0));
        m_edUseColor.setCentered (false);
        p2.add (m_edColor = new EComboBox (), new Point (1, 0));
        m_edColor.add (new TextItem ("No Color"));
        m_edColor.select (0);
        
        p2.add (m_edUseImage = new ECheckbox ("Background Image", 
                                               false, cg), new Point (0, 1));
        m_edUseImage.setCentered (false);
        p2.add (m_edImageKey = new EComboBox (), new Point (1, 1));
        m_edImageKey.add (new TextItem ("No Image"));
        m_edImageKey.select (0);
        
        // a border layout two hold the two tableaus together
        EFauxWindowContainer fwc = new EFauxWindowContainer ();
        p3 = fwc.getBackFaceContainer ();
        p3.setLayout (new BorderLayout (0, 5));
        p3.add (p2, BorderLayout.NORTH);
        p3.add (p1, BorderLayout.CENTER);
    //////////
        // and something for the frame.
        setLayout (new GridLayout (1, 1));
        add (fwc);
    }
    public static void main (String [] args) {
        TableauLayoutDemo f = new TableauLayoutDemo ();
        
        f.pack ();
        f.show ();
    }
}


Listing Four
import espial.awt.*;
import espial.awt.accessories.*;
import espial.awt.borders.*;
import espial.awt.item.*;
import java.awt.event.*;
import espial.image.*;

import java.awt.*;

public class AnimPage extends EContainer implements ActionListener {
    private AnimationView anim;
    private EButton start, stop;
    
    public AnimPage (Image [] frames) {
        buildUI (frames);
    }
    private void buildUI (Image [] frames) {
        anim = new AnimationView (frames, 120);
        setLayout (new BorderLayout ());
        add (anim, BorderLayout.CENTER);
        
        EContainer p0 = new EContainer ();
        p0.setLayout (new FlowLayout (FlowLayout.CENTER, 20, 5));
        
        p0.add (start = new EButton ("  Start  ", null, EButton.TOP));
        start.addActionListener (this);
        start.setTip ("Begin the animation");
        
        p0.add (stop = new EButton ("  Stop  ", null, EButton.TOP));
        stop.addActionListener (this);
        stop.setEnabled (false);
        stop.setTip ("Stop the animation.");
        
        add (p0, BorderLayout.SOUTH);
    }
    public void actionPerformed (ActionEvent e) {
        Object src = e.getSource ();
        
        if (src == start ) {
            start.setEnabled (false);
            stop.setEnabled (true);
            anim.startAnimation ();
            start.repaint ();
            stop.repaint ();
        }
        else if (src == stop) {
            stop.setEnabled (false);
            start.setEnabled (true);
            anim.stopAnimation ();
            start.repaint ();
            stop.repaint ();
            anim.repaint ();
        }
    }
}


Listing Five
public class BordersExample extends Frame implements WindowListener {
    /* Protected constructor used internally to set up the application. */
    protected BordersExample () {
        super ("BordersExample");
        addWindowListener (this);
        setBackground (Color.lightGray);
        buildUI ();
    }
    /* The method which holds the EXAMPLE code. Study the code to understand 
     * how to use the class this example demonstrates. */
    public void buildUI () {
        DoubleBufferPanel dbp = new DoubleBufferPanel ();
        ///////////  EXAMPLE STUDY CODE
        BaseBorder bo;
        dbp.setLayout (new GridLayout (3, 3));
        
        EContainer p0 = new EContainer ();
        p0.setLayout (new FlowLayout ());
        bo = new BaseBorder (Color.white);
        bo.setOuterInsets (5, 5, 5, 5);
        p0.setBorder (bo);
        p0.add (new ELabel ("BaseBorder"));
        dbp.add (p0);
        
        p0 = new EContainer ();
        p0.setLayout (new FlowLayout ());
        bo = new LineBorder (Color.white);
        bo.setOuterInsets (5, 5, 5, 5);
        p0.setBorder (bo);
        p0.add (new ELabel ("LineBorder"));
        dbp.add (p0);

        p0 = new EContainer ();
        p0.setLayout (new FlowLayout ());
        bo = new BevelBorder (true);
        bo.setOuterInsets (5, 5, 5, 5);
        p0.setBorder (bo);
        p0.add (new ELabel ("BevelBorder-Raised"));
        dbp.add (p0);

        p0 = new EContainer ();
        p0.setLayout (new FlowLayout ());
        bo = new BevelBorder (false);
        bo.setOuterInsets (5, 5, 5, 5);
        p0.setBorder (bo);
        p0.add (new ELabel ("BevelBorder-Lowered"));
        dbp.add (p0);

        p0 = new EContainer ();
        p0.setLayout (new FlowLayout ());
        bo = new GroupBorder (new TextItem ("Group Border"));
        bo.setOuterInsets (5, 5, 5, 5);
        p0.setBorder (bo);
        dbp.add (p0);
        ///////////
        
        setLayout (new BorderLayout ());
        add (dbp, BorderLayout.CENTER);
    }
    /* Application starts out sized at 450x480 pixels. */
    public Dimension getPreferredSize () {
        return new Dimension (450, 480);
    }
    /* Invoked when a window has been opened. */
    public void windowOpened(WindowEvent e) {
    }
    /* Invoked when a window is in the process of being closed.
     * The close operation can be overridden at this point. */
    public void windowClosing(WindowEvent e) {
        dispose ();
        System.exit (0);
    }
    /* Invoked when a window has been closed. */
    public void windowClosed(WindowEvent e) {
    }
    /* Invoked when a window is iconified. */
    public void windowIconified(WindowEvent e) {
    }
    /* Invoked when a window is de-iconified. */
    public void windowDeiconified(WindowEvent e) {
    }
    /* Invoked when a window is activated. */
    public void windowActivated(WindowEvent e) {
    }
    /* Invoked when a window is de-activated. */
    public void windowDeactivated(WindowEvent e) {
    }
    /* The method used to start the application by the JVM. */
    public static void main (String [] arg) {
        BordersExample f = new BordersExample ();
        f.pack ();
        f.show ();
    }
}

Listing Six
class MyApp extends Frame {
  ...
  // we decide if we want to use double buffering explicitly or rely
  // on pjava (if it provides it)
  EContainer p = null;
  Class comp = Component.class;
  try {
    Method dblchk = comp.getMethod ("isDoubleBuffered", new Class [0]);
    // method exists
    Boolean chk = (Boolean) dblchk.invoke (this, null);
    if (chk.booleanValue()) {
      // no need for double buffering ourselves
      p = new EContainer ();
    }
  }
  catch (NoSuchMethodException x) {
  }
  if (p == null) {
    // check if we made one, else force double buffering
    p = new DoubleBufferPanel ();
  }
  ...
}






9


