home *** CD-ROM | disk | FTP | other *** search
/ NetObjects Fusion 7 / Fusion7.iso / NetObjects Fusion / data1.cab / Language_Resource_-_English / Components / DynaButtons / PopButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-18  |  5.2 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 = null;
  24.    private Image backgroundImage = null;
  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.    PopButton(DynaButtons parent, PopObject popObject, String buttonText, Color fontColor, Color highliteFontColor, Font buttonFont, int textJustification, int textAlignment, Image buttonImage, Image highliteImage, Color backgroundColor) {
  38.       this.parent = parent;
  39.       this.popObject = popObject;
  40.       this.buttonText = buttonText;
  41.       this.fontColor = fontColor;
  42.       this.highliteFontColor = highliteFontColor;
  43.       this.buttonFont = buttonFont;
  44.       this.textJustification = textJustification;
  45.       this.textAlignment = textAlignment;
  46.       this.buttonImage = buttonImage;
  47.       this.highliteImage = highliteImage;
  48.       this.backgroundColor = backgroundColor;
  49.       this.currentImage = buttonImage;
  50.       this.currentFontColor = fontColor;
  51.       ((Component)this).repaint();
  52.    }
  53.  
  54.    PopButton(DynaButtons parent, PopObject popObject, String buttonText, Color fontColor, Color highliteFontColor, Font buttonFont, int textJustification, int textAlignment, Image buttonImage, Image highliteImage, Image backgroundImage) {
  55.       this.parent = parent;
  56.       this.popObject = popObject;
  57.       this.buttonText = buttonText;
  58.       this.fontColor = fontColor;
  59.       this.buttonFont = buttonFont;
  60.       this.textJustification = textJustification;
  61.       this.textAlignment = textAlignment;
  62.       this.buttonImage = buttonImage;
  63.       this.highliteImage = highliteImage;
  64.       this.backgroundImage = backgroundImage;
  65.       this.currentImage = buttonImage;
  66.       this.currentFontColor = fontColor;
  67.       ((Component)this).repaint();
  68.    }
  69.  
  70.    private void drawBackground(Graphics g) {
  71.       if (this.backgroundColor != null) {
  72.          g.setColor(this.backgroundColor);
  73.          Dimension d = ((Component)this).size();
  74.          g.fillRect(0, 0, d.width, d.height);
  75.       } else if (this.backgroundImage != null) {
  76.          int imHeight = this.backgroundImage.getHeight(this);
  77.          int imWidth = this.backgroundImage.getWidth(this);
  78.          Dimension d = ((Component)this).size();
  79.          int x = d.width / imWidth + 1;
  80.          int y = d.height / imHeight + 1;
  81.  
  82.          for(int i = 0; i < y; ++i) {
  83.             for(int j = 0; j < x; ++j) {
  84.                g.drawImage(this.backgroundImage, j * imWidth, i * imHeight, this);
  85.             }
  86.          }
  87.       }
  88.  
  89.    }
  90.  
  91.    private int getHspace(FontMetrics fm) {
  92.       int imgWidth = this.currentImage.getWidth(this);
  93.       int hspace;
  94.       switch (this.textAlignment & 224) {
  95.          case 32:
  96.             hspace = 0;
  97.             break;
  98.          case 64:
  99.             hspace = (imgWidth - fm.stringWidth(this.buttonText)) / 2;
  100.             break;
  101.          case 128:
  102.             hspace = imgWidth - fm.stringWidth(this.buttonText);
  103.             break;
  104.          default:
  105.             hspace = (imgWidth - fm.stringWidth(this.buttonText)) / 2;
  106.       }
  107.  
  108.       return hspace;
  109.    }
  110.  
  111.    private int getVspace(FontMetrics fm) {
  112.       int imgHeight = this.currentImage.getHeight(this);
  113.       int vspace;
  114.       switch (this.textAlignment & 1792) {
  115.          case 256:
  116.             vspace = fm.getHeight();
  117.             break;
  118.          case 512:
  119.             vspace = (imgHeight + fm.getHeight()) / 2 - 3;
  120.             break;
  121.          case 1024:
  122.             vspace = imgHeight - 3;
  123.             break;
  124.          default:
  125.             vspace = (imgHeight + fm.getHeight()) / 2 - 3;
  126.       }
  127.  
  128.       return vspace;
  129.    }
  130.  
  131.    public boolean mouseDown(Event evt, int x, int y) {
  132.       this.popObject.setMessageType(2);
  133.       this.parent.action(this.popObject);
  134.       return false;
  135.    }
  136.  
  137.    public boolean mouseEnter(Event evt, int x, int y) {
  138.       this.currentImage = this.highliteImage;
  139.       this.currentFontColor = this.highliteFontColor;
  140.       ((Component)this).repaint();
  141.       this.popObject.setMessageType(1);
  142.       this.parent.action(this.popObject);
  143.       return false;
  144.    }
  145.  
  146.    public boolean mouseExit(Event evt, int x, int y) {
  147.       this.currentImage = this.buttonImage;
  148.       this.currentFontColor = this.fontColor;
  149.       ((Component)this).repaint();
  150.       this.popObject.setMessageType(0);
  151.       this.parent.action(this.popObject);
  152.       return false;
  153.    }
  154.  
  155.    public void paint(Graphics g) {
  156.       this.drawBackground(g);
  157.       if (this.buttonImage != null) {
  158.          g.drawImage(this.currentImage, 0, 0, this);
  159.       }
  160.  
  161.       g.setFont(this.buttonFont);
  162.       FontMetrics fm = g.getFontMetrics(g.getFont());
  163.       int hspace = this.getHspace(fm);
  164.       int vspace = this.getVspace(fm);
  165.       g.setColor(this.currentFontColor);
  166.       g.drawString(this.buttonText, hspace, vspace);
  167.    }
  168.  
  169.    public void showButtonDown() {
  170.       this.currentImage = this.highliteImage;
  171.       this.currentFontColor = this.highliteFontColor;
  172.       ((Component)this).repaint();
  173.    }
  174.  
  175.    public void showButtonUp() {
  176.       this.currentImage = this.buttonImage;
  177.       this.currentFontColor = this.fontColor;
  178.       ((Component)this).repaint();
  179.    }
  180.  
  181.    public final synchronized void update(Graphics theG) {
  182.       Dimension d = ((Component)this).size();
  183.       if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
  184.          this.offScreenImage = ((Component)this).createImage(d.width, d.height);
  185.          this.offScreenSize = d;
  186.          this.offScreenGraphics = this.offScreenImage.getGraphics();
  187.          this.offScreenGraphics.setFont(((Component)this).getFont());
  188.       }
  189.  
  190.       this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
  191.       this.paint(this.offScreenGraphics);
  192.       theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
  193.    }
  194. }
  195.