home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Netobjs / Install.exe / data1.cab / Components / DynaButtons / PopButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-16  |  5.1 KB  |  195 lines

  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.image.ImageObserver;
  11.  
  12. public class PopButton extends Canvas {
  13.    public static final int LINE_H_ALIGN_LEFT = 32;
  14.    public static final int LINE_H_ALIGN_CENTER = 64;
  15.    public static final int LINE_H_ALIGN_RIGHT = 128;
  16.    public static final int LINE_V_ALIGN_TOP = 256;
  17.    public static final int LINE_V_ALIGN_CENTER = 512;
  18.    public static final int LINE_V_ALIGN_BOTTOM = 1024;
  19.    private DynaButtons parent;
  20.    private Image buttonImage;
  21.    private Image highliteImage;
  22.    private Image currentImage;
  23.    private Color backgroundColor;
  24.    private Image backgroundImage;
  25.    private String buttonText;
  26.    private PopObject popObject;
  27.    private Color fontColor;
  28.    private Color highliteFontColor;
  29.    private Color currentFontColor;
  30.    private Font buttonFont;
  31.    private int textJustification;
  32.    private int textAlignment;
  33.    private Image offScreenImage;
  34.    private Graphics offScreenGraphics;
  35.    private Dimension offScreenSize;
  36.  
  37.    private int getVspace(FontMetrics fm) {
  38.       int imgHeight = this.currentImage.getHeight(this);
  39.       int vspace;
  40.       switch (this.textAlignment & 1792) {
  41.          case 256:
  42.             vspace = fm.getHeight();
  43.             break;
  44.          case 512:
  45.             vspace = (imgHeight + fm.getHeight()) / 2 - 3;
  46.             break;
  47.          case 1024:
  48.             vspace = imgHeight - 3;
  49.             break;
  50.          default:
  51.             vspace = (imgHeight + fm.getHeight()) / 2 - 3;
  52.       }
  53.  
  54.       return vspace;
  55.    }
  56.  
  57.    public boolean mouseEnter(Event evt, int x, int y) {
  58.       this.currentImage = this.highliteImage;
  59.       this.currentFontColor = this.highliteFontColor;
  60.       ((Component)this).repaint();
  61.       this.popObject.setMessageType(1);
  62.       this.parent.action(this.popObject);
  63.       return false;
  64.    }
  65.  
  66.    public void showButtonDown() {
  67.       this.currentImage = this.highliteImage;
  68.       this.currentFontColor = this.highliteFontColor;
  69.       ((Component)this).repaint();
  70.    }
  71.  
  72.    PopButton(DynaButtons parent, PopObject popObject, String buttonText, Color fontColor, Color highliteFontColor, Font buttonFont, int textJustification, int textAlignment, Image buttonImage, Image highliteImage, Color backgroundColor) {
  73.       this.parent = parent;
  74.       this.popObject = popObject;
  75.       this.buttonText = buttonText;
  76.       this.fontColor = fontColor;
  77.       this.highliteFontColor = highliteFontColor;
  78.       this.buttonFont = buttonFont;
  79.       this.textJustification = textJustification;
  80.       this.textAlignment = textAlignment;
  81.       this.buttonImage = buttonImage;
  82.       this.highliteImage = highliteImage;
  83.       this.backgroundColor = backgroundColor;
  84.       this.currentImage = buttonImage;
  85.       this.currentFontColor = fontColor;
  86.       ((Component)this).repaint();
  87.    }
  88.  
  89.    PopButton(DynaButtons parent, PopObject popObject, String buttonText, Color fontColor, Color highliteFontColor, Font buttonFont, int textJustification, int textAlignment, Image buttonImage, Image highliteImage, Image backgroundImage) {
  90.       this.parent = parent;
  91.       this.popObject = popObject;
  92.       this.buttonText = buttonText;
  93.       this.fontColor = fontColor;
  94.       this.buttonFont = buttonFont;
  95.       this.textJustification = textJustification;
  96.       this.textAlignment = textAlignment;
  97.       this.buttonImage = buttonImage;
  98.       this.highliteImage = highliteImage;
  99.       this.backgroundImage = backgroundImage;
  100.       this.currentImage = buttonImage;
  101.       this.currentFontColor = fontColor;
  102.       ((Component)this).repaint();
  103.    }
  104.  
  105.    public boolean mouseExit(Event evt, int x, int y) {
  106.       this.currentImage = this.buttonImage;
  107.       this.currentFontColor = this.fontColor;
  108.       ((Component)this).repaint();
  109.       this.popObject.setMessageType(0);
  110.       this.parent.action(this.popObject);
  111.       return false;
  112.    }
  113.  
  114.    private void drawBackground(Graphics g) {
  115.       if (this.backgroundColor != null) {
  116.          g.setColor(this.backgroundColor);
  117.          Dimension d = ((Component)this).size();
  118.          g.fillRect(0, 0, d.width, d.height);
  119.       } else if (this.backgroundImage != null) {
  120.          int imHeight = this.backgroundImage.getHeight(this);
  121.          int imWidth = this.backgroundImage.getWidth(this);
  122.          Dimension d = ((Component)this).size();
  123.          int x = d.width / imWidth + 1;
  124.          int y = d.height / imHeight + 1;
  125.  
  126.          for(int i = 0; i < y; ++i) {
  127.             for(int j = 0; j < x; ++j) {
  128.                g.drawImage(this.backgroundImage, j * imWidth, i * imHeight, this);
  129.             }
  130.          }
  131.       }
  132.  
  133.    }
  134.  
  135.    public void paint(Graphics g) {
  136.       this.drawBackground(g);
  137.       if (this.buttonImage != null) {
  138.          g.drawImage(this.currentImage, 0, 0, this);
  139.       }
  140.  
  141.       g.setFont(this.buttonFont);
  142.       FontMetrics fm = g.getFontMetrics(g.getFont());
  143.       int hspace = this.getHspace(fm);
  144.       int vspace = this.getVspace(fm);
  145.       g.setColor(this.currentFontColor);
  146.       g.drawString(this.buttonText, hspace, vspace);
  147.    }
  148.  
  149.    public final synchronized void update(Graphics theG) {
  150.       Dimension d = ((Component)this).size();
  151.       if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
  152.          this.offScreenImage = ((Component)this).createImage(d.width, d.height);
  153.          this.offScreenSize = d;
  154.          this.offScreenGraphics = this.offScreenImage.getGraphics();
  155.          this.offScreenGraphics.setFont(((Component)this).getFont());
  156.       }
  157.  
  158.       this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
  159.       this.paint(this.offScreenGraphics);
  160.       theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
  161.    }
  162.  
  163.    public void showButtonUp() {
  164.       this.currentImage = this.buttonImage;
  165.       this.currentFontColor = this.fontColor;
  166.       ((Component)this).repaint();
  167.    }
  168.  
  169.    public boolean mouseDown(Event evt, int x, int y) {
  170.       this.popObject.setMessageType(2);
  171.       this.parent.action(this.popObject);
  172.       return false;
  173.    }
  174.  
  175.    private int getHspace(FontMetrics fm) {
  176.       int imgWidth = this.currentImage.getWidth(this);
  177.       int hspace;
  178.       switch (this.textAlignment & 224) {
  179.          case 32:
  180.             hspace = 0;
  181.             break;
  182.          case 64:
  183.             hspace = (imgWidth - fm.stringWidth(this.buttonText)) / 2;
  184.             break;
  185.          case 128:
  186.             hspace = imgWidth - fm.stringWidth(this.buttonText);
  187.             break;
  188.          default:
  189.             hspace = (imgWidth - fm.stringWidth(this.buttonText)) / 2;
  190.       }
  191.  
  192.       return hspace;
  193.    }
  194. }
  195.