Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы RadioAppButton.java

Оглавление

Назад

// ==========================================
// RadioAppButton.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;

public class RadioAppButton extends Applet 
  implements Runnable
{
  private String parmWidth    = "80";
  private String parmHeight   = "20";
  private String parmImageUp  = "btnup.gif";
  private String parmImageDn  = "btndn.gif";
  private String parmAnimate  = "true";
  private String parmTitle    = "Button";
  private String parmShadowValue  = "1";
  private String parmTitleFont    = "Helvetica";
  private String parmTitleFontSize = "12";
  private String parmTitleBold    = "true";
  private String parmTitleItalic  = "false";
  private String parmURL  = null;
  private String parmURLWindow  = "_self";
  
  int nWidth   = 80;
  int nHeight  = 20;
  boolean bAnimate = true;
  Image imgButtonUp;
  Image imgButtonDn;
  int nShadowValue;
  boolean bTitileBold;
  boolean bTitileItalic;
  
  Dimension dimAppletWindow;
  int nWindowWidth;
  int nWindowHeight;
  int nTitleFontSize;
  
  boolean bButtonUp = true;
  boolean bStartAnimation = false;
  
  Thread threadAnimate = null;
  Color clrTitleColor = Color.black;
  
  private final String 
    name_parmWidth    = "Width";
  private final String 
    name_parmHeight   = "Height";
  private final String 
    name_parmImageUp  = "ImageUp";
  private final String 
    name_parmImageDn  = "ImageDn";
  private final String 
    name_parmAnimate  = "Animate";
  private final String 
    name_parmTitle    = "Title";
  private final String 
    name_parmShadowValue  = "ShadowValue";
  private final String 
    name_parmTitleFont  = "TitleFont";
  private final String 
    name_TitleFontSize  = "TitleFontSize";
  private final String 
    name_TitleBold      = "TitleBold";
  private final String 
    name_TitleItalic    = "TitleItalic";
  private final String 
    name_URL        = "URL";
  private final String 
    name_URLWindow  = "URLWindow";

  // ===========================================
  // getParameterInfo
  // ===========================================
  public String[][] getParameterInfo()
  {
    String[][] paramInfo = 
    {
      {
	name_parmWidth, 
	  "String", "Button width"
      },
      {
	name_parmHeight,  
	  "String", "Button height"
      },
      {
	name_parmImageUp, 
	  "String", "Image when button is down"
      },
      {
	name_parmImageDn, 
	  "String", "Image when button is up"
      },
      {
	name_parmAnimate, 
	  "String", "Animation used"
      },
      {
	name_parmTitle, 
	  "String", "Button title"
      },
      {
	name_parmShadowValue, 
	  "String", "Shadow value in pixels"
      },
      {
	name_parmTitleFont, 
	  "String", "Title font"
      },
      {
	name_TitleFontSize, 
	  "String", "Title font size"
      },
      {
	name_TitleBold, 
	  "String", "Title font is bold"
      },
      {
	name_TitleItalic, 
	  "String", "Title font is italic"
      },
      {
	name_URL, 
	  "String", "URL link"
      },
      {
	name_URLWindow, 
	  "String", "URL window"
      }
    };
    
    return paramInfo;
  }
      
  // ===========================================
  // init
  // ===========================================
  public void init()
  {
    String parm;
    
    parm = getParameter(name_parmWidth);
    if(parm != null)
      parmWidth = parm;
  
    parm = getParameter(name_parmHeight);
    if(parm != null)
      parmHeight = parm;
      
    parm = getParameter(name_parmImageUp);
    if(parm != null)
      parmImageUp = parm;
      
    parm = getParameter(name_parmImageDn);
    if(parm != null)
      parmImageDn = parm;
      
    parm = getParameter(name_parmAnimate);
    if(parm != null)
      parmAnimate = parm;
      
    parm = getParameter(name_parmTitle);
    if(parm != null)
      parmTitle = parm;
      
    parm = getParameter(name_parmShadowValue);
    if(parm != null)
      parmShadowValue = parm;
      
    parm = getParameter(name_parmTitleFont);
    if(parm != null)
      parmTitleFont = parm;
      
    parm = getParameter(name_TitleFontSize);
    if(parm != null)
      parmTitleFontSize = parm;
      
    parm = getParameter(name_TitleBold);
    if(parm != null)
      parmTitleBold = parm;
      
    parm = getParameter(name_TitleItalic);
    if(parm != null)
      parmTitleItalic = parm;
      
    parm = getParameter(name_URL);
    if(parm != null)
      parmURL = parm;
      
    parm = getParameter(name_URLWindow);
    if(parm != null)
      parmURLWindow = parm;
      
    try
    {  
      Integer intVal = new Integer(parmWidth);
      nWidth = intVal.intValue();
    }
    catch(Exception ex) {}

    try
    {  
      Integer intVal = new Integer(parmHeight);
      nHeight = intVal.intValue();
    }
    catch(Exception ex) {}

    try
    {  
      Integer intVal = 
        new Integer(parmShadowValue);
      nShadowValue = intVal.intValue();
    }
    catch(Exception ex) {}
    
    if(parmAnimate.equals("true"))
      bAnimate = true;
    else
      bAnimate = false;
      
    if(parmTitleBold.equals("true"))
      bTitileBold = true;
      
    if(parmTitleItalic.equals("true"))
      bTitileItalic = true;
      
    try
    {  
      Integer intVal = 
	new Integer(parmTitleFontSize);
      nTitleFontSize = intVal.intValue();
    }
    catch(Exception ex) {}
      
      
    imgButtonUp = 
      getImage(getCodeBase(), parmImageUp);
    imgButtonDn = 
      getImage(getCodeBase(), parmImageDn);
    
    dimAppletWindow = size();
    nWindowWidth = dimAppletWindow.width;
    nWindowHeight = dimAppletWindow.height;
  }

  // ===========================================
  // getAppletInfo
  // ===========================================
  public String getAppletInfo()
  {
    return(
      "Name: Rich Button-2, " + 
      "(C) Alexandr Frolov, 1998");
  }

  // ===========================================
  // paint
  // ===========================================
  public void paint(Graphics g)
  {
    int nFontStyle = Font.PLAIN;
    
    if(bTitileBold)
      nFontStyle |= Font.BOLD;
     
    if(bTitileItalic)
      nFontStyle |= Font.ITALIC;
    
    g.setFont(new Font(parmTitleFont, 
      nFontStyle, nTitleFontSize));
    
    FontMetrics fm = g.getFontMetrics();
    int nTitileWidth = fm.stringWidth(parmTitle);
    int nTitleHeight = fm.getAscent() - 
      fm.getLeading() - fm.getDescent();
    
    int x0 = (nWindowWidth - nTitileWidth) / 2;
    int y0 = 
      ((nWindowHeight - nTitleHeight) / 2) +
       nTitleHeight;
    
    if(bButtonUp)
    {
      g.drawImage(imgButtonUp, 0, 0, this);
      
      g.setColor(clrTitleColor);
      g.drawString(parmTitle, x0, y0);
    }
    else
    {
      g.drawImage(imgButtonDn, 0, 0, this);
      
      if(bStartAnimation == false)
      {
        g.setColor(clrTitleColor);
      }
      else
        g.setColor(Color.blue);

      g.drawString(parmTitle, 
	x0 - nShadowValue, y0 - nShadowValue);
    }
  }
  
  // ===========================================
  // mouseDown
  // ===========================================
  public boolean mouseDown(
    Event ev, int x, int y)
  {
    AppletContext ac;
    Enumeration enApplets;
    Applet ap;
    ac = getAppletContext();
    enApplets = ac.getApplets();
    
    while(enApplets.hasMoreElements())
    {
      ap = (Applet)(enApplets.nextElement());
      if(ap != this)
      {
	((RadioAppButton)ap).popButton();
      }
    }
  
    if(bButtonUp)
    {
      bButtonUp = false;
      repaint();
      doButtonAction();
    }  
    
    return true;
  }
  
  // ===========================================
  // popButton
  // ===========================================
  public boolean popButton()
  {
    if(!bButtonUp)
    {
      bButtonUp = true;
      repaint();
    }  
    return true;
  }

  // ===========================================
  // mouseEnter
  // ===========================================
  public boolean mouseEnter(
    Event ev, int x, int y)
  {
    if(bAnimate)
    {
      if(threadAnimate == null)
      {
	threadAnimate = new Thread(this);
	threadAnimate.start();
      }
      bStartAnimation = true;
    }
    return true;
  }
  
  // ===========================================
  // mouseExit
  // ===========================================
  public boolean mouseExit(
    Event ev, int x, int y)
  {
    if(bAnimate)
    {
      if(threadAnimate != null)
      {
        threadAnimate.stop();
	threadAnimate = null;
	clrTitleColor = Color.black;
      }
    }
    
    bStartAnimation = false;
    repaint();
    
    return true;
  }

  // ===========================================
  // stop
  // ===========================================
  public void stop()
  {
    if(bAnimate)
    {
      if(threadAnimate != null)
      {
        threadAnimate.stop();
	threadAnimate = null;
	clrTitleColor = Color.black;
      }
    }
  }

  // ===========================================
  // run
  // ===========================================
  public void run()
  {
    boolean bFlipFlop = true;
    
    while(true)
    {
      if(bFlipFlop)
      {
        clrTitleColor = Color.red;
	bFlipFlop = false;
      }
      else
      {
	clrTitleColor = Color.black;
        bFlipFlop = true;
      }
      
      repaint();
    
      try
      {
	Thread.sleep(300);
      }
      catch (InterruptedException ex)
      {
        stop();
      }
    }
  }

  // ===========================================
  // doButtonAction
  // ===========================================
  void doButtonAction()
  {
    if(parmURL != null)
    {
      URL url = null;
      AppletContext appletContext;
      appletContext = getAppletContext();

      try
      {
        url = new URL(
	  getCodeBase().toExternalForm() +
	     "/" + parmURL);
      }
      catch (MalformedURLException e) { }

      if (url != null)
      {
        appletContext.showDocument(
	  url, parmURLWindow);
      }
    }
  }
}

[Назад]