Электронная библиотека книг Александра Фролова и Григория Фролова.
 
Библиотека
Братьев
Фроловых
Электронная библиотека книг Александра Фролова и Григория Фролова.
Библиотека системного программиста
Программирование на JAVA
ПК. Шаг за шагом
Другие книги
Восстановление данных
Антивирусная защита
Статьи для
программистов
Пользователю компьютера
[Назад]

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

Оглавление

Назад

// ==========================================
// btn.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.*;

public class btn 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;
  
  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, (C) A. 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);
      
      g.setColor(Color.blue);

      g.drawString(parmTitle, 
	x0 - nShadowValue, y0 - nShadowValue);
    }
  }
  
  // ===========================================
  // mouseDown
  // ===========================================
  public boolean mouseDown(
    Event ev, int x, int y)
  {
    if(bButtonUp)
    {
      bButtonUp = false;
      repaint();
      doButtonAction();
    }  
    
    return true;
  }

  // ===========================================
  // mouseUp
  // ===========================================
  public boolean mouseUp(
    Event ev, int x, int y)
  {
    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();
      }
    }
    return true;
  }
  
  // ===========================================
  // mouseExit
  // ===========================================
  public boolean mouseExit(
    Event ev, int x, int y)
  {
    if(bAnimate)
    {
      if(threadAnimate != null)
      {
        threadAnimate.stop();
	threadAnimate = null;
	clrTitleColor = Color.black;
      }
    }

    bButtonUp = true;
    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(parmURL);
      }
      catch (MalformedURLException e) { }

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

[Назад]


Создание интернет-магазинов: http://www.shop2you.ru/ © Александр Фролов, Григорий Фролов, 1991-2016