home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / progs / CDWEB / button.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-10-03  |  9.6 KB  |  442 lines

  1. import java.applet.Applet;
  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.MediaTracker;
  11. import java.awt.image.ImageObserver;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14.  
  15. public class button extends Applet implements Runnable {
  16.    // $FF: renamed from: w int
  17.    int field_0;
  18.    // $FF: renamed from: h int
  19.    int field_1;
  20.    int start;
  21.    Image offImage;
  22.    Graphics offGraphics;
  23.    Dimension offDimension;
  24.    Thread animationThread;
  25.    String prefex;
  26.    Color color = new Color(0, 0, 0);
  27.    int num_buttons = 1;
  28.    int direction;
  29.    int[] button_x;
  30.    int[] button_y;
  31.    int button_w;
  32.    int button_h;
  33.    int degree = 20;
  34.    int with = 1;
  35.    String[] label;
  36.    String[] link;
  37.    String[] frame;
  38.    Color[] b_clr_light;
  39.    Color[] b_clr_dark;
  40.    int f_size = 12;
  41.    int f_style;
  42.    int[] font_x;
  43.    int[] font_y;
  44.    int f_offset = 1;
  45.    Color f_color;
  46.    Color f_color2;
  47.    Font font1;
  48.    int selected;
  49.    int last;
  50.    int mouse_in;
  51.    int style;
  52.    int position;
  53.    int[] position_is;
  54.    int[] position_be;
  55.    int down;
  56.    int[] image_w;
  57.    int[] image_h;
  58.    int[] image_x;
  59.    int[] image_y;
  60.    Image[] sourceimage;
  61.  
  62.    public void init() {
  63.       this.field_0 = ((Component)this).size().width;
  64.       this.field_1 = ((Component)this).size().height;
  65.       this.prefex = ((Applet)this).getDocumentBase().toString();
  66.  
  67.       try {
  68.          this.num_buttons = Integer.parseInt(((Applet)this).getParameter("buttons"));
  69.       } catch (Exception var21) {
  70.       }
  71.  
  72.       try {
  73.          this.direction = Integer.parseInt(((Applet)this).getParameter("direction"));
  74.       } catch (Exception var20) {
  75.       }
  76.  
  77.       try {
  78.          this.with = Integer.parseInt(((Applet)this).getParameter("border width"));
  79.       } catch (Exception var19) {
  80.       }
  81.  
  82.       try {
  83.          this.position = Integer.parseInt(((Applet)this).getParameter("position"));
  84.       } catch (Exception var18) {
  85.       }
  86.  
  87.       try {
  88.          this.degree = Integer.parseInt(((Applet)this).getParameter("degree"));
  89.       } catch (Exception var17) {
  90.       }
  91.  
  92.       try {
  93.          this.f_offset = Integer.parseInt(((Applet)this).getParameter("f_offset"));
  94.       } catch (Exception var16) {
  95.       }
  96.  
  97.       try {
  98.          this.style = Integer.parseInt(((Applet)this).getParameter("style"));
  99.       } catch (Exception var15) {
  100.       }
  101.  
  102.       String temp_color = ((Applet)this).getParameter("color");
  103.       if (temp_color == null) {
  104.          temp_color = "000000";
  105.       }
  106.  
  107.       this.color = new Color(Integer.parseInt(temp_color, 16));
  108.  
  109.       try {
  110.          this.f_size = Integer.parseInt(((Applet)this).getParameter("f_size"));
  111.       } catch (Exception var14) {
  112.       }
  113.  
  114.       try {
  115.          this.f_style = Integer.parseInt(((Applet)this).getParameter("f_style"));
  116.       } catch (Exception var13) {
  117.       }
  118.  
  119.       String my_font = ((Applet)this).getParameter("font");
  120.       if (my_font == null) {
  121.          my_font = "Helvetica";
  122.       }
  123.  
  124.       String temp_f_color = ((Applet)this).getParameter("f_color");
  125.       if (temp_f_color == null) {
  126.          temp_f_color = "FFFFFF";
  127.       }
  128.  
  129.       this.f_color = new Color(Integer.parseInt(temp_f_color, 16));
  130.       temp_f_color = ((Applet)this).getParameter("f_color2");
  131.       if (temp_f_color == null) {
  132.          temp_f_color = "FFFFFF";
  133.       }
  134.  
  135.       this.f_color2 = new Color(Integer.parseInt(temp_f_color, 16));
  136.       this.image_w = new int[this.num_buttons];
  137.       this.image_h = new int[this.num_buttons];
  138.       this.image_x = new int[this.num_buttons];
  139.       this.image_y = new int[this.num_buttons];
  140.       this.position_is = new int[this.num_buttons];
  141.       this.position_be = new int[this.num_buttons];
  142.       this.sourceimage = new Image[this.num_buttons];
  143.       this.link = new String[this.num_buttons];
  144.       this.label = new String[this.num_buttons];
  145.       this.frame = new String[this.num_buttons];
  146.  
  147.       for(int a = 0; a < this.num_buttons; ++a) {
  148.          this.position_be[a] = 1;
  149.          this.position_is[a] = 0;
  150.          this.image_w[a] = 0;
  151.          this.image_h[a] = 0;
  152.          this.image_x[a] = 0;
  153.          this.image_y[a] = 0;
  154.          this.label[a] = ((Applet)this).getParameter("label " + Integer.toString(a));
  155.          if (this.label[a] == null) {
  156.             this.label[a] = "";
  157.          }
  158.  
  159.          this.link[a] = ((Applet)this).getParameter("link " + Integer.toString(a));
  160.          if (this.link[a] == null) {
  161.             this.link[a] = "";
  162.          }
  163.  
  164.          String imagename = ((Applet)this).getParameter("image " + Integer.toString(a));
  165.          if (imagename == null) {
  166.             imagename = "";
  167.          }
  168.  
  169.          this.frame[a] = ((Applet)this).getParameter("frame " + Integer.toString(a));
  170.          if (this.frame[a] == null) {
  171.             this.frame[a] = "_top";
  172.          }
  173.  
  174.          if (this.frame[a].equalsIgnoreCase("")) {
  175.             this.frame[a] = "_top";
  176.          }
  177.  
  178.          this.sourceimage[a] = ((Applet)this).getImage(((Applet)this).getCodeBase(), imagename);
  179.          this.loadimage(this.sourceimage[a], a);
  180.          this.image_w[a] = this.sourceimage[a].getWidth(this);
  181.          this.image_h[a] = this.sourceimage[a].getHeight(this);
  182.       }
  183.  
  184.       this.button_x = new int[this.num_buttons];
  185.       this.button_y = new int[this.num_buttons];
  186.       if (this.direction == 0) {
  187.          this.button_w = this.field_0 - 1;
  188.          this.button_h = this.field_1 / this.num_buttons;
  189.  
  190.          for(int a = 0; a < this.num_buttons; ++a) {
  191.             this.button_x[a] = 0;
  192.             this.button_y[a] = this.button_h * a;
  193.          }
  194.       } else {
  195.          this.button_h = this.field_1 - 1;
  196.          this.button_w = this.field_0 / this.num_buttons;
  197.  
  198.          for(int a = 0; a < this.num_buttons; ++a) {
  199.             this.button_y[a] = 0;
  200.             this.button_x[a] = this.button_w * a;
  201.          }
  202.       }
  203.  
  204.       int r = this.color.getRed();
  205.       int g = this.color.getGreen();
  206.       int b = this.color.getBlue();
  207.       this.b_clr_dark = new Color[this.with + 1];
  208.       this.b_clr_dark[0] = this.color;
  209.       this.b_clr_light = new Color[this.with + 1];
  210.       this.b_clr_light[0] = this.color;
  211.  
  212.       for(int a = 1; a < this.with + 1; ++a) {
  213.          this.b_clr_dark[a] = new Color(Math.max(0, r - a * this.degree), Math.max(0, g - a * this.degree), Math.max(0, b - a * this.degree));
  214.          this.b_clr_light[a] = new Color(Math.min(255, r + a * this.degree), Math.min(255, g + a * this.degree), Math.min(255, b + a * this.degree));
  215.       }
  216.  
  217.       int style = 0;
  218.       if (this.f_style == 1) {
  219.          style = 1;
  220.       } else if (this.f_style == 2) {
  221.          style = 2;
  222.       }
  223.  
  224.       this.font_x = new int[this.num_buttons];
  225.       this.font_y = new int[this.num_buttons];
  226.       this.font1 = new Font(my_font, style, this.f_size);
  227.       ((Component)this).setFont(this.font1);
  228.       FontMetrics fm = ((Component)this).getFontMetrics(this.font1);
  229.  
  230.       for(int a = 0; a < this.num_buttons; ++a) {
  231.          switch (this.position) {
  232.             case 0:
  233.                this.image_x[a] = this.button_x[a] + (this.button_w - this.image_w[a]) / 2;
  234.                this.image_y[a] = this.button_y[a] + (this.button_h - this.image_h[a]) / 2;
  235.                this.font_x[a] = this.button_x[a] + (this.button_w - fm.stringWidth(this.label[a])) / 2;
  236.                break;
  237.             case 1:
  238.                this.image_x[a] = this.button_x[a] + this.with + this.f_offset;
  239.                this.image_y[a] = this.button_y[a] + (this.button_h - this.image_h[a]) / 2;
  240.                this.font_x[a] = this.image_h[a] + this.button_x[a] + this.with + this.f_offset * 2;
  241.                break;
  242.             case 2:
  243.                this.image_x[a] = this.button_x[a] + this.button_w - this.with - this.image_w[a] - this.f_offset;
  244.                this.image_y[a] = this.button_y[a] + (this.button_h - this.image_h[a]) / 2;
  245.                this.font_x[a] = this.button_x[a] + this.button_w - this.with + 1 - fm.stringWidth(this.label[a]) - this.image_w[a] - this.f_offset * 2;
  246.          }
  247.  
  248.          this.font_y[a] = this.button_y[a] + this.button_h / 2 + fm.getHeight() / 3;
  249.       }
  250.  
  251.    }
  252.  
  253.    public void paintFrame(Graphics g) {
  254.       g.setFont(this.font1);
  255.  
  256.       for(int a = 0; a < this.num_buttons; ++a) {
  257.          if (this.position_be[a] != this.position_is[a]) {
  258.             int dwn = 0;
  259.             this.position_is[a] = this.position_be[a];
  260.             if (this.position_be[a] == 1) {
  261.                dwn = 1;
  262.             }
  263.  
  264.             g.setColor(this.color);
  265.             g.fillRect(this.button_x[a], this.button_y[a], this.button_w, this.button_h);
  266.             if (dwn == 1) {
  267.                g.drawImage(this.sourceimage[a], this.image_x[a], this.image_y[a], this.color, this);
  268.                g.setColor(this.f_color);
  269.                g.drawString(this.label[a], this.font_x[a], this.font_y[a]);
  270.             } else {
  271.                g.drawImage(this.sourceimage[a], this.image_x[a] + 1, this.image_y[a] + 1, this.color, this);
  272.                g.setColor(this.f_color2);
  273.                g.drawString(this.label[a], this.font_x[a] + 1, this.font_y[a] + 1);
  274.             }
  275.  
  276.             for(int b = 0; b < this.with; ++b) {
  277.                if (dwn == 0) {
  278.                   g.setColor(this.b_clr_dark[this.with - b]);
  279.                } else {
  280.                   g.setColor(this.b_clr_light[this.with - b]);
  281.                }
  282.  
  283.                g.drawLine(this.button_x[a] + b, this.button_y[a] + b, this.button_x[a] + this.button_w - b, this.button_y[a] + b);
  284.                g.drawLine(this.button_x[a] + b, this.button_y[a] + b, this.button_x[a] + b, this.button_y[a] + this.button_h - b);
  285.                if (dwn == 1) {
  286.                   g.setColor(this.b_clr_dark[this.with - b]);
  287.                } else {
  288.                   g.setColor(this.b_clr_light[this.with - b]);
  289.                }
  290.  
  291.                g.drawLine(this.button_x[a] + b, this.button_y[a] + this.button_h - b, this.button_x[a] + this.button_w - b, this.button_y[a] + this.button_h - b);
  292.                g.drawLine(this.button_x[a] + this.button_w - b, this.button_y[a] + this.button_h - b, this.button_x[a] + this.button_w - b, this.button_y[a] + b);
  293.             }
  294.          } else if (this.style == 0) {
  295.             if (a + 1 == this.mouse_in) {
  296.                g.setColor(this.color);
  297.                g.setColor(this.f_color2);
  298.                g.drawString(this.label[a], this.font_x[a], this.font_y[a]);
  299.             } else {
  300.                g.setColor(this.color);
  301.                g.setColor(this.f_color);
  302.                g.drawString(this.label[a], this.font_x[a], this.font_y[a]);
  303.             }
  304.          }
  305.       }
  306.  
  307.    }
  308.  
  309.    void loadimage(Image img, int btn) {
  310.       MediaTracker mt = new MediaTracker(this);
  311.       mt.addImage(img, 0);
  312.  
  313.       try {
  314.          mt.waitForID(0);
  315.       } catch (InterruptedException var4) {
  316.       }
  317.    }
  318.  
  319.    public boolean mouseDown(Event e, int x, int y) {
  320.       for(int a = 0; a < this.num_buttons; ++a) {
  321.          if (x >= this.button_x[a] && x < this.button_x[a] + this.button_w && y >= this.button_y[a] && y < this.button_y[a] + this.button_h) {
  322.             this.down = 1;
  323.  
  324.             for(int b = 0; b < this.num_buttons; ++b) {
  325.                this.position_be[b] = 1;
  326.             }
  327.  
  328.             this.position_be[a] = 0;
  329.             break;
  330.          }
  331.       }
  332.  
  333.       ((Component)this).repaint();
  334.       return true;
  335.    }
  336.  
  337.    public boolean mouseUp(Event e, int x, int y) {
  338.       for(int a = 0; a < this.num_buttons; ++a) {
  339.          if (x >= this.button_x[a] && x < this.button_x[a] + this.button_w && y >= this.button_y[a] && y < this.button_y[a] + this.button_h) {
  340.             this.down = 0;
  341.             if (this.style == 0) {
  342.                for(int b = 0; b < this.num_buttons; ++b) {
  343.                   this.position_be[b] = 1;
  344.                }
  345.             }
  346.  
  347.             if (!this.link[a].equalsIgnoreCase("")) {
  348.                if (this.frame[a].equalsIgnoreCase("")) {
  349.                   try {
  350.                      URL base_url = ((Applet)this).getDocumentBase();
  351.                      URL link_to = new URL(base_url, this.link[a]);
  352.                      ((Applet)this).getAppletContext().showDocument(link_to);
  353.                   } catch (MalformedURLException var8) {
  354.                   }
  355.                } else {
  356.                   try {
  357.                      URL base_url = ((Applet)this).getDocumentBase();
  358.                      URL link_to = new URL(base_url, this.link[a]);
  359.                      ((Applet)this).getAppletContext().showDocument(link_to, this.frame[a]);
  360.                   } catch (MalformedURLException var7) {
  361.                   }
  362.                }
  363.             }
  364.             break;
  365.          }
  366.       }
  367.  
  368.       ((Component)this).repaint();
  369.       return true;
  370.    }
  371.  
  372.    public boolean mouseMove(Event e, int x, int y) {
  373.       for(int a = 0; a < this.num_buttons; ++a) {
  374.          if (x >= this.button_x[a] && x < this.button_x[a] + this.button_w && y >= this.button_y[a] && y < this.button_y[a] + this.button_h) {
  375.             this.mouse_in = a + 1;
  376.             if (this.style == 1 & this.down == 0) {
  377.                for(int b = 0; b < this.num_buttons; ++b) {
  378.                   this.position_be[b] = 1;
  379.                }
  380.  
  381.                this.position_be[a] = 0;
  382.             }
  383.             break;
  384.          }
  385.       }
  386.  
  387.       ((Component)this).repaint();
  388.       return true;
  389.    }
  390.  
  391.    public boolean mouseExit(Event e, int x, int y) {
  392.       for(int b = 0; b < this.num_buttons; ++b) {
  393.          this.position_be[b] = 1;
  394.       }
  395.  
  396.       this.down = 0;
  397.       this.mouse_in = 0;
  398.       ((Component)this).repaint();
  399.       return true;
  400.    }
  401.  
  402.    public void update(Graphics g) {
  403.       Dimension dim = ((Component)this).size();
  404.       if (this.offGraphics == null || dim.width != this.offDimension.width || dim.height != this.offDimension.height) {
  405.          this.offDimension = dim;
  406.          this.offImage = ((Component)this).createImage(dim.width, dim.height);
  407.          this.offGraphics = this.offImage.getGraphics();
  408.          this.offGraphics.setColor(this.color);
  409.          this.offGraphics.fillRect(0, 0, this.field_0, this.field_1);
  410.       }
  411.  
  412.       this.paintFrame(this.offGraphics);
  413.       g.drawImage(this.offImage, 0, 0, (ImageObserver)null);
  414.    }
  415.  
  416.    public void start() {
  417.       if (this.animationThread == null) {
  418.          this.animationThread = new Thread(this);
  419.          this.animationThread.start();
  420.          Thread.currentThread().setPriority(10);
  421.       }
  422.  
  423.    }
  424.  
  425.    public void run() {
  426.       for(; this.animationThread != null; ((Component)this).repaint()) {
  427.          try {
  428.             Thread.sleep(400L);
  429.          } catch (InterruptedException var1) {
  430.          }
  431.       }
  432.  
  433.    }
  434.  
  435.    public void paint(Graphics g) {
  436.       if (this.offImage != null) {
  437.          g.drawImage(this.offImage, 0, 0, (ImageObserver)null);
  438.       }
  439.  
  440.    }
  441. }
  442.