home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
lotus
/
eSuite.exe
/
eSuiteDPP
/
samples
/
weatherchart
/
Banner.java
< prev
next >
Wrap
Text File
|
1998-01-09
|
10KB
|
324 lines
//******************************************************************************
/* Banner.java
Description: Simple text-banner applet.
Author: F Salazar
Create: 30 July, 1997
Copyright (C) 1997, Lotus Development Corporation. All Rights Reserved.
*/
//******************************************************************************
/*******************************************************************************
Change History:
$Log: //reebok/xyzL/JavaComp/webpack/samples/weatherchart/Banner.java
Rev 1.4.2.1 09 Jan 1998 10:51:52 jdonohue
Change package name to samples.weatherchart
Rev 1.4 09 Sep 1997 09:48:20 fsalazar
Added back-, foreground color support.
Rev 1.3 05 Aug 1997 10:15:42 fsalazar
Use webpack.samples.weatherchart package name.
Rev 1.2 31 Jul 1997 11:48:48 fsalazar
Correct deprecated warnings, errors.
Rev 1.1 31 Jul 1997 09:50:22 fsalazar
Added PVCS log keyword.
*******************************************************************************/
package samples.weatherchart;
import java.applet.*;
import java.awt.*;
//==============================================================================
// Main Class for applet Banner
//
//==============================================================================
public class Banner extends Applet
{
// Font for drawing banner
private Font m_Font;
// PARAMETER SUPPORT:
// Parameters allow an HTML author to pass information to the applet;
// the HTML author specifies them using the <PARAM> tag within the <APPLET>
// tag. The following variables are used to store the values of the
// parameters.
//--------------------------------------------------------------------------
// Members for applet parameters
// <type> <MemberVar> = <Default Value>
//--------------------------------------------------------------------------
private String m_sFont = "Helvetica";
private int m_iSize = 18;
private boolean m_bBold = false;
private boolean m_bItalic = false;
private String m_sText = "[Banner text]";
private int m_iAlign = 0;
private String m_sBackColor = null;
private String m_sForeColor = null;
// Parameter names. To change a name of a parameter, you need only make
// a single change. Simply modify the value of the parameter string below.
//--------------------------------------------------------------------------
private final String PARAM_FONT = "FONT";
private final String PARAM_SIZE = "SIZE";
private final String PARAM_BOLD = "BOLD";
private final String PARAM_ITALIC = "ITALIC";
private final String PARAM_TEXT = "TEXT";
private final String PARAM_ALIGN = "ALIGN";
private final String PARAM_BACKGROUNDCOLOR = "BACKGROUNDCOLOR";
private final String PARAM_FOREGROUNDCOLOR = "FOREGROUNDCOLOR";
// Banner Class Constructor
//--------------------------------------------------------------------------
public Banner()
{
// TODO: Add constructor code here
}
// APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
//--------------------------------------------------------------------------
public String getAppletInfo()
{
return "Name: Banner\r\n" +
"Author: Lotus Development Corp.";
}
// PARAMETER SUPPORT
// The getParameterInfo() method returns an array of strings describing
// the parameters understood by this applet.
//
// Banner Parameter Information:
// { "Name", "Type", "Description" },
//--------------------------------------------------------------------------
public String[][] getParameterInfo()
{
String[][] info =
{
{ PARAM_FONT, "String", "Font-name for banner." },
{ PARAM_SIZE, "int", "Font-size for banner." },
{ PARAM_BOLD, "boolean", "Bold style." },
{ PARAM_ITALIC, "boolean", "Italic style." },
{ PARAM_TEXT, "String", "Display text." },
{ PARAM_ALIGN, "int", "Text alignment." },
{ PARAM_BACKGROUNDCOLOR, "String", "Background color" },
{ PARAM_FOREGROUNDCOLOR, "String", "Foreground color" }
};
return info;
}
// The init() method is called by the AWT when an applet is first loaded or
// reloaded. Override this method to perform whatever initialization your
// applet needs, such as initializing data structures, loading images or
// fonts, creating frame windows, setting the layout manager, or adding UI
// components.
//--------------------------------------------------------------------------
public void init()
{
// PARAMETER SUPPORT
// The following code retrieves the value of each parameter
// specified with the <PARAM> tag and stores it in a member
// variable.
//----------------------------------------------------------------------
String param;
// FONT: Font-name for banner.
//----------------------------------------------------------------------
param = getParameter(PARAM_FONT);
if (param != null)
m_sFont = param;
// SIZE: Font-size for banner.
//----------------------------------------------------------------------
param = getParameter(PARAM_SIZE);
if (param != null)
m_iSize = Integer.parseInt(param);
// BOLD: Bold style.
//----------------------------------------------------------------------
param = getParameter(PARAM_BOLD);
if (param != null)
m_bBold = Boolean.valueOf(param).booleanValue();
// ITALIC: Italic style.
//----------------------------------------------------------------------
param = getParameter(PARAM_ITALIC);
if (param != null)
m_bItalic = Boolean.valueOf(param).booleanValue();
// TEXT: Display text.
//----------------------------------------------------------------------
param = getParameter(PARAM_TEXT);
if (param != null)
m_sText = param;
// ALIGN: Text alignment.
//----------------------------------------------------------------------
param = getParameter(PARAM_ALIGN);
if (param != null)
m_iAlign = Integer.parseInt(param);
// BACKGROUNDCOLOR: Background color
//----------------------------------------------------------------------
param = getParameter(PARAM_BACKGROUNDCOLOR);
if (param != null)
m_sBackColor = param;
// FOREGROUNDCOLOR: Foreground color
//----------------------------------------------------------------------
param = getParameter(PARAM_FOREGROUNDCOLOR);
if (param != null)
m_sForeColor = param;
// TODO: Place additional initialization code here
setupDraw();
}
// Place additional applet clean up code here. destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
public void destroy()
{
// TODO: Place applet cleanup code here
}
// Banner Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
int x = 0, y = 0, sw = 0, sh = 0;
Dimension d = getSize();
FontMetrics m;
if ( null == m_Font )
return;
g.setFont( m_Font );
m = g.getFontMetrics();
sw = m.stringWidth( m_sText );
sh = m.getAscent();
// handle alignment
switch( m_iAlign )
{
case 0: // left
x = 0;
y = sh;
break;
case 1: // center
x = ( d.width / 2 ) - ( sw / 2 );
y = sh;
break;
case 2: // right
x = d.width - sw;
y = sh;
break;
}
g.drawString( m_sText, x, y );
}
// The start() method is called when the page containing the applet
// first appears on the screen. The AppletWizard's initial implementation
// of this method starts execution of the applet's thread.
//--------------------------------------------------------------------------
public void start()
{
// TODO: Place additional applet start code here
}
// The stop() method is called when the page containing the applet is
// no longer on the screen. The AppletWizard's initial implementation of
// this method stops execution of the applet's thread.
//--------------------------------------------------------------------------
public void stop()
{
}
protected Color colorFromString( String s )
{
String sHexFmt = new String( "0x" );
String lStr, cStr;
Color c = null;
Integer i = null;
int idx, radix = 10;
if ( null == s )
return null;
lStr = s.toLowerCase();
c = Color.getColor( lStr );
if ( c != null )
return c;
try
{
idx = lStr.indexOf( sHexFmt );
if ( idx >= 0 )
{
lStr = lStr.substring( idx + sHexFmt.length() );
radix = 16;
}
i = Integer.valueOf( lStr, radix );
if ( null != i )
c = new Color( i.intValue() );
}
catch( Exception e )
{
c = null;
}
return c;
}
// setupDraw : Create needed font for text
protected void setupDraw()
{
Color c = null;
int style = Font.PLAIN;
if ( m_bBold ) style += Font.BOLD;
if ( m_bItalic ) style += Font.ITALIC;
m_Font = new Font( m_sFont, style, m_iSize );
c = colorFromString( m_sBackColor );
if ( c != null )
setBackground( c );
c = colorFromString( m_sForeColor );
if ( c != null )
setForeground( c );
}
public void setText( String s )
{
m_sText = s;
repaint();
}
}