home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / dance / classes / button.jav < prev    next >
Encoding:
Text File  |  1995-09-14  |  13.2 KB  |  605 lines

  1. /*-
  2.  * Copyright (c) 1995 by Georg Hessmann.
  3.  * All Right Reserved.
  4.  *
  5.  * Button.java    1.0f 25.08f.95
  6.  *
  7.  */
  8.  
  9.  
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.Font;
  13. import java.awt.Color;
  14.  
  15. import MEXButtonList;
  16.  
  17. /**
  18.  * Button is the main class for creating buttons.
  19.  *
  20.  * @see ToggleButton
  21.  * @see MEXButton
  22.  * @see MEXButtonList
  23.  *
  24.  * @version 1.0f 25 Aug 1995
  25.  * @author Georg Heßmann
  26.  */
  27.  
  28. class Button {
  29.  
  30.   /**
  31.    * Static pointer to the main applet. Has to be set before
  32.    * the first use of a button.
  33.    */
  34.   public static Dance app = null;
  35.   
  36.   static Button    BLst = null;
  37.  
  38.   Button        Next = null;
  39.  
  40.   int bX, bY, bW, bH;
  41.  
  42.   Font          BFont;
  43.   String        Label;
  44.   int           LabelWidth;
  45.  
  46.   int           BNumber;
  47.  
  48.   boolean       Selected  = false;    // mouseDown bekommen
  49.   boolean    isActive  = false;    // bleibt auch bei mouseDrag true
  50.   boolean    Changed   = true;
  51.  
  52.  
  53.   /**
  54.    * Creates a button.
  55.    * @param bnum id-number for the button
  56.    * @param l label
  57.    * @param x x-ccordinate of the button
  58.    * @param y y-coordinate of the button
  59.    * @param w the size().width
  60.    * @param h the size().height
  61.    * @param f the font used for the label
  62.    */
  63.   public Button(int bnum, String l, int x, int y, int w, int h, Font f)
  64.   {
  65.     Label  = l;
  66.     BFont  = f;
  67.     bX = x;
  68.     bY = y;
  69.     bW = w;
  70.     bH = h;
  71.  
  72.     BNumber = bnum;
  73.  
  74.     Next   = BLst;
  75.     BLst   = this;
  76.  
  77.     LabelWidth = -1; 
  78.   }
  79.  
  80.   /**
  81.    * Deletes all buttons.
  82.    */
  83.   public static void freeAll()
  84.   {
  85.     BLst = null;
  86.   }
  87.  
  88.   /**
  89.    * Redraw all buttons.
  90.    */
  91.   static public void drawAll(Graphics g)
  92.   {
  93.     Button lst = BLst;
  94.  
  95.     while (lst != null) {
  96.       lst.drawButton(g);
  97.       lst = lst.Next;
  98.     }
  99.   }
  100.  
  101.  
  102.   /**
  103.    * Redraw only these buttons, which are changed since the last
  104.    * redraw.
  105.    */
  106.   static public void drawChanged(Graphics g)
  107.   {
  108.     Button lst = BLst;
  109.  
  110.     while (lst != null) {
  111.       if (lst.Changed) {
  112.     lst.drawButton(g);
  113.     lst.Changed = false;
  114.       }
  115.       lst = lst.Next;
  116.     }
  117.   }
  118.  
  119.  
  120.   /**
  121.    * Handels a mouse click. Normaly called from the applet.
  122.    * If something has changed, java.applet.Applet.repaint() will be called.
  123.    */
  124.   static public boolean mouseDown(int x, int y)
  125.   {
  126.     boolean changed = false;
  127.     Button lst = BLst;
  128.  
  129.     while (lst != null) {
  130.       boolean b = lst.mouseDownButton(x, y);
  131.       changed = changed || b;
  132.       lst = lst.Next; 
  133.     }
  134.  
  135.     if (changed) app.repaint();
  136.     
  137.     return false;
  138.   }
  139.  
  140.   /**
  141.    * Handels a mouse up event. Normaly called from the applet.
  142.    * If something has changed java.applet.Applet.repaint() will be called.
  143.    * If the mouse down and the mouse up event was over the
  144.    * same button, the action function of this button will be called.
  145.    */
  146.   static public boolean mouseUp(int x, int y)
  147.   {
  148.     boolean changed = false;
  149.     Button lst = BLst;
  150.  
  151.     while (lst != null) {
  152.       boolean b = lst.mouseUpButton(x, y);
  153.       changed = changed || b;
  154.       lst = lst.Next;
  155.     }
  156.  
  157.     if (changed) app.repaint();
  158.  
  159.     return false;
  160.   }
  161.  
  162.   /**
  163.    * Handels a mouse drag event. Normaly called from the applet.
  164.    * If something has changed java.applet.Applet.repaint() will be called.
  165.    */
  166.   static public boolean mouseDrag(int x, int y)
  167.   {
  168.     boolean changed = false;
  169.     Button lst = BLst;
  170.  
  171.     while (lst != null) {
  172.       boolean b = lst.mouseDragButton(x, y);
  173.       changed = changed || b;
  174.       lst = lst.Next;
  175.     }
  176.  
  177.     if (changed) app.repaint();
  178.  
  179.     return false;
  180.   }
  181.  
  182.   /**
  183.    * Gets the id-number of this button.
  184.    * @return id-number of this button.
  185.    */
  186.   public int GetNum()
  187.   {
  188.     return BNumber;
  189.   }
  190.  
  191.   protected void drawButton(Graphics g)
  192.   {
  193.     if (LabelWidth == -1) {
  194.       LabelWidth = g.getFontMetrics(BFont).stringWidth(Label);
  195.     }
  196.     
  197.     Font oldf   = g.getFont();
  198.     Color fgCol = g.getColor();
  199.  
  200.     g.setFont(BFont);
  201.     g.setColor(Color.lightGray);
  202.     
  203.     if (Selected) {
  204.       g.fill3DRect(bX, bY, bW, bH, false);
  205.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, false);
  206.       g.setColor(Color.black);
  207.       g.drawString(Label, bX + (bW - LabelWidth)/2 + 1, bY + BFont.getSize() + 2 + 1);
  208.     }
  209.     else {
  210.       g.fill3DRect(bX, bY, bW, bH, true);
  211.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, true);
  212.       g.setColor(Color.black);
  213.       g.drawString(Label, bX + (bW - LabelWidth)/2, bY + BFont.getSize() + 2);
  214.     }
  215.     
  216.     g.setFont(oldf);
  217.     g.setColor(fgCol);
  218.   }
  219.  
  220.  
  221.   private boolean mouseDownButton(int x, int y)
  222.   {
  223.     if (x >= bX && x < bX+bW && y >= bY && y < bY+bH) {
  224.       Selected = true;
  225.       isActive = true;
  226.       Changed  = true;
  227.     }
  228.  
  229.     return Changed;
  230.   }
  231.  
  232.  
  233.   protected boolean mouseUpButton(int x, int y)
  234.   {
  235.     if (x >= bX && x < bX+bW && y >= bY && y < bY+bH) {
  236.       if (Selected) {
  237.     Selected = false;
  238.     Changed  = true;
  239.     action();
  240.       }
  241.     }
  242.     else {
  243.       if (Selected) {
  244.     Selected = false;
  245.     Changed  = true;
  246.       }
  247.     }
  248.     isActive = false;
  249.  
  250.     return Changed;
  251.   }
  252.  
  253.  
  254.   private boolean mouseDragButton(int x, int y)
  255.   {
  256.     if (x >= bX && x < bX+bW && y >= bY && y < bY+bH) {
  257.       if (isActive && !Selected) {
  258.     Selected = true;
  259.     Changed  = true;
  260.       }
  261.     }
  262.     else {
  263.       if (Selected) {
  264.     Selected = false;
  265.     Changed  = true;
  266.       }
  267.     }
  268.  
  269.     return Changed;
  270.   }
  271.  
  272.   /**
  273.    * Main scheduling function. Will be called if an button
  274.    * is correctly pressed (mouse down and up).
  275.    */
  276.   protected void action()
  277.   {
  278.     switch (BNumber) {
  279.      case app.BslowW:
  280.      case app.Btango:
  281.      case app.BslowF:
  282.      case app.Bquick:
  283.       app.actionDanceButton(BNumber);
  284.       break;
  285.      case app.Bshow:
  286.       app.actionShowButton();
  287.       break;
  288.      case app.Binfo:
  289.       app.actionInfoButton();
  290.       break;
  291.      case app.Bpmusic:
  292.       app.actionPlayMusicButton();
  293.       break;
  294.      case app.Bmhelp:
  295.       app.actionMainHelpButton();
  296.       break;
  297.      case app.Bahelp:
  298.       app.actionAnimationHelpButton();
  299.       break;
  300.      case app.Bstep:
  301.       app.actionStepButton();
  302.       break;
  303.      case app.Bback:
  304.       app.actionBackStepButton();
  305.       break;
  306.      case app.Bplay:
  307.       app.actionPlayButton();
  308.       break;
  309.      case app.Bmenu:
  310.       app.actionMenuButton();
  311.       break;
  312.      case app.Bsound:
  313.       app.actionSoundButton(((ToggleButton)this).GetChoosed());
  314.       break;
  315.      case app.Bcomm:
  316.       app.actionCommentButton(((ToggleButton)this).GetChoosed());
  317.       break;
  318.      default:
  319.       if (BNumber >= app.Bfigur0 && BNumber < app.Bfigur0+10) {
  320.     app.actionFigurButton(BNumber);
  321.       }
  322.       else if (BNumber >= app.Bmusic0 && BNumber < app.Bmusic0+10) {
  323.     app.actionMusicButton(BNumber);
  324.       }
  325.       break;
  326.     }
  327.   }
  328. }
  329.  
  330.  
  331. /**
  332.  * MEXButton is the class of mutual exclusive buttons.
  333.  *
  334.  * @see MEXButtonList
  335.  *
  336.  * @version 1.0f 25 Aug 1995
  337.  * @author Georg Heßmann
  338.  */
  339.  
  340. class MEXButton extends Button {
  341.   MEXButtonList    Father;
  342.   boolean    choosed;
  343.   MEXButton    MEXList;
  344.   Font        choosedFont;
  345.   int          choosedLabelWidth;
  346.  
  347.   /**
  348.    * Creates an mutual exclusive button. Will be called only
  349.    * from within the MEXButtonList class.
  350.    *
  351.    * @param father MEXButtonList head
  352.    * @param brother previsious MEXButton
  353.    * @param bnum id-number for the button
  354.    * @param l label
  355.    * @param x x-ccordinate of the button
  356.    * @param y y-coordinate of the button
  357.    * @param w the size().width
  358.    * @param h the size().height
  359.    * @param f the font used for the label
  360.    * @param cfnt the font used for the selected label
  361.    */
  362.   public MEXButton(MEXButtonList father, MEXButton brother,
  363.            int bnum, String l, int x, int y, int w, int h, Font f,
  364.            Font cfnt)
  365.   {
  366.     super(bnum, l, x, y, w, h, f);
  367.  
  368.     Father  = father;
  369.     choosed = false;        // wird erst nachtraeglich gesetzt
  370.     MEXList = brother;
  371.     choosedFont = cfnt;
  372.     
  373.     choosedLabelWidth = -1;
  374.   }
  375.  
  376.   /**
  377.    * Set this MEXButton to the selected button.
  378.    * Will be called only via class MEXButtonList.
  379.    * Don't call this function directly.
  380.    */
  381.   public void SetChoosed()
  382.   {
  383.     if (!choosed) {
  384.       Changed = true;
  385.       choosed = true;
  386.     }
  387.   }
  388.   
  389.   /**
  390.    * Set this MEXButton to an unselected button.
  391.    * Will be called only via class MEXButtonList.
  392.    * Don't call this function directly.
  393.    */
  394.   public void ClearChoosed()
  395.   {
  396.     if (choosed) {
  397.       Changed = true;
  398.       choosed = false;
  399.     }
  400.   }
  401.  
  402.   /**
  403.    * Is this button the selected button?
  404.    * @return state of this button
  405.    */
  406.   public boolean IsChoosed()    { return choosed;  }
  407.  
  408.   /**
  409.    * Get the next MEXButton.
  410.    * Will be called only via class MEXButtonList.
  411.    * Don't call this function directly.
  412.    * @return next MEXButton in the internal list.
  413.    */
  414.   public MEXButton NextMEX()    { return MEXList;  }
  415.  
  416.  
  417.   protected void drawButton(Graphics g)
  418.   {
  419.     if (LabelWidth == -1) {
  420.       LabelWidth = g.getFontMetrics(BFont).stringWidth(Label);
  421.     }
  422.     if (choosedLabelWidth == -1) {
  423.       choosedLabelWidth = g.getFontMetrics(choosedFont).stringWidth(Label);
  424.     }
  425.     
  426.     Font oldf   = g.getFont();
  427.     Color fgCol = g.getColor();
  428.  
  429.     int    labw;
  430.  
  431.     if (choosed) {
  432.       g.setFont(choosedFont);
  433.       labw = choosedLabelWidth;
  434.     }
  435.     else {
  436.       g.setFont(BFont);
  437.       labw = LabelWidth; 
  438.     }
  439.  
  440.     if (choosed) g.setFont(choosedFont);
  441.     else     g.setFont(BFont);
  442.       
  443.     g.setColor(Color.lightGray);
  444.     
  445.     if (Selected) {
  446.       g.fill3DRect(bX, bY, bW, bH, false);
  447.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, false);
  448.       g.setColor(Color.black);
  449.       g.drawString(Label, bX + (bW - labw)/2 + 1, bY + BFont.getSize() + 2 + 1);
  450.     }
  451.     else {
  452.       g.fill3DRect(bX, bY, bW, bH, true);
  453.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, true);
  454.       g.setColor(Color.black);
  455.       g.drawString(Label, bX + (bW - labw)/2, bY + BFont.getSize() + 2);
  456.     }
  457.     
  458.     g.setFont(oldf);
  459.     g.setColor(fgCol);
  460.   }
  461.  
  462.  
  463.   protected boolean mouseUpButton(int x, int y)
  464.   {
  465.     if (x >= bX && x < bX+bW && y >= bY && y < bY+bH) {
  466.       if (Selected) {
  467.     Selected = false;
  468.     Changed  = true;
  469.     boolean dummy = Father.SetChoosedNum(BNumber);
  470.     action();
  471.       }
  472.     }
  473.     else {
  474.       if (Selected) {
  475.     Selected = false;
  476.     Changed  = true;
  477.       }
  478.     }
  479.     isActive = false;
  480.  
  481.     return Changed;
  482.   }
  483.  
  484. }
  485.  
  486.  
  487. /**
  488.  * ToggleButton is the class for buttons with to states.
  489.  *
  490.  * @version 1.0f 25 Aug 1995
  491.  * @author Georg Heßmann
  492.  */
  493.  
  494. class ToggleButton extends Button {
  495.   
  496.   boolean choosed;
  497.   String  choosedLabel;
  498.   int      choosedLabelWidth;
  499.   Font    choosedFont;
  500.  
  501.   /**
  502.    * Creates a toggle button.
  503.    * This button has two states represended by two labels and two fonts.
  504.    *
  505.    * @param bnum id-number for the button
  506.    * @param l label for state one
  507.    * @param cl label for state two
  508.    * @param x x-ccordinate of the button
  509.    * @param y y-coordinate of the button
  510.    * @param w the size().width
  511.    * @param h the size().height
  512.    * @param f the font used for the label in state one
  513.    * @param cf the font used for the label in state two
  514.    * @param cho initialize the ToggleButton in state two?
  515.    */
  516.   public ToggleButton(int bnum, String l, String cl, int x, int y, int w, int h,
  517.               Font f, Font cf, boolean cho)
  518.   {
  519.     super(bnum, l, x, y, w, h, f);
  520.  
  521.     choosed      = cho;
  522.     choosedLabel = cl;
  523.     choosedFont  = cf;
  524.     
  525.     choosedLabelWidth = -1;
  526.   }
  527.  
  528.   /**
  529.    * Returns the state of the button.
  530.    * @return is the button in state two?
  531.    */
  532.   public boolean GetChoosed()
  533.   {
  534.     return choosed;
  535.   }
  536.  
  537.   protected void drawButton(Graphics g)
  538.   {
  539.     if (LabelWidth == -1) {
  540.       LabelWidth = g.getFontMetrics(BFont).stringWidth(Label);
  541.     }
  542.     if (choosedLabelWidth == -1) {
  543.       choosedLabelWidth = g.getFontMetrics(choosedFont).stringWidth(choosedLabel);
  544.     }
  545.  
  546.     Font oldf   = g.getFont();
  547.     Color fgCol = g.getColor();
  548.  
  549.     int    labw;
  550.     String lab;
  551.  
  552.     if (choosed) {
  553.       g.setFont(choosedFont);
  554.       lab  = choosedLabel;
  555.       labw = choosedLabelWidth;
  556.     }
  557.     else {
  558.       g.setFont(BFont);
  559.       lab  = Label;
  560.       labw = LabelWidth;
  561.     }
  562.       
  563.     g.setColor(Color.lightGray);
  564.     
  565.     if (Selected) {
  566.       g.fill3DRect(bX, bY, bW, bH, false);
  567.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, false);
  568.       g.setColor(Color.black);
  569.       g.drawString(lab, bX + (bW - labw)/2 + 1,
  570.            bY + BFont.getSize() + 2 + 1);
  571.     }
  572.     else {
  573.       g.fill3DRect(bX, bY, bW, bH, true);
  574.       if (app.fatBorder) g.draw3DRect(bX+1, bY+1, bW-2, bH-2, true);
  575.       g.setColor(Color.black);
  576.       g.drawString(lab, bX + (bW - labw)/2, bY + BFont.getSize() + 2);
  577.     }
  578.     
  579.     g.setFont(oldf);
  580.     g.setColor(fgCol);
  581.   }
  582.  
  583.  
  584.   protected boolean mouseUpButton(int x, int y)
  585.   {
  586.     if (x >= bX && x < bX+bW && y >= bY && y < bY+bH) {
  587.       if (Selected) {
  588.     choosed  = !choosed;
  589.     Selected = false;
  590.     Changed  = true;
  591.     action();
  592.       }
  593.     }
  594.     else {
  595.       if (Selected) {
  596.     Selected = false;
  597.     Changed  = true;
  598.       }
  599.     }
  600.     isActive = false;
  601.  
  602.     return Changed;
  603.   }
  604. }
  605.