home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / desvs7nu / src / ximagetest.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  11.3 KB  |  333 lines

  1.  
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.net.*;
  5. import java.applet.*;
  6.  
  7. /**
  8.  * Canvas sub-class to hold an Image
  9.  */
  10. class XCanvas extends Canvas {
  11.    private Image image;
  12.    private String label;
  13.    private int labelWidth;
  14.    private Dimension size = null;
  15.    private static Font font = new Font("Helvetica", Font.BOLD, 10);
  16.    private FontMetrics fm;
  17.    private int fontHeight;
  18.  
  19.    /**
  20.     * Construct canvas from xbm data.
  21.     */
  22.    XCanvas(int w, int h, int[] bitmap,
  23.          Color fg, Color bg, boolean trans, String label) {
  24.       // create image
  25.       image = createImage(new XImageSource(w, h, bitmap, fg, bg, trans));
  26.  
  27.       // init label & labe size
  28.       initLabel(label);
  29.    }
  30.  
  31.    /**
  32.     * Construct canvas from xbm input stream.
  33.     */
  34.    XCanvas(InputStream is, Color fg, Color bg, boolean trans, String label) {
  35.       // create image
  36.       image = createImage(new XImageSource(is, fg, bg, trans));
  37.  
  38.       // init label & labe size
  39.       initLabel(label);
  40.    }
  41.  
  42.    /**
  43.     * Construct canvas from xpm data.
  44.     */
  45.    XCanvas(String[] pixmap, String label) {
  46.       // create image
  47.       image = createImage(new XImageSource(pixmap));
  48.  
  49.       // init label & labe size
  50.       initLabel(label);
  51.    }
  52.    
  53.    /**
  54.     * Construct canvas from xpm input stream.
  55.     */
  56.    XCanvas(InputStream is, String label) {
  57.       // create image
  58.       image = createImage(new XImageSource(is));
  59.  
  60.       // init label & label size
  61.       initLabel(label);
  62.    }
  63.  
  64.    void initLabel(String label) {
  65.       this.label = label;
  66.       fm = getFontMetrics(font);
  67.       labelWidth = fm.stringWidth(label);
  68.       fontHeight = fm.getHeight();
  69.    }
  70.    
  71.    Dimension getSize() {
  72.       int width = image.getWidth(this);
  73.       int height = image.getHeight(this);
  74.       width = Math.max(width, labelWidth);
  75.       height += fontHeight + 2;
  76.       return new Dimension(width+4, height+4);
  77.    }
  78.  
  79.    /**
  80.     * Paint Image with label
  81.     */
  82.    public void paint(Graphics g) {
  83.       if (size == null)
  84.      size = getSize();
  85.  
  86.       // draw image
  87.       int imWidth = image.getWidth(this);
  88.       int imHeight = image.getHeight(this);
  89.       g.drawImage(image,
  90.           size.width/2 - imWidth/2,
  91.           0,
  92.           this);
  93.  
  94.       // draw text
  95.       g.setFont(font);
  96.       g.drawString(label,
  97.            size.width/2 - labelWidth/2,
  98.            size.height - fontHeight + fm.getAscent());
  99.    }
  100.  
  101.    /** 
  102.     * Returns the preferred size of this component.
  103.     * @see #minimumSize
  104.     * @see LayoutManager
  105.     */
  106.    public Dimension preferredSize() {
  107.       return minimumSize();
  108.    }
  109.    
  110.    /**
  111.     * Returns the minimum size of this component.
  112.     * @see #preferredSize
  113.     * @see LayoutManager
  114.     */
  115.    public synchronized Dimension minimumSize() {
  116.       if (size == null)
  117.      size = getSize();
  118.       return size;
  119.    }
  120. }
  121.  
  122. /**
  123.  * Simple Applet to view a few Images wrapped in a canvas...
  124.  */
  125. public class XImageTest extends Applet {
  126.  
  127.    // static inline bitmaps/pixmaps:
  128.    static private int[] cupBitmap = {
  129.       0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  130.       0x00, 0x00, 0xc1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
  131.       0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x84, 0x01, 0x00, 0x00,
  132.       0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  133.       0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x90, 0x18, 0x00, 0x00,
  134.       0x00, 0x20, 0x6e, 0x23, 0x00, 0x00, 0x00, 0x60, 0xef, 0x37, 0x00, 0x00,
  135.       0x00, 0xa0, 0xdf, 0xef, 0x03, 0x00, 0x00, 0x60, 0xfd, 0x77, 0x05, 0x00,
  136.       0x00, 0xa0, 0xaa, 0xea, 0x0b, 0x00, 0x00, 0x60, 0x55, 0x35, 0x16, 0x00,
  137.       0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
  138.       0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
  139.       0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
  140.       0x00, 0xa0, 0xaa, 0x2a, 0x1a, 0x00, 0x00, 0x60, 0x55, 0x35, 0x0d, 0x00,
  141.       0x00, 0xa0, 0xaa, 0xea, 0x06, 0x00, 0x00, 0x60, 0x55, 0x75, 0x03, 0x00,
  142.       0x00, 0xa0, 0xaa, 0xea, 0x01, 0x00, 0x00, 0x60, 0x55, 0x35, 0x00, 0x00,
  143.       0x00, 0xa0, 0xaa, 0x2a, 0x00, 0x00, 0x00, 0x40, 0x55, 0x15, 0x00, 0x00,
  144.       0x00, 0x80, 0xab, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x00,
  145.       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146.       0x00, 0x8c, 0x03, 0x00, 0x06, 0x00, 0x00, 0xcc, 0x01, 0x00, 0x06, 0x00,
  147.       0x00, 0xec, 0x78, 0x3c, 0x66, 0x00, 0x00, 0x7c, 0xfc, 0x7e, 0x36, 0x00,
  148.       0x00, 0x7c, 0xc0, 0x0e, 0x1e, 0x00, 0x00, 0x6c, 0xf8, 0x3c, 0x3e, 0x00,
  149.       0x00, 0xcc, 0xcc, 0x70, 0x36, 0x00, 0x00, 0x8c, 0xfd, 0x7e, 0x36, 0x00,
  150.       0x00, 0x8c, 0xdb, 0x3c, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151.       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152.       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153.    };
  154.    
  155.    static private int[] errorBitmap = {
  156.       0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xfe, 0x7f, 0x00,
  157.       0x80, 0xff, 0xff, 0x01, 0xc0, 0xff, 0xff, 0x03, 0xe0, 0x7f, 0xfe, 0x07,
  158.       0xf0, 0x3f, 0xfc, 0x0f, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x1f,
  159.       0xfc, 0x1f, 0xf8, 0x3f, 0xfc, 0x1f, 0xf8, 0x3f, 0xfe, 0x1f, 0xf8, 0x7f,
  160.       0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f,
  161.       0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x3f, 0xfc, 0x7f,
  162.       0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f,
  163.       0xfc, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x3f, 0xf8, 0x3f, 0xfc, 0x1f,
  164.       0xf8, 0x1f, 0xf8, 0x1f, 0xf0, 0x1f, 0xf8, 0x0f, 0xe0, 0x3f, 0xfc, 0x07,
  165.       0xc0, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0x01, 0x00, 0xfe, 0x7f, 0x00,
  166.       0x00, 0xf8, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00
  167.    };
  168.  
  169.    static private String[] yesPixmap = {
  170.       "64 40 6 1",
  171.       "       s None  c None",
  172.       ".      c #0000FFFF0000",
  173.       "X      c #0000BFBF0000",
  174.       "o      c #00000000BFBF",
  175.       "O      c #808080808080",
  176.       "+      c black",
  177.       "                                                                ",
  178.       "                                                                ",
  179.       "                                                                ",
  180.       "                                                                ",
  181.       "                                                                ",
  182.       "                                                                ",
  183.       "                        ....                                    ",
  184.       "                       ..XXXo                                   ",
  185.       "                      ..XXXXoo                                  ",
  186.       "                     ..XXXXXooO                                 ",
  187.       "                    ..XXXXXooOOO                                ",
  188.       "                   ..XXXXXooOOOO                                ",
  189.       "                  ..XXXXXooOOOO                                 ",
  190.       "                 ..XXXXXooOOOO                                  ",
  191.       "                ..XXXXXooOOOO        +     +                    ",
  192.       "               ..XXXXXooOOOO         +     +                    ",
  193.       "              ..XXXXXooOOOO           +   +                     ",
  194.       "       ...    ..XXXXooOOOO            +   +   +++   +++         ",
  195.       "      ..XXo  ..XXXXXooOOO              + +   +   + +   +        ",
  196.       "     ..XXXXo..XXXXXooOOO               + +   +   + +            ",
  197.       "     ..XXXXo..XXXXXooOOO                +    +++++  +++         ",
  198.       "     ..XXXXXoXXXXXooOOO                 +    +         +        ",
  199.       "      ..XXXXXXXXXooOOOO                 +    +         +        ",
  200.       "      ..XXXXXXXXXooOOO                  +    +   + +   +        ",
  201.       "       ..XXXXXXXooOOO                   +     +++   +++         ",
  202.       "       ..XXXXXXXooOOO                                           ",
  203.       "        ..XXXXXooOOO                  ++++++                    ",
  204.       "        ..XXXXXooOOO                                            ",
  205.       "         ..XXXooOOO                                             ",
  206.       "         ..XXXooOOO                                             ",
  207.       "           .oooOOO                                              ",
  208.       "            ooOOOO                                              ",
  209.       "             OOOO                                               ",
  210.       "              OO                                                ",
  211.       "                                                                ",
  212.       "                                                                ",
  213.       "                                                                ",
  214.       "                                                                ",
  215.       "                                                                ",
  216.       "                                                                "
  217.    };
  218.  
  219.    boolean canvasInitiated = false;
  220.    GridLayout grid;
  221.  
  222.    public void init() {
  223.       setBackground(new Color(0xEED5B7));
  224.  
  225.       // use grid layout
  226.       grid = new GridLayout(2, 3, 4, 4);
  227.       setLayout(grid);
  228.    }
  229.    
  230.    
  231.    void initCanvases() {
  232.  
  233.       // Create canvases
  234.  
  235.       // Inline Xbm, transparent
  236.       add(new XCanvas(32, 32, errorBitmap,
  237.               Color.red, null, true,
  238.               "Xbm 32x32"));
  239.  
  240.       // Inline Xbm, with specified background
  241.       add(new XCanvas(48, 48, cupBitmap,
  242.               getForeground(), Color.white, false,
  243.               "Xbm 48x48"));
  244.  
  245.       // Xbm file, transparent
  246.       try {
  247.      InputStream is = openFile("daffy-sm.xbm");
  248.      add (new XCanvas(is, Color.blue, null, true,
  249.               "daffy-sm.xbm"));
  250.      is.close();
  251.       }
  252.       catch (Exception e) {
  253.      System.err.println("Couldn't load file: daffy-sm.xbm");
  254.      e.printStackTrace();
  255.       }
  256.       
  257.       //Inline Xpm data
  258.       add(new XCanvas(yesPixmap, "Xpm 64x40"));
  259.       
  260.       // Xpm files
  261.       try {
  262.      InputStream is = openFile("donald_duck.xpm");
  263.      add (new XCanvas(is, "donald_duck.xpm"));
  264.      is.close();
  265.       }
  266.       catch (Exception e) {
  267.      System.err.println("Couldn't load file: donald_duck.xpm");
  268.      e.printStackTrace();
  269.       }
  270.  
  271.       try {
  272.      InputStream is = openFile("rolodex.xpm");
  273.      add (new XCanvas(is, "rolodex.xpm"));
  274.      is.close();
  275.       }
  276.       catch (Exception e) {
  277.      System.err.println("Couldn't load file: rolodex.xbm");
  278.      e.printStackTrace();
  279.       }
  280.       
  281.       layout();
  282.       canvasInitiated = true;
  283.    }
  284.  
  285.    /**
  286.     * Open input stream to classes/pixmaps/<file>
  287.     */
  288.    private InputStream openFile(String file) throws IOException {
  289.       InputStream is = null;
  290.       URL url;
  291.       URLConnection urlConnection = null;
  292.  
  293.       // create URL
  294.       try {
  295.          url = new URL(getDocumentBase(),
  296.                "classes" + File.separatorChar +
  297.                "pixmaps" + File.separatorChar + file);
  298.       }
  299.       catch (MalformedURLException ml) {
  300.          throw new IOException("Malformed URL: " + file);
  301.       }
  302.       
  303.       // and finally open input stream
  304.       try {
  305.          is = url.openStream();
  306.       }
  307.       catch (Exception e) {
  308.          throw new IOException("Error opening input stream to URL:"
  309.                                + url.toString());
  310.       }
  311.       return is;
  312.    }
  313.  
  314.    public void paint(Graphics g) {
  315.       // add canvases (once)
  316.       if (!canvasInitiated) {
  317.      initCanvases();
  318.       }
  319.    }
  320.    
  321.    public static void main(String args[]) {
  322.       Frame f = new Frame("XImageTest");
  323.       XImageTest applet = new XImageTest();
  324.       applet.init();
  325.       applet.start();
  326.  
  327.       f.add("Center", applet);
  328.       f.pack();
  329.       f.resize(260, 200);
  330.       f.show();
  331.    }
  332. }
  333.