Reactive Button


Please copy, compile and use the following code on your web page. The more folks using Java the more likely Microsoft will return Java to its own web pages. View document source to see how the button is implemented. The .au files are in JDK 1.02, or make your own. The idea is for the button to repeat its name while it is highlighted for use. The button can be used on many pages on your web site. It is loaded only once the first page visited and remains available for use on otherpages. Hence Java is faster than scriplets when your visitors are expected to view more than a single page from your site. Go Java.

Java


// Reactive button Copyright(C)1996,1997. Masakazu Fujimiya

// m-fujimiya@nri.co.jp

// Modifications for sound Copyright(C) 1997. Mighetto & Associates

// mighetto@eskimo.com

import java.applet.*;

import java.awt.*;

import java.net.*;

public class ReactiveButton extends java.applet.Applet {

Color fColor,nColor,fFontColornFontColor,f1Color,n1Color;

boolean focus,bDown;

String str,fstr,fontName;

String toURL,toFrame;

URL docURL;

int stx,sty,fstx,fsty;

int fontSize;

// Mighetto & Associates**************************************

SoundList soundList;

String onceFile;

String loopFile;

AudioClip onceClip;

AudioClip loopClip;

boolean looping = false;

//*********************************************************

public void init() {

String at;

int h,w;

// Mighetto & Associates**************************************

loopFile = getParameter("focus_sound");

if (loopFile==null) loopFile = "spacemusic.au";

onceFile = getParameter("pressed_sound");

if (onceFile==null) onceFile = "whoopy.au";

startLoadingSounds();

//*********************************************************

str = getParameter("string");

if (str==null) str = "";

fstr = getParameter("focus_string");

if (fstr==null) fstr = str;

toURL = getParameter("url");

if (toURL==null) {

docURL = null;

} else {

try {docURL = new URL(toURL);} catch (MalformedURLException e) {docURL = null;}

}

toFrame = getParameter("frame");

at = getParameter("normal_button_color");

if (at==null) {

nColor = new Color(192, 192, 192);

} else {

nColor = new Color(Integer.valueOf(at,16).intValue());

}

n1Color = new Color(255-(255-nColor.getRed())/2, 255-(255-nColor.getGreen())/2, 255-(255-nColor.getBlue())/2);

at = getParameter("focus_button_color");

if (at==null) {

fColor = new Color(160, 160, 160);

} else {

fColor = new Color(Integer.valueOf(at,16).intValue());

}

f1Color = new Color(255-(255-fColor.getRed())/2, 255-(255-fColor.getGreen())/2, 255-(255-fColor.getBlue())/2);

at = getParameter("normal_font_color");

if (at==null) {

nFontColor = new Color(0);

} else {

nFontColor = new Color(Integer.valueOf(at,16).intValue());

}

at = getParameter("focus_font_color");

if (at==null) {

fFontColor = nFontColor;

} else {

fFontColor = new Color(Integer.valueOf(at,16).intValue());

}

at = getParameter("font_size");

if (at==null) {

fontSize = 14;

} else {

fontSize = Integer.valueOf(at).intValue();

}

at = getParameter("font_name");

if (at==null) {

fontName = "TimesRoman";

} else {

fontName = at;

}

Graphics g = getGraphics();

g.setFont(new java.awt.Font(fontName, Font.PLAIN, fontSize));

setFont(new java.awt.Font(fontName, Font.PLAIN, fontSize));

h = g.getFontMetrics().getHeight();

fsty = sty = (size().height - h)/2 + h*8/10;

w = g.getFontMetrics().stringWidth(str);

stx = (size().width - w)/2;

w = g.getFontMetrics().stringWidth(fstr);

fstx = (size().width - w)/2;

bDown = focus = false;

}

// Mighetto & Associates**************************************

void startLoadingSounds() {

//Start asynchronous sound loading.

soundList = new SoundList(this, getCodeBase());

soundList.startLoading(loopFile);

soundList.startLoading(onceFile);

}

public void stop() {

//If one-time sound were long, we'd stop it here, too.

if (looping) {

loopClip.stop(); //Stop the sound loop.

}

}

public void start() {

if (looping) {

loopClip.loop(); //Restart the sound loop.

}

}

//*********************************************************

public void paint(Graphics g) {

int x,y;

if (focus) {

x = fstx ; y = fsty;

} else {

x = stx ; y = sty;

}

if (bDown) {

x-- ; y--;

}

if (focus) {

g.setColor(fColor);

} else {

g.setColor(nColor);

}

g.fill3DRect(0, 0, size().width, size().height, !bDown);

if (focus) {

g.setColor(f1Color);

g.drawString(fstr, x+1, y+1);

g.setColor(fFontColor);

g.drawString(fstr, x, y);

} else {

g.setColor(n1Color);

g.drawString(str, x+1, y+1);

g.setColor(nFontColor);

g.drawString(str, x, y);

}

}



public boolean mouseUp(java.awt.Event evt, int jx, int jy) {

bDown = false;

getAppletContext().showStatus("Go to " + str);

repaint();

if (inside(jx,jy)) {

if (toFrame==null) {

if (docURL!=null) getAppletContext().showDocument(docURL);

} else {

if (docURL!=null) getAppletContext().showDocument(docURL, toFrame);

}

}

return(true);

}



public boolean mouseDown(java.awt.Event evt, int jx, int jy) {

bDown = true;

//getAppletContext().showStatus(docURL.getProtocol()+"://"+docURL.getHost()+docURL. getFile());

//getAppletContext().showStatus(getCodeBase().getProtocol()+":"+getCodeBase().getHost()+ge tCodeBase().getFile());

//getAppletContext().showStatus("Reactive button... Copyright(C)1997. mighetto@eskimo.com 04");

// Mighetto & Associates**************************************

if (onceClip == null) {

//Try to get the AudioClip.

onceClip = soundList.getClip(onceFile);

}

if (onceClip != null) { //If the sound is loaded:

onceClip.play(); //Play it once.

showStatus("Playing sound " + onceFile + ".");

} else {

showStatus("Sound " + onceFile + " not loaded yet.");

}

// return true;

//*********************************************************

repaint();

return(true);

}



public boolean mouseMove(java.awt.Event evt, int jx, int jy) {

getAppletContext().showStatus(docURL.getProtocol()+"://"+docURL.getHost()+docURL.g etFile());

return(true);

}

public boolean mouseExit(java.awt.Event evt, int jx, int jy) {

focus = false;

getAppletContext().showStatus("");

repaint();

// Mighetto & Associates**************************************

if (looping) {

looping = false;

loopClip.stop(); //Stop the sound loop.

}

showStatus("Stopped playing sound " + loopFile + ".");

//***************************************************061197

return(true);

}

public boolean mouseEnter(java.awt.Event evt, int jx, int jy) {

focus = true;

getAppletContext().showStatus(docURL.getProtocol()+"://"+docURL.getHost()+docURL.g etFile());

repaint();

// Mighetto & Associates**************************************

if (loopClip == null) {

//Try to get the AudioClip.

loopClip = soundList.getClip(loopFile);

}

if (loopClip != null) { //If the sound is loaded:

looping = true;

loopClip.loop(); //Start the sound loop.

showStatus("Playing sound " + loopFile + " continuously.");

} else {

showStatus("Sound " + loopFile + " not loaded yet.");

}

//***************************************************061197

return(true);

}

}


Java


Java


Java


Java


Java