home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 11.3 KB | 333 lines |
-
- import java.awt.*;
- import java.io.*;
- import java.net.*;
- import java.applet.*;
-
- /**
- * Canvas sub-class to hold an Image
- */
- class XCanvas extends Canvas {
- private Image image;
- private String label;
- private int labelWidth;
- private Dimension size = null;
- private static Font font = new Font("Helvetica", Font.BOLD, 10);
- private FontMetrics fm;
- private int fontHeight;
-
- /**
- * Construct canvas from xbm data.
- */
- XCanvas(int w, int h, int[] bitmap,
- Color fg, Color bg, boolean trans, String label) {
- // create image
- image = createImage(new XImageSource(w, h, bitmap, fg, bg, trans));
-
- // init label & labe size
- initLabel(label);
- }
-
- /**
- * Construct canvas from xbm input stream.
- */
- XCanvas(InputStream is, Color fg, Color bg, boolean trans, String label) {
- // create image
- image = createImage(new XImageSource(is, fg, bg, trans));
-
- // init label & labe size
- initLabel(label);
- }
-
- /**
- * Construct canvas from xpm data.
- */
- XCanvas(String[] pixmap, String label) {
- // create image
- image = createImage(new XImageSource(pixmap));
-
- // init label & labe size
- initLabel(label);
- }
-
- /**
- * Construct canvas from xpm input stream.
- */
- XCanvas(InputStream is, String label) {
- // create image
- image = createImage(new XImageSource(is));
-
- // init label & label size
- initLabel(label);
- }
-
- void initLabel(String label) {
- this.label = label;
- fm = getFontMetrics(font);
- labelWidth = fm.stringWidth(label);
- fontHeight = fm.getHeight();
- }
-
- Dimension getSize() {
- int width = image.getWidth(this);
- int height = image.getHeight(this);
- width = Math.max(width, labelWidth);
- height += fontHeight + 2;
- return new Dimension(width+4, height+4);
- }
-
- /**
- * Paint Image with label
- */
- public void paint(Graphics g) {
- if (size == null)
- size = getSize();
-
- // draw image
- int imWidth = image.getWidth(this);
- int imHeight = image.getHeight(this);
- g.drawImage(image,
- size.width/2 - imWidth/2,
- 0,
- this);
-
- // draw text
- g.setFont(font);
- g.drawString(label,
- size.width/2 - labelWidth/2,
- size.height - fontHeight + fm.getAscent());
- }
-
- /**
- * Returns the preferred size of this component.
- * @see #minimumSize
- * @see LayoutManager
- */
- public Dimension preferredSize() {
- return minimumSize();
- }
-
- /**
- * Returns the minimum size of this component.
- * @see #preferredSize
- * @see LayoutManager
- */
- public synchronized Dimension minimumSize() {
- if (size == null)
- size = getSize();
- return size;
- }
- }
-
- /**
- * Simple Applet to view a few Images wrapped in a canvas...
- */
- public class XImageTest extends Applet {
-
- // static inline bitmaps/pixmaps:
- static private int[] cupBitmap = {
- 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xc1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x84, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x90, 0x18, 0x00, 0x00,
- 0x00, 0x20, 0x6e, 0x23, 0x00, 0x00, 0x00, 0x60, 0xef, 0x37, 0x00, 0x00,
- 0x00, 0xa0, 0xdf, 0xef, 0x03, 0x00, 0x00, 0x60, 0xfd, 0x77, 0x05, 0x00,
- 0x00, 0xa0, 0xaa, 0xea, 0x0b, 0x00, 0x00, 0x60, 0x55, 0x35, 0x16, 0x00,
- 0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
- 0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
- 0x00, 0xa0, 0xaa, 0x2a, 0x1c, 0x00, 0x00, 0x60, 0x55, 0x35, 0x14, 0x00,
- 0x00, 0xa0, 0xaa, 0x2a, 0x1a, 0x00, 0x00, 0x60, 0x55, 0x35, 0x0d, 0x00,
- 0x00, 0xa0, 0xaa, 0xea, 0x06, 0x00, 0x00, 0x60, 0x55, 0x75, 0x03, 0x00,
- 0x00, 0xa0, 0xaa, 0xea, 0x01, 0x00, 0x00, 0x60, 0x55, 0x35, 0x00, 0x00,
- 0x00, 0xa0, 0xaa, 0x2a, 0x00, 0x00, 0x00, 0x40, 0x55, 0x15, 0x00, 0x00,
- 0x00, 0x80, 0xab, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x03, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x8c, 0x03, 0x00, 0x06, 0x00, 0x00, 0xcc, 0x01, 0x00, 0x06, 0x00,
- 0x00, 0xec, 0x78, 0x3c, 0x66, 0x00, 0x00, 0x7c, 0xfc, 0x7e, 0x36, 0x00,
- 0x00, 0x7c, 0xc0, 0x0e, 0x1e, 0x00, 0x00, 0x6c, 0xf8, 0x3c, 0x3e, 0x00,
- 0x00, 0xcc, 0xcc, 0x70, 0x36, 0x00, 0x00, 0x8c, 0xfd, 0x7e, 0x36, 0x00,
- 0x00, 0x8c, 0xdb, 0x3c, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
-
- static private int[] errorBitmap = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xfe, 0x7f, 0x00,
- 0x80, 0xff, 0xff, 0x01, 0xc0, 0xff, 0xff, 0x03, 0xe0, 0x7f, 0xfe, 0x07,
- 0xf0, 0x3f, 0xfc, 0x0f, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x1f, 0xf8, 0x1f,
- 0xfc, 0x1f, 0xf8, 0x3f, 0xfc, 0x1f, 0xf8, 0x3f, 0xfe, 0x1f, 0xf8, 0x7f,
- 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f,
- 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x1f, 0xf8, 0x7f, 0xfe, 0x3f, 0xfc, 0x7f,
- 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f,
- 0xfc, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0x3f, 0xf8, 0x3f, 0xfc, 0x1f,
- 0xf8, 0x1f, 0xf8, 0x1f, 0xf0, 0x1f, 0xf8, 0x0f, 0xe0, 0x3f, 0xfc, 0x07,
- 0xc0, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0x01, 0x00, 0xfe, 0x7f, 0x00,
- 0x00, 0xf8, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00
- };
-
- static private String[] yesPixmap = {
- "64 40 6 1",
- " s None c None",
- ". c #0000FFFF0000",
- "X c #0000BFBF0000",
- "o c #00000000BFBF",
- "O c #808080808080",
- "+ c black",
- " ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " .... ",
- " ..XXXo ",
- " ..XXXXoo ",
- " ..XXXXXooO ",
- " ..XXXXXooOOO ",
- " ..XXXXXooOOOO ",
- " ..XXXXXooOOOO ",
- " ..XXXXXooOOOO ",
- " ..XXXXXooOOOO + + ",
- " ..XXXXXooOOOO + + ",
- " ..XXXXXooOOOO + + ",
- " ... ..XXXXooOOOO + + +++ +++ ",
- " ..XXo ..XXXXXooOOO + + + + + + ",
- " ..XXXXo..XXXXXooOOO + + + + + ",
- " ..XXXXo..XXXXXooOOO + +++++ +++ ",
- " ..XXXXXoXXXXXooOOO + + + ",
- " ..XXXXXXXXXooOOOO + + + ",
- " ..XXXXXXXXXooOOO + + + + + ",
- " ..XXXXXXXooOOO + +++ +++ ",
- " ..XXXXXXXooOOO ",
- " ..XXXXXooOOO ++++++ ",
- " ..XXXXXooOOO ",
- " ..XXXooOOO ",
- " ..XXXooOOO ",
- " .oooOOO ",
- " ooOOOO ",
- " OOOO ",
- " OO ",
- " ",
- " ",
- " ",
- " ",
- " ",
- " "
- };
-
- boolean canvasInitiated = false;
- GridLayout grid;
-
- public void init() {
- setBackground(new Color(0xEED5B7));
-
- // use grid layout
- grid = new GridLayout(2, 3, 4, 4);
- setLayout(grid);
- }
-
-
- void initCanvases() {
-
- // Create canvases
-
- // Inline Xbm, transparent
- add(new XCanvas(32, 32, errorBitmap,
- Color.red, null, true,
- "Xbm 32x32"));
-
- // Inline Xbm, with specified background
- add(new XCanvas(48, 48, cupBitmap,
- getForeground(), Color.white, false,
- "Xbm 48x48"));
-
- // Xbm file, transparent
- try {
- InputStream is = openFile("daffy-sm.xbm");
- add (new XCanvas(is, Color.blue, null, true,
- "daffy-sm.xbm"));
- is.close();
- }
- catch (Exception e) {
- System.err.println("Couldn't load file: daffy-sm.xbm");
- e.printStackTrace();
- }
-
- //Inline Xpm data
- add(new XCanvas(yesPixmap, "Xpm 64x40"));
-
- // Xpm files
- try {
- InputStream is = openFile("donald_duck.xpm");
- add (new XCanvas(is, "donald_duck.xpm"));
- is.close();
- }
- catch (Exception e) {
- System.err.println("Couldn't load file: donald_duck.xpm");
- e.printStackTrace();
- }
-
- try {
- InputStream is = openFile("rolodex.xpm");
- add (new XCanvas(is, "rolodex.xpm"));
- is.close();
- }
- catch (Exception e) {
- System.err.println("Couldn't load file: rolodex.xbm");
- e.printStackTrace();
- }
-
- layout();
- canvasInitiated = true;
- }
-
- /**
- * Open input stream to classes/pixmaps/<file>
- */
- private InputStream openFile(String file) throws IOException {
- InputStream is = null;
- URL url;
- URLConnection urlConnection = null;
-
- // create URL
- try {
- url = new URL(getDocumentBase(),
- "classes" + File.separatorChar +
- "pixmaps" + File.separatorChar + file);
- }
- catch (MalformedURLException ml) {
- throw new IOException("Malformed URL: " + file);
- }
-
- // and finally open input stream
- try {
- is = url.openStream();
- }
- catch (Exception e) {
- throw new IOException("Error opening input stream to URL:"
- + url.toString());
- }
- return is;
- }
-
- public void paint(Graphics g) {
- // add canvases (once)
- if (!canvasInitiated) {
- initCanvases();
- }
- }
-
- public static void main(String args[]) {
- Frame f = new Frame("XImageTest");
- XImageTest applet = new XImageTest();
- applet.init();
- applet.start();
-
- f.add("Center", applet);
- f.pack();
- f.resize(260, 200);
- f.show();
- }
- }
-