home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 4
/
BUGCD1997_05.BIN
/
aplic
/
visualj
/
vjtrial.exe
/
RCDATA
/
CABINET
/
vjappwiz.awx
/
TEMPLATE
/
APPLET.JVA
< prev
next >
Wrap
Text File
|
1997-01-28
|
21KB
|
683 lines
$$IF(Comments)
//******************************************************************************
// $$AppName$$.java: Applet
//
//******************************************************************************
$$ENDIF
import java.applet.*;
import java.awt.*;
$$IF(StandAlone)
import $$AppName$$Frame;
$$ENDIF(StandAlone)
$$IF(Comments)
//==============================================================================
// Main Class for applet $$AppName$$
//
//==============================================================================
$$ENDIF
$$IF(IsRunnable)
public class $$AppName$$ extends Applet implements Runnable
$$ELSE
public class $$AppName$$ extends Applet
$$ENDIF
{
$$IF(IsRunnable)
$$IF(Comments)
// THREAD SUPPORT:
// m_$$AppName$$ is the Thread object for the applet
//--------------------------------------------------------------------------
$$ENDIF
private Thread m_$$AppName$$ = null;
$$IF(Animation)
$$IF(Comments)
// ANIMATION SUPPORT:
// m_Graphics used for storing the applet's Graphics context
// m_Images[] the array of Image objects for the animation
// m_nCurrImage the index of the next image to be displayed
// m_ImgWidth width of each image
// m_ImgHeight height of each image
// m_fAllLoaded indicates whether all images have been loaded
// NUM_IMAGES number of images used in the animation
//--------------------------------------------------------------------------
$$ENDIF
private Graphics m_Graphics;
private Image m_Images[];
private int m_nCurrImage;
private int m_nImgWidth = 0;
private int m_nImgHeight = 0;
private boolean m_fAllLoaded = false;
private final int NUM_IMAGES = 18;
$$ENDIF(Animation)
$$ENDIF(IsRunnable)
$$IF(StandAlone)
// STANDALONE APPLICATION SUPPORT:
// m_fStandAlone will be set to true if applet is run standalone
//--------------------------------------------------------------------------
private boolean m_fStandAlone = false;
$$ENDIF
$$IF(HasParameters)
$$IF(Comments)
// 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>
//--------------------------------------------------------------------------
$$ENDIF
$$BEGINLOOP(Parameters)
$$IF(HasMember)
private $$ParamType$$ $$ParamMemberName$$ = $$ParamDefValue$$;
$$ENDIF
$$ENDLOOP
// 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.
//--------------------------------------------------------------------------
$$BEGINLOOP(Parameters)
private final String PARAM_$$ParamName$$ = "$$ParamName$$";
$$ENDLOOP
$$IF(StandAlone)
$$IF(Comments)
// STANDALONE APPLICATION SUPPORT
// The GetParameter() method is a replacement for the getParameter() method
// defined by Applet. This method returns the value of the specified parameter;
// unlike the original getParameter() method, this method works when the applet
// is run as a standalone application, as well as when run within an HTML page.
// This method is called by GetParameters().
//---------------------------------------------------------------------------
$$ENDIF(Comments)
String GetParameter(String strName, String args[])
{
if (args == null)
{
$$IF(Comments)
// Running within an HTML page, so call original getParameter().
//-------------------------------------------------------------------
$$ENDIF(Comments)
return getParameter(strName);
}
$$IF(Comments)
// Running as standalone application, so parameter values are obtained from
// the command line. The user specifies them as follows:
//
// JView $$AppName$$ param1=<val> param2=<"val with spaces"> ...
//-----------------------------------------------------------------------
$$ENDIF(Comments)
int i;
String strArg = strName + "=";
String strValue = null;
int nLength = strArg.length();
try
{
for (i = 0; i < args.length; i++)
{
String strParam = args[i].substring(0, nLength);
if (strArg.equalsIgnoreCase(strParam))
{
// Found matching parameter on command line, so extract its value.
// If in double quotes, remove the quotes.
//---------------------------------------------------------------
strValue = args[i].substring(nLength);
if (strValue.startsWith("\""))
{
strValue = strValue.substring(1);
if (strValue.endsWith("\""))
strValue = strValue.substring(0, strValue.length() - 1);
}
break;
}
}
}
catch (Exception e)
{
$$IF(TODOComments)
// TODO: Place exception-handling code here in case an
$$ENDIF(TODOComments)
}
return strValue;
}
$$IF(Comments)
// STANDALONE APPLICATION SUPPORT
// The GetParameters() method retrieves the values of each of the applet's
// parameters and stores them in variables. This method works both when the
// applet is run as a standalone application and when it's run within an HTML
// page. When the applet is run as a standalone application, this method is
// called by the main() method, which passes it the command-line arguments.
// When the applet is run within an HTML page, this method is called by the
// init() method with args == null.
//---------------------------------------------------------------------------
$$ENDIF(Comments)
void GetParameters(String args[])
{
$$IF(Comments)
// Query values of all Parameters
//--------------------------------------------------------------
$$ENDIF
String param;
$$BEGINLOOP(Parameters)
$$IF(Comments)
// $$ParamName$$: $$ParamDescription$$
//--------------------------------------------------------------
$$ENDIF
param = GetParameter(PARAM_$$ParamName$$, args);
if (param != null)
$$IF(HasMember)
$$ParamMemberName$$ = $$ParamFromString$$;
$$ELSE
$$IF(TODOComments)
// TODO: Process parameter $$ParamName$$
$$ENDIF
;
else
$$IF(TODOComments)
// TODO: Handle case of parameter not provided
$$ENDIF
;
$$ENDIF(HasMember)
$$ENDLOOP()
}
$$ENDIF(StandAlone)
$$ENDIF(HasParameters)
$$IF(StandAlone)
$$IF(Comments)
// STANDALONE APPLICATION SUPPORT
// The main() method acts as the applet's entry point when it is run
// as a standalone application. It is ignored if the applet is run from
// within an HTML page.
//--------------------------------------------------------------------------
$$ENDIF
public static void main(String args[])
{
$$IF(Comments)
// Create Toplevel Window to contain applet $$AppName$$
//----------------------------------------------------------------------
$$ENDIF
$$AppName$$Frame frame = new $$AppName$$Frame("$$AppName$$");
// Must show Frame before we size it so insets() will return valid values
//----------------------------------------------------------------------
frame.show();
frame.hide();
frame.resize(frame.insets().left + frame.insets().right + $$InitialWidth$$,
frame.insets().top + frame.insets().bottom + $$InitialHeight$$);
$$IF(Comments)
// The following code starts the applet running within the frame window.
// It also calls GetParameters() to retrieve parameter values from the
// command line, and sets m_fStandAlone to true to prevent init() from
// trying to get them from the HTML page.
//----------------------------------------------------------------------
$$ENDIF
$$AppName$$ applet_$$AppName$$ = new $$AppName$$();
frame.add("Center", applet_$$AppName$$);
applet_$$AppName$$.m_fStandAlone = true;
$$IF(HasParameters)
applet_$$AppName$$.GetParameters(args);
$$ENDIF
applet_$$AppName$$.init();
applet_$$AppName$$.start();
frame.show();
}
$$ENDIF(StandAlone)
$$IF(Comments)
// $$AppName$$ Class Constructor
//--------------------------------------------------------------------------
$$ENDIF
public $$AppName$$()
{
$$IF(TODOComments)
// TODO: Add constructor code here
$$ENDIF
}
$$IF(Comments)
// APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
//--------------------------------------------------------------------------
$$ENDIF
public String getAppletInfo()
{
$$BEGINLOOP(AppInfoLines)
$$AppInfoLine$$
$$ENDLOOP
}
$$IF(HasParameters)
$$IF(Comments)
// PARAMETER SUPPORT
// The getParameterInfo() method returns an array of strings describing
// the parameters understood by this applet.
//
// $$AppName$$ Parameter Information:
// { "Name", "Type", "Description" },
//--------------------------------------------------------------------------
$$ENDIF
public String[][] getParameterInfo()
{
String[][] info =
{
$$BEGINLOOP(Parameters)
{ PARAM_$$ParamName$$, "$$ParamType$$", "$$ParamDescription$$" },
$$ENDLOOP
};
return info;
}
$$ENDIF(Parameters)
$$IF(Comments)
// 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.
//--------------------------------------------------------------------------
$$ENDIF
public void init()
{
$$IF(HasParameters)
$$IF(StandAlone)
if (!m_fStandAlone)
GetParameters(null);
$$ELSE
$$IF(Comments)
// PARAMETER SUPPORT
// The following code retrieves the value of each parameter
// specified with the <PARAM> tag and stores it in a member
// variable.
//----------------------------------------------------------------------
$$ENDIF
String param;
$$BEGINLOOP(Parameters)
$$IF(Comments)
// $$ParamName$$: $$ParamDescription$$
//----------------------------------------------------------------------
$$ENDIF
param = getParameter(PARAM_$$ParamName$$);
if (param != null)
$$IF(HasMember)
$$ParamMemberName$$ = $$ParamFromString$$;
$$ELSE
$$IF(TODOComments)
// TODO: Process parameter $$ParamName$$
$$ENDIF
;
else
$$IF(TODOComments)
// TODO: Handle case of parameter not provided
$$ENDIF
;
$$ENDIF(HasMember)
$$ENDLOOP()
$$ENDIF(StandAlone)
$$ENDIF(HasParameters)
$$IF(Comments)
// If you use a ResourceWizard-generated "control creator" class to
// arrange controls in your applet, you may want to call its
// CreateControls() method from within this method. Remove the following
// call to resize() before adding the call to CreateControls();
// CreateControls() does its own resizing.
//----------------------------------------------------------------------
$$ENDIF(Comments)
resize($$InitialWidth$$, $$InitialHeight$$);
$$IF(TODOComments)
// TODO: Place additional initialization code here
$$ENDIF
}
$$IF(Comments)
// Place additional applet clean up code here. destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
$$ENDIF
public void destroy()
{
$$IF(TODOComments)
// TODO: Place applet cleanup code here
$$ENDIF
}
$$IF(Animation)
$$IF(Comments)
// ANIMATION SUPPORT:
// Draws the next image, if all images are currently loaded
//--------------------------------------------------------------------------
$$ENDIF
private void displayImage(Graphics g)
{
if (!m_fAllLoaded)
return;
$$IF(Comments)
// Draw Image in center of applet
//----------------------------------------------------------------------
$$ENDIF
g.drawImage(m_Images[m_nCurrImage],
(size().width - m_nImgWidth) / 2,
(size().height - m_nImgHeight) / 2, null);
}
$$ENDIF(Animation)
$$IF(Comments)
// $$AppName$$ Paint Handler
//--------------------------------------------------------------------------
$$ENDIF
public void paint(Graphics g)
{
$$IF(Animation)
// ANIMATION SUPPORT:
// The following code displays a status message until all the
// images are loaded. Then it calls displayImage to display the current
// image.
//----------------------------------------------------------------------
if (m_fAllLoaded)
{
Rectangle r = g.getClipRect();
g.clearRect(r.x, r.y, r.width, r.height);
displayImage(g);
}
else
g.drawString("Loading images...", 10, 20);
$$IF(TODOComments)
// TODO: Place additional applet Paint code here
$$ENDIF
$$ELIF(IsRunnable)
$$IF(TODOComments)
// TODO: Place applet paint code here
$$ENDIF
g.drawString("Running: " + Math.random(), 10, 20);
$$ELSE
g.drawString("Created with Microsoft Visual J++ Version 1.1", 10, 20);
$$ENDIF(Animation)
}
$$IF(Comments)
// 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.
//--------------------------------------------------------------------------
$$ENDIF
public void start()
{
$$IF(IsRunnable)
if (m_$$AppName$$ == null)
{
m_$$AppName$$ = new Thread(this);
m_$$AppName$$.start();
}
$$ENDIF
$$IF(TODOComments)
// TODO: Place additional applet start code here
$$ENDIF
}
$$IF(Comments)
// 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.
//--------------------------------------------------------------------------
$$ENDIF
public void stop()
{
$$IF(IsRunnable)
if (m_$$AppName$$ != null)
{
m_$$AppName$$.stop();
m_$$AppName$$ = null;
}
$$IF(TODOComments)
// TODO: Place additional applet stop code here
$$ENDIF
$$ENDIF(IsRunnable)
}
$$IF(IsRunnable)
$$IF(Comments)
// THREAD SUPPORT
// The run() method is called when the applet's thread is started. If
// your applet performs any ongoing activities without waiting for user
// input, the code for implementing that behavior typically goes here. For
// example, for an applet that performs animation, the run() method controls
// the display of images.
//--------------------------------------------------------------------------
$$ENDIF
public void run()
{
$$IF(Animation)
m_nCurrImage = 0;
// If re-entering the page, then the images have already been loaded.
// m_fAllLoaded == TRUE.
//----------------------------------------------------------------------
if (!m_fAllLoaded)
{
repaint();
m_Graphics = getGraphics();
m_Images = new Image[NUM_IMAGES];
$$IF(Comments)
// Load in all the images
//------------------------------------------------------------------
$$ENDIF
MediaTracker tracker = new MediaTracker(this);
String strImage;
// For each image in the animation, this method first constructs a
// string containing the path to the image file; then it begins
// loading the image into the m_Images array. Note that the call to
// getImage will return before the image is completely loaded.
//------------------------------------------------------------------
for (int i = 1; i <= NUM_IMAGES; i++)
{
$$IF(Comments)
// Build path to next image
//--------------------------------------------------------------
$$ENDIF
strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";
$$IF(StandAlone)
if (m_fStandAlone)
m_Images[i-1] = Toolkit.getDefaultToolkit().getImage(strImage);
else
m_Images[i-1] = getImage(getDocumentBase(), strImage);
$$ELSE
m_Images[i-1] = getImage(getDocumentBase(), strImage);
$$ENDIF
tracker.addImage(m_Images[i-1], 0);
}
$$IF(Comments)
// Wait until all images are fully loaded
//------------------------------------------------------------------
$$ENDIF
try
{
tracker.waitForAll();
m_fAllLoaded = !tracker.isErrorAny();
}
catch (InterruptedException e)
{
$$IF(TODOComments)
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
$$ENDIF
}
if (!m_fAllLoaded)
{
stop();
m_Graphics.drawString("Error loading images!", 10, 40);
return;
}
$$IF(Comments)
// Assuming all images are same width and height.
//--------------------------------------------------------------
$$ENDIF
m_nImgWidth = m_Images[0].getWidth(this);
m_nImgHeight = m_Images[0].getHeight(this);
}
repaint();
$$ENDIF(Animation)
while (true)
{
try
{
$$IF(Animation)
$$IF(Comments)
// Draw next image in animation
//--------------------------------------------------------------
$$ENDIF
displayImage(m_Graphics);
m_nCurrImage++;
if (m_nCurrImage == NUM_IMAGES)
m_nCurrImage = 0;
$$ELSE
repaint();
$$ENDIF(Animation)
$$IF(TODOComments)
// TODO: Add additional thread-specific code here
$$ENDIF
Thread.sleep(50);
}
catch (InterruptedException e)
{
$$IF(TODOComments)
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
$$ENDIF
stop();
}
}
}
$$ENDIF(IsRunnable)
$$IF(Mouse)
$$IF(MouseDownUp)
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseDown() method is called if the mouse button is pressed
// while the mouse cursor is over the applet's portion of the screen.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseDown(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseDown code here
$$ENDIF
return true;
}
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseUp() method is called if the mouse button is released
// while the mouse cursor is over the applet's portion of the screen.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseUp(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseUp code here
$$ENDIF
return true;
}
$$ENDIF(MouseDownUp)
$$IF(MouseDragMove)
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseDrag() method is called if the mouse cursor moves over the
// applet's portion of the screen while the mouse button is being held down.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseDrag(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseDrag code here
$$ENDIF
return true;
}
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseMove() method is called if the mouse cursor moves over the
// applet's portion of the screen and the mouse button isn't being held down.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseMove(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseMove code here
$$ENDIF
return true;
}
$$ENDIF(MouseDragMove)
$$IF(MouseEnterExit)
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseEnter() method is called if the mouse cursor enters the
// applet's portion of the screen.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseEnter(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseEnter code here
$$ENDIF
return true;
}
$$IF(Comments)
// MOUSE SUPPORT:
// The mouseExit() method is called if the mouse cursor leaves the
// applet's portion of the screen.
//--------------------------------------------------------------------------
$$ENDIF
public boolean mouseExit(Event evt, int x, int y)
{
$$IF(TODOComments)
// TODO: Place applet mouseExit code here
$$ENDIF
return true;
}
$$ENDIF(MouseDragMove)
$$ENDIF(Mouse)
$$IF(TODOComments)
// TODO: Place additional applet code here
$$ENDIF
}