home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1998 October / DPPCPRO1098.ISO / Hotdog / files / snagpack.exe / IMAGEBUT.CLASS (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-21  |  5.6 KB  |  345 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.Panel;
  12. import java.awt.Point;
  13. import java.awt.Rectangle;
  14. import java.awt.image.ImageObserver;
  15.  
  16. public class imagebut extends Panel {
  17.    private int buttonNum;
  18.    private AudioClip clickSound;
  19.    private static final Color defaultBorderColor = new Color(160, 160, 160);
  20.    private static final Color defaultBackColor = new Color(192, 192, 192);
  21.    private static final Color defaultInColor = new Color(192, 192, 192);
  22.    private boolean isUp = true;
  23.    private boolean lastPressed;
  24.    private boolean isMouseDown;
  25.    private int borderWidth;
  26.    private Color borderColor;
  27.    private Color backColor;
  28.    private Color inColor;
  29.    private afkal applet;
  30.    private String name;
  31.    private Image image;
  32.    private Image buttonImage;
  33.    private boolean upAllDrawn;
  34.    private Image downImage;
  35.    private boolean downAllDrawn;
  36.    private MediaTracker tracker;
  37.    private boolean doneLoading;
  38.    private int height;
  39.    private int width;
  40.    private int oldW;
  41.    private int oldH;
  42.  
  43.    private void createButton() {
  44.       this.applet.showStatus("Generating buttons...");
  45.       this.upAllDrawn = false;
  46.       this.downAllDrawn = false;
  47.       Rectangle var1 = ((Component)this).bounds();
  48.       if (var1.width > 0 && var1.height > 0) {
  49.          if (this.buttonImage != null) {
  50.             this.buttonImage.flush();
  51.             this.buttonImage = null;
  52.          }
  53.  
  54.          if (this.downImage != null) {
  55.             this.downImage.flush();
  56.             this.buttonImage = null;
  57.          }
  58.  
  59.          this.buttonImage = ((Component)this).createImage(var1.width, var1.height);
  60.          this.downImage = ((Component)this).createImage(var1.width, var1.height);
  61.          int var2 = -1;
  62.          int var3 = -1;
  63.          if (this.image != null) {
  64.             int var4 = Math.min(var1.width, var1.height);
  65.             double var5 = var4 == var1.width ? (double)(var4 - 2 * this.borderWidth) / (double)this.image.getWidth(this) : (double)(var4 - 2 * this.borderWidth) / (double)this.image.getHeight(this);
  66.             var2 = (int)((double)this.image.getWidth(this) * var5);
  67.             var3 = (int)((double)this.image.getHeight(this) * var5);
  68.             if (var2 > var1.width - 2 * this.borderWidth) {
  69.                var5 = (double)(var1.width - 2 * this.borderWidth) / (double)var2;
  70.                var2 = (int)((double)var2 * var5);
  71.                var3 = (int)((double)var3 * var5);
  72.             }
  73.  
  74.             if (var3 > var1.height - 2 * this.borderWidth) {
  75.                var5 = (double)(var1.height - 2 * this.borderWidth) / (double)var3;
  76.                var2 = (int)((double)var2 * var5);
  77.                var3 = (int)((double)var3 * var5);
  78.             }
  79.  
  80.             if (var5 > (double)1.0F) {
  81.                var5 = (double)1.0F;
  82.             }
  83.          }
  84.  
  85.          Graphics var11 = this.buttonImage.getGraphics();
  86.          Graphics var14 = this.downImage.getGraphics();
  87.          this.drawBorder(var11, true);
  88.          new Point(-1, -1);
  89.          if (this.image != null) {
  90.             Point var6 = this.centreAt(var2, var3);
  91.  
  92.             while(!var11.drawImage(this.image, var6.x, var6.y, var2, var3, this)) {
  93.                try {
  94.                   Thread.sleep(5L);
  95.                } catch (InterruptedException var8) {
  96.                }
  97.             }
  98.          }
  99.  
  100.          this.upAllDrawn = true;
  101.          var2 = (int)((double)var2 * 0.9);
  102.          var3 = (int)((double)var3 * 0.9);
  103.          this.drawBorder(var14, false, (int)((double)var1.width * 0.9), (int)((double)var1.height * 0.9));
  104.          if (this.image != null) {
  105.             Point var16 = this.centreAt(var2, var3);
  106.  
  107.             while(!var14.drawImage(this.image, var16.x, var16.y, var2, var3, this)) {
  108.                try {
  109.                   Thread.sleep(5L);
  110.                } catch (InterruptedException var7) {
  111.                }
  112.             }
  113.          }
  114.  
  115.          this.downAllDrawn = true;
  116.          var11.dispose();
  117.          Object var12 = null;
  118.          var14.dispose();
  119.          Object var15 = null;
  120.          ((Component)this).repaint();
  121.          System.gc();
  122.          this.applet.showStatus("Buttons generated...");
  123.       }
  124.    }
  125.  
  126.    public boolean mouseEnter(Event var1, int var2, int var3) {
  127.       if (this.isMouseDown) {
  128.          this.isUp = !this.isUp;
  129.          ((Component)this).repaint();
  130.          this.clickSound.play();
  131.       }
  132.  
  133.       return true;
  134.    }
  135.  
  136.    public boolean mouseExit(Event var1, int var2, int var3) {
  137.       if (this.isMouseDown) {
  138.          this.isUp = !this.isUp;
  139.          ((Component)this).repaint();
  140.          this.clickSound.play();
  141.       }
  142.  
  143.       return true;
  144.    }
  145.  
  146.    public void paint(Graphics var1) {
  147.       if (!this.doneLoading) {
  148.          this.waitForImage(true);
  149.       } else if (this.isUp) {
  150.          if (!this.upAllDrawn) {
  151.             return;
  152.          }
  153.  
  154.          var1.drawImage(this.buttonImage, 0, 0, (ImageObserver)null);
  155.       } else {
  156.          if (!this.downAllDrawn) {
  157.             return;
  158.          }
  159.  
  160.          var1.drawImage(this.downImage, 0, 0, (ImageObserver)null);
  161.       }
  162.  
  163.    }
  164.  
  165.    public boolean mouseUp(Event var1, int var2, int var3) {
  166.       this.isMouseDown = false;
  167.       this.popButton();
  168.       var1.id = 1001;
  169.       var1.arg = this;
  170.       return super.action(var1, var1.arg);
  171.    }
  172.  
  173.    public void drawBorder(Graphics var1, boolean var2) {
  174.       Rectangle var3 = ((Component)this).bounds();
  175.       int var4 = 0;
  176.       int var5 = 0;
  177.       int var6 = var3.width;
  178.       int var7 = var3.height;
  179.       var1.setColor(this.borderColor);
  180.  
  181.       for(int var8 = 0; var8 < this.borderWidth; ++var8) {
  182.          var1.draw3DRect(var4, var5, var6, var7, var2);
  183.          ++var4;
  184.          ++var5;
  185.          var6 -= 2;
  186.          var7 -= 2;
  187.       }
  188.  
  189.       if (var2) {
  190.          var1.setColor(this.backColor);
  191.       } else {
  192.          var1.setColor(this.inColor);
  193.       }
  194.  
  195.       var1.fillRect(var4, var5, var6 + 1, var7 + 1);
  196.    }
  197.  
  198.    public void drawBorder(Graphics var1, boolean var2, int var3, int var4) {
  199.       Rectangle var5 = ((Component)this).bounds();
  200.       int var6 = (var5.width - var3) / 2;
  201.       int var7 = (var5.height - var4) / 2;
  202.       int var8 = var3;
  203.       int var9 = var4;
  204.       var1.setColor(this.applet.getBackground());
  205.       var1.fillRect(0, 0, var5.width, var5.height);
  206.       var1.setColor(this.borderColor);
  207.  
  208.       for(int var10 = 0; var10 < (int)((double)this.borderWidth * 0.9); ++var10) {
  209.          var1.draw3DRect(var6, var7, var8, var9, var2);
  210.          ++var6;
  211.          ++var7;
  212.          var8 -= 2;
  213.          var9 -= 2;
  214.       }
  215.  
  216.       if (var2) {
  217.          var1.setColor(this.backColor);
  218.       } else {
  219.          var1.setColor(this.inColor);
  220.       }
  221.  
  222.       var1.fillRect(var6, var7, var8 + 1, var9 + 1);
  223.    }
  224.  
  225.    public Dimension minimumSize() {
  226.       if (!this.doneLoading) {
  227.          this.waitForImage(false);
  228.       }
  229.  
  230.       return new Dimension(this.width, this.height);
  231.    }
  232.  
  233.    public void waitForImage(boolean var1) {
  234.       if (!this.doneLoading) {
  235.          this.applet.showStatus("Loading image " + this.name + " ...");
  236.  
  237.          try {
  238.             this.tracker.waitForID(0);
  239.          } catch (InterruptedException var3) {
  240.             System.out.println("Picture download was interrupted");
  241.          } catch (Exception var4) {
  242.             System.out.println("Error loading " + this.name + ":" + ((Throwable)var4).getMessage());
  243.             this.image = null;
  244.          }
  245.  
  246.          if (this.tracker.isErrorID(0)) {
  247.             System.out.println("Error loading image " + this.name);
  248.             this.image = null;
  249.          }
  250.  
  251.          this.doneLoading = true;
  252.          if (this.image != null) {
  253.             int var2 = Math.max(this.image.getWidth(this), this.image.getHeight(this));
  254.             this.width = var2 + 2 * this.borderWidth;
  255.             this.height = this.width;
  256.          }
  257.  
  258.          Container var5 = ((Component)this).getParent();
  259.          if (var5 != null && var1) {
  260.             var5.layout();
  261.          }
  262.  
  263.          if (this.buttonImage != null) {
  264.             ((Component)this).repaint();
  265.          }
  266.       }
  267.  
  268.    }
  269.  
  270.    private imagebut() {
  271.       this.borderColor = defaultBorderColor;
  272.       this.backColor = defaultBackColor;
  273.       this.inColor = defaultInColor;
  274.       this.oldW = -1;
  275.       this.oldH = -1;
  276.    }
  277.  
  278.    public imagebut(String var1, int var2, int var3, AudioClip var4, afkal var5) {
  279.       this.borderColor = defaultBorderColor;
  280.       this.backColor = defaultBackColor;
  281.       this.inColor = defaultInColor;
  282.       this.oldW = -1;
  283.       this.oldH = -1;
  284.       this.applet = var5;
  285.       this.buttonNum = var2;
  286.       this.image = ((Applet)var5).getImage(((Applet)var5).getDocumentBase(), var1);
  287.       this.clickSound = var4;
  288.       this.name = var1;
  289.       this.borderWidth = var3;
  290.       this.tracker = new MediaTracker(this);
  291.       this.tracker.addImage(this.image, 0);
  292.    }
  293.  
  294.    public int getButtonNum() {
  295.       return this.buttonNum;
  296.    }
  297.  
  298.    public boolean mouseDown(Event var1, int var2, int var3) {
  299.       this.isMouseDown = true;
  300.       this.isUp = false;
  301.       ((Component)this).repaint();
  302.       this.clickSound.play();
  303.       return true;
  304.    }
  305.  
  306.    public Dimension preferredSize() {
  307.       if (!this.doneLoading) {
  308.          this.waitForImage(false);
  309.       }
  310.  
  311.       return new Dimension(this.width, this.height);
  312.    }
  313.  
  314.    public void popButton() {
  315.       if (!this.isUp) {
  316.          this.isUp = !this.isUp;
  317.          ((Component)this).repaint();
  318.       }
  319.  
  320.    }
  321.  
  322.    private Point centreAt(int var1, int var2) {
  323.       Rectangle var3 = ((Component)this).bounds();
  324.       int var4 = var3.width;
  325.       int var5 = var3.height;
  326.       int var6 = var4 / 2 - var1 / 2;
  327.       int var7 = var5 / 2 - var2 / 2;
  328.       return new Point(var6, var7);
  329.    }
  330.  
  331.    public synchronized void reshape(int var1, int var2, int var3, int var4) {
  332.       if (!this.doneLoading) {
  333.          this.waitForImage(false);
  334.       }
  335.  
  336.       super.reshape(var1, var2, var3, var4);
  337.       if (var3 != this.oldW || var4 != this.oldH) {
  338.          this.createButton();
  339.          this.oldW = var3;
  340.          this.oldH = var4;
  341.       }
  342.  
  343.    }
  344. }
  345.