home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / dialogpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.9 KB  |  137 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // DialogPanel.java      v 1.00 b3
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:           v 1.00b3 26-03-1996
  8. // Released in public domain: v 1.00b3 26-03-1996
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for DialogPanel.
  12. //
  13. // This program and the Java source is in the public domain.
  14. // Permission to use, copy, modify, and distribute this software
  15. // and its documentation for NON-COMMERCIAL purposes and
  16. // without fee is hereby granted.
  17. //
  18. //    Copyright 1996
  19. //
  20. //    Iwan van Rienen
  21. //    Joan Maetsuyckerstr. 145
  22. //    2593 ZG  The Hague
  23. //    The Netherlands
  24. //
  25. // I am not responsible for any bugs in this program and
  26. // possible damage to hard- or software when using this program.
  27. //****************************************************************************
  28. import java.awt.*;
  29. import java.awt.image.*;
  30. import sun.awt.image.URLImageSource;
  31.  
  32. class DialogPanel extends Panel implements ImageObserver {
  33.     DigSimFrame frame = null;
  34.     static final int HORIZONTAL_GAP = 50;
  35.     protected Font SimpleDialogFont;
  36.     protected FontMetrics SimpleDialogFontMetrics;
  37.     String Capt;
  38.     int CaptWidth;
  39.     Image ImageToDisplay = null;
  40.     int imgw = 0, imgh = 0;
  41.     static final long updateRate = 100;
  42.  
  43. //----------------------------------------------------------------------------
  44. // The constructor of the SimpleDialog class.
  45. //----------------------------------------------------------------------------
  46.     public DialogPanel (DigSimFrame f, String capt, int ImageID) {
  47.         frame = f;
  48.         Capt = capt;
  49.         getImage (ImageID);
  50.  
  51.         setLayout( new BorderLayout());
  52.         SimpleDialogFont = new Font("TimesRoman",Font.PLAIN, 14);
  53.         SimpleDialogFontMetrics = getFontMetrics(SimpleDialogFont);
  54.         setFont(SimpleDialogFont);
  55.         super.setLayout( new BorderLayout());
  56.         CaptWidth = SimpleDialogFontMetrics.stringWidth(capt);
  57.         repaint();
  58.     }
  59.  
  60. //-----------------------------------------------------------------------------
  61. // Calculate and return the preferred size of the Dialog
  62. //-----------------------------------------------------------------------------
  63.     public Dimension preferredSize() {
  64.         return new Dimension(HORIZONTAL_GAP + CaptWidth, 125);
  65.     }
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Draw the message, and if available, the image
  69. //-----------------------------------------------------------------------------
  70.     public void paint(Graphics g) {
  71.         if (ImageToDisplay != null) {
  72.             g.drawImage (ImageToDisplay, 20, 10, this);
  73.             g.setColor (Color.black);
  74.             int x = (size().width - CaptWidth) / 2 + imgw + 20;
  75.             if (x < imgw + 50) x = imgw + 50;
  76.             g.drawString (Capt, x, 30);
  77.         } else {
  78.             g.setColor (Color.black);
  79.             int x = (size().width - CaptWidth) / 2;
  80.             g.drawString (Capt, x, 30);
  81.         }
  82.     }
  83.  
  84. //-----------------------------------------------------------------------------
  85. // Load an image from the specified URL
  86. //-----------------------------------------------------------------------------
  87.     public void getImage (int ImageID) {
  88.         String FileName = null;
  89.         switch (ImageID) {
  90.             case SimpleDialog.IMAGE_STOP:
  91.                 FileName = "images/stop.gif";
  92.                 break;
  93.             case SimpleDialog.IMAGE_WARNING:
  94.                 FileName = "images/warning.gif";
  95.                 break;
  96.             case SimpleDialog.IMAGE_NONE:
  97.             default:
  98.                 return;
  99.         }
  100.         if (frame != null) {
  101.             ImageToDisplay = frame.applet.getImage(frame.applet.getDocumentBase(), FileName);
  102.         }
  103.     }
  104.  
  105. //-----------------------------------------------------------------------------
  106. // There is new data available for the picture
  107. //-----------------------------------------------------------------------------
  108.     public synchronized boolean imageUpdate(Image img, int infoflags,
  109.                         int x, int y, int w, int h) {
  110.         boolean ret = true;
  111.         boolean dopaint = false;
  112.         long updatetime = 0;
  113.  
  114.         if ((infoflags & WIDTH) != 0) {
  115.             imgw = w;
  116.             dopaint = true;
  117.         }
  118.         if ((infoflags & HEIGHT) != 0) {
  119.             imgh = h;
  120.             dopaint = true;
  121.         }
  122.         if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
  123.             dopaint = true;
  124.             ret = false;
  125.         } else if ((infoflags & SOMEBITS) != 0) {
  126.             dopaint = true;
  127.             updatetime = updateRate;
  128.         }
  129.         if ((infoflags & ERROR) != 0) {
  130.             ret = false;
  131.         }
  132.         if (dopaint) {
  133.             repaint(updatetime);
  134.         }
  135.         return ret;
  136.     }
  137. }