📄 hint_map.java
字号:
// hint_map.java
//
// Description
// ===========
//
// A Java class which implements image map like functionality, and
// displays a Win95 style help text for each area.
//
// Also provides the facility to load images (background, foreground), so that
// we have the appearance of transparency.
//
// Developed as a replacement for multiple copies of pbimg.java, which
// was becoming a piece of spahgetti, and had perfromnce problems if used
// multiple times within the same page to achieve a image map effect.
//
//
//
// Authors Name email
// ======= ==== ======
// 04-Feb-1996/SBH Steve Hill 100144.3120@compuserve.com
//
// Revision History
// ================
// 04-Feb-1996/SBH Created. Based on methods used in pbimg.java
//
// 13-Feb-1996/SBH First released version, as unregistered shareware.
// (1.0) Placed in CIS:JAVAUSER
//
// 21-Feb-1996/SBH Added (optional) support for Netscape frames. Specifed by
// (1.2) names as the last parameter for a hot area.
// (NOTE: v1.1a was a 'special', and not generally
// released.
//
// Fixed bug whereby hint not displayed following call to
// paint() if not caused by mouse movement - moved
// repaint of hint to CopyOfftoOn()
//
// Incorporated workaround for problem in Netscape
// where HTML background loaded over a cached applet
// (just redraw 0.75 seconds after load)
//
// 27-Feb-1996/SBH Fixed bug whereby after numerous reloads of the same
// (1.21) applet, mouseDown messages were not delivered until
// the mouse moved within the hot_area (fix was to
// destry the timer thread when stop() called.
//-------------------------------------------------------------------------
import java.awt.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;
class hot_area
// hot_area is a class which represents a polgon on screen, within the area
// of it's parent applet. The user interacts with hot areas as follows:
//
// o A Mouseclick will result in the URL being actioned, if valid
// o The mouse entering the area will result in a hint being displayed
//
//
//
{
private Polygon polyMyArea = new Polygon(); // The area covered
private hint_map Parent = null; // My Parent applet
private String szHint; // The hint text to display
private String szURLDescription; // Description of my URL
private URL uURL; // The URL to action
private boolean bHintVisible = false; // When last called, was I visible?
private boolean bUseFrame = false; // 21-Feb-96/SBH Load into Netscape Frame?
private String szFrame; // 21-Feb-96/SBH The name of Netscape frame into which to load.
public boolean bInitialised = false; // Everything ok.
public hot_area ()
{
}
public hot_area (String _szConfiguration, hint_map _Parent)
// Description
// ===========
//
// This takes a string of the format:
//
// "URL,Hint,X1,Y1,X2,Y2,.....Xn,Yn"
//
// and from it initialises the hot area.
{
int iX1 = 0;
int iX2 = 0;
int iX3 = 0;
int iY1 = 0;
int iY2 = 0;
int iY3 = 0;
String szTemp=""; //21-Feb-96/SBH Added to hold last param fetched
boolean bRectangle = false; //(if only two points, we assume a rectangle)
//debug ("New hot_area :" + _szConfiguration );
//keep a track of our parent..
Parent = _Parent;
StringTokenizer st = new StringTokenizer(_szConfiguration, ",");
try
{
szURLDescription = st.nextToken();
szHint = st.nextToken();
}
catch (Exception e)
{
szHint = "Unable to determine URL" ;
}
try
{
uURL = new URL(Parent.getDocumentBase(),szURLDescription);
}
catch (MalformedURLException e)
{
uURL = null;
}
if (null != uURL)
szURLDescription = uURL.toString();
else
szURLDescription = "Invalid URL";
if (szURLDescription != null)
{
// We now try and work out our area.
// There must be at least 4 numbers (x1,y1,x2,y2).
try
{
iX1 = Integer.parseInt(st.nextToken());
iY1 = Integer.parseInt(st.nextToken());
iX2 = Integer.parseInt(st.nextToken());
iY2 = Integer.parseInt(st.nextToken());
}
catch (Exception e)
{
szURLDescription = null;
debug("Error: At least two points must be specified for hot_area '"+
szHint + "'");
}
//debug(szHint +":" + iX1 + iY1 + iX2 + iY2);
// Now, if there are no more points configured we have a rectangle
// (we consider two points to be a rectangle)
try
{
iX3 = Integer.parseInt(szTemp = st.nextToken());
iY3 = Integer.parseInt(st.nextToken());
}
catch (Exception e)
{
bRectangle = true;
}
if (bRectangle)
{
polyMyArea.addPoint(iX1,iY1);
polyMyArea.addPoint(iX2,iY1);
polyMyArea.addPoint(iX2,iY2);
polyMyArea.addPoint(iX1,iY2);
// debug (szHint +":Configured as rectangle");
bInitialised = true;
}
else
{
// debug (szHint+ ": Polygon specified");
polyMyArea.addPoint(iX1,iY1);
polyMyArea.addPoint(iX2,iY2);
polyMyArea.addPoint(iX3,iY3);
while (!bInitialised)
{
try
{
iX1 = Integer.parseInt(szTemp=st.nextToken());
iY1 = Integer.parseInt(st.nextToken());
polyMyArea.addPoint(iX1,iY1);
}
catch (Exception e)
{
bInitialised = true;
}
}
}
if (!szTemp.equals(""))
{
szFrame = szTemp;
bUseFrame = true;
}
}
}
//--------------------------------------------------------------------------
public void MouseMove(int x, int y)
// Description
// ===========
//
// x and y are the absolute parameters of the cursor within the
// applet. We check to see if they are inside us, and if so we should
// display both a hint and a status.
//
//
{
if (polyMyArea.inside(x,y))
{
//Show our URL on the status line
Parent.setStatusText(szURLDescription,true);
Parent.setNewHintText(szHint);
if (!bHintVisible)
{
//debug(szHint+": Mouse Entered");
bHintVisible = true;
}
else
{
}
}
else
{
//Hide our URL, if on the status line
Parent.setStatusText(szURLDescription,false);
if (bHintVisible)
{
//debug(szHint+": Mouse Left");
bHintVisible = false;
}
else
{
}
}
}//MouseMove
//--------------------------------------------------------------------------
public void MouseUp(int x, int y)
// Description
// ===========
//
// x and y are the absolute parameters of the cursor within the
// applet. A mouse button has just been released. If it is in here
// then do the jump
//
{
// Check if inside, and hint visible. This means that the mouse went
// down in this area as well.
if (polyMyArea.inside(x,y) && bHintVisible)
{
if (bUseFrame)
{
Parent.getAppletContext().showDocument(uURL,szFrame);
}
else
{
Parent.getAppletContext().showDocument(uURL);
}
Parent.showStatus("Fetching " + szURLDescription);
}
} //MouseUp
//--------------------------------------------------------------------------
private void debug(String szText)
{
Parent.debug(szText);
}
}
//========================================================================
public class hint_map extends java.applet.Applet implements Runnable {
public String szVersion = "hint_map.class v1.21 (UNREGISTERED) (c) Steve Hill 100144.3120@compuserve.com";
Thread timer;
Image imgStaticOffScreen = null; // Holds a copy of foreground over background
Image imgBackground = null; // Used to load background
Image imgForeground = null; // Used to load foreground
Graphics gcOff = null; // Graphics context to draw on imgStaticOffScreen
Graphics gcOn = null; // Graphics context to draw on visible screen.
String szBackgroundFile; // URL of background as string
String szForegroundFile; // URL of foreground as string
int iOutstandingImageCount = 0; //Count of images not yet fully drawn offscreen.
boolean bBackgroundOK = false; // Background file exists
boolean bForegroundOK = false; // Foreground file exists
Color colBackground; // Color of background (under backgroundimage)
Dimension dimOffScreen; //size of offscreen image
boolean bInitComplete = false; // Interlock between paint() and run().
hot_area HotAreas[] = new hot_area[20]; //array of all hot areas
int iMaxHotArea = 0; // offset of last member of HotAreas
String szLastStatus = ""; //Last vlaue shown on status bar
boolean bStatusChanged = false; //New status bar value
String szHintText = "";
boolean bHintVisible = false;
boolean bHintChanged = false;
int lx,ly,lw,lh; // keep a copy of location of hint
// in case we need to redraw during image fetch
public void init()
{
System.out.println(szVersion);
//Load up the background color from a hex triplet
//debug("Entered init()");
colBackground = getColorFromParam("bgcolor",Color.gray);
// we always use the same on-screen graphics context, get it now..
gcOn = getGraphics();
szBackgroundFile = getParameter("background");
if (null != (imgBackground = getImage(getDocumentBase(),szBackgroundFile)))
{
bBackgroundOK = true;
iOutstandingImageCount++;
}
szForegroundFile = getParameter("foreground");
if (null != (imgForeground = getImage(getDocumentBase(),szForegroundFile)))
{
bForegroundOK = true;
iOutstandingImageCount++;
}
maintainStaticImages();
copyOfftoOn();
// now initialise all the hot areas.
boolean bDone = false;
int j = 1;
while (!bDone)
{
bDone = (null==(HotAreas[j] = HotAreaFromParam(j++)));
}
iMaxHotArea = --j;
bInitComplete = true;
//debug("Exited init()");
} //init()
//----------------------------------------------------------------------------
// Added start() stop() back in when flicker re-appeared..as a test
public void start() {
timer = new Thread(this);
timer.start();
}
public void stop() {
timer.stop();
// 27-Feb-1996/SBH Added following line to fix bug whereby mouseup
// sometimes not seen until mousemove occurs.
timer=null;
}
//----------------------------------------------------------------------------
public void setStatusText(String _szStatus, boolean bVisible)
// Description
// ===========
// If bVisible is true, then we display this text. If bVisible is false,
// we set the status bar to empty, if the last string displayed was same
// this one. This gives each hot_area apparent access to their own
// status bar, with the last call with bVisible=true having priority.
{
if (bVisible)
{
showStatus(_szStatus);
szLastStatus = _szStatus;
}
else
{
if (szLastStatus.equals(_szStatus))
{
szLastStatus = "";
showStatus("");
}
}
}//setStatusText()
//----------------------------------------------------------------------------
public void setNewHintText(String _szHint)
// Description
// ===========
// bVisible is true, then display szText as a hint.
// otherwise, hide the hint, if the current text shown is szText
//
//
{
// if someone wants to display a hint, we must
bHintVisible |= true;
// but check if it's different from the last one displayed (we
// may not need to redraw otherwise)
// if the new hint is null, make sure we set flag (as appropriate)
// but don't call .equals()
if (_szHint == null)
{
if (szHintText != null)
bHintChanged = true;
szHintText = null;
}
else
{
if (!_szHint.equals(szHintText))
{
bHintChanged = true;
szHintText = _szHint;
}
}
}//setHintNewText()
//----------------------------------------------------------------------------
private hot_area HotAreaFromParam(int iAreaNumber)
// Description
// ===========
//
// Create a hot_area, given an integer. If not created, return null
//
// It is assumed that the HTML contains a parameter hot_area<iAreaNumber>
//
//
{
hot_area retArea = null;
String szParamValue;
szParamValue = getParameter("hot_area" + iAreaNumber);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -