home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / windowsxp / ftgateoffice / ftgateoffice.exe / Main / webhelp.jar / hhapplet / CanvasButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-11-07  |  3.0 KB  |  161 lines

  1. package hhapplet;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.Rectangle;
  11. import java.awt.image.ImageObserver;
  12. import java.util.Vector;
  13.  
  14. public class CanvasButton extends Canvas {
  15.    protected Rectangle inside;
  16.    protected Image img;
  17.    protected Vector listeners;
  18.    protected boolean button_push_state;
  19.  
  20.    public CanvasButton(Image var1) {
  21.       this();
  22.       this.img = var1;
  23.    }
  24.  
  25.    public CanvasButton() {
  26.       this.button_push_state = false;
  27.       this.img = null;
  28.       this.listeners = new Vector();
  29.       this.button_push_state = false;
  30.       ((Component)this).setBackground(new Color(192, 192, 192));
  31.    }
  32.  
  33.    public void paint(Graphics var1) {
  34.       try {
  35.          var1.setColor(((Component)this).getBackground());
  36.          var1.fillRect(0, 0, ((Component)this).bounds().width, ((Component)this).bounds().height);
  37.          if (this.img != null) {
  38.             var1.drawImage(this.img, 2, 2, (ImageObserver)null);
  39.          }
  40.  
  41.          if (this.button_push_state) {
  42.             this.paintBorderIn(var1);
  43.          } else {
  44.             this.paintBorderOut(var1);
  45.          }
  46.       } catch (Exception var3) {
  47.          ((Throwable)var3).printStackTrace();
  48.       }
  49.    }
  50.  
  51.    public void paintBorderOut(Graphics var1) {
  52.       Rectangle var2 = ((Component)this).bounds();
  53.       Color var3 = ((Component)this).getBackground().brighter();
  54.       Color var4 = var3.darker();
  55.       Color var5 = var4.darker();
  56.       Color var6 = var5.darker();
  57.       var1.setColor(var3);
  58.       var1.drawLine(0, 0, var2.width - 1, 0);
  59.       var1.drawLine(0, 0, 0, var2.height - 1);
  60.       var1.setColor(var4);
  61.       var1.drawLine(1, 1, var2.width - 2, 1);
  62.       var1.drawLine(1, 1, 1, var2.height - 2);
  63.       var1.setColor(var6);
  64.       var1.drawLine(var2.width - 2, 1, var2.width - 2, var2.height - 2);
  65.       var1.drawLine(1, var2.height - 2, var2.width - 2, var2.height - 2);
  66.       var1.setColor(var5);
  67.       var1.drawLine(var2.width - 1, 1, var2.width - 1, var2.height - 1);
  68.       var1.drawLine(0, var2.height - 1, var2.width - 1, var2.height - 1);
  69.    }
  70.  
  71.    public void update(Graphics var1) {
  72.       this.paint(var1);
  73.    }
  74.  
  75.    public void paintBorderIn(Graphics var1) {
  76.       Rectangle var2 = ((Component)this).bounds();
  77.       Color var3 = ((Component)this).getBackground().brighter();
  78.       Color var4 = var3.darker();
  79.       Color var5 = var4.darker();
  80.       Color var6 = var5.darker();
  81.       var1.setColor(var6);
  82.       var1.drawLine(0, 0, var2.width - 1, 0);
  83.       var1.drawLine(0, 0, 0, var2.height - 1);
  84.       var1.setColor(var5);
  85.       var1.drawLine(1, 1, var2.width - 2, 1);
  86.       var1.drawLine(1, 1, 1, var2.height - 2);
  87.       var1.setColor(var4);
  88.       var1.drawLine(var2.width - 2, 1, var2.width - 2, var2.height - 2);
  89.       var1.drawLine(1, var2.height - 2, var2.width - 2, var2.height - 2);
  90.       var1.setColor(var3);
  91.       var1.drawLine(var2.width - 1, 1, var2.width - 1, var2.height - 1);
  92.       var1.drawLine(0, var2.height - 1, var2.width - 1, var2.height - 1);
  93.    }
  94.  
  95.    public void removeButtonPushEventListener(ButtonPushEventListener var1) {
  96.       this.listeners.removeElement(var1);
  97.    }
  98.  
  99.    public Dimension preferredSize() {
  100.       return this.img != null ? new Dimension(this.img.getWidth(this) + 4, this.img.getHeight(this) + 4) : new Dimension(20, 20);
  101.    }
  102.  
  103.    public void addButtonPushEventListener(ButtonPushEventListener var1) {
  104.       this.listeners.addElement(var1);
  105.    }
  106.  
  107.    public boolean handleEvent(Event var1) {
  108.       if (var1.id == 501) {
  109.          this.button_push_state = true;
  110.          ((Component)this).repaint();
  111.          return true;
  112.       } else if (var1.id == 502 && this.button_push_state) {
  113.          this.button_push_state = false;
  114.          ((Component)this).repaint();
  115.          Rectangle var3 = ((Component)this).bounds();
  116.          var3.x = 0;
  117.          var3.y = 0;
  118.          if (var3.inside(var1.x, var1.y)) {
  119.             this.doAction(var1);
  120.          }
  121.  
  122.          return true;
  123.       } else if (var1.id == 506) {
  124.          Rectangle var2 = ((Component)this).bounds();
  125.          var2.x = 0;
  126.          var2.y = 0;
  127.          if (!var2.inside(var1.x, var1.y)) {
  128.             if (this.button_push_state) {
  129.                this.button_push_state = false;
  130.                ((Component)this).repaint();
  131.             }
  132.          } else if (!this.button_push_state) {
  133.             this.button_push_state = true;
  134.             ((Component)this).repaint();
  135.          }
  136.  
  137.          return true;
  138.       } else {
  139.          return false;
  140.       }
  141.    }
  142.  
  143.    protected void doAction(Event var1) {
  144.       ButtonPushEvent var3 = new ButtonPushEvent(this, var1.x, var1.y);
  145.       synchronized(this){}
  146.  
  147.       Vector var2;
  148.       try {
  149.          var2 = (Vector)this.listeners.clone();
  150.       } catch (Throwable var9) {
  151.          throw var9;
  152.       }
  153.  
  154.       for(int var4 = 0; var4 < var2.size(); ++var4) {
  155.          ButtonPushEventListener var5 = (ButtonPushEventListener)this.listeners.elementAt(var4);
  156.          var5.notifyButtonPushEvent(var3);
  157.       }
  158.  
  159.    }
  160. }
  161.