home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1996 December
/
PCWKCD1296.iso
/
vjplusb
/
msdev
/
samples
/
microsoft
/
javabeep
/
javabeep.java
< prev
next >
Wrap
Text File
|
1996-07-12
|
15KB
|
413 lines
//******************************************************************************
// javabeep.java: Applet
//
// This applet is a simple demonstration of calling a COM object in a
// Java program, utilizing the ActiveX runtime for Java. The COM object
// is the Beeper sample from the ActiveX Template Library (ATL). ATL is
// a Template based Visual C++ 4.x add on that contains a custom AppWizard
// and various header files to generate a skeleton ActiveX Control. For
// more information on ATL, including the source code to the beeper.dll
// COM object, please visit the Microsoft web site at:
//
// http://www.microsoft.com/visualc/v42/atl
//
// Most of the code of interest can be found in the javabeep.mouseDown()
// method
//******************************************************************************
import java.applet.*;
import java.awt.*;
import javabeepframe;
// Import the class files that JavaTLB generates from beeper.dll
import beeper.*;
//==============================================================================
// Main Class for applet javabeep
//
//==============================================================================
public class javabeep extends Applet implements Runnable
{
// Beeper COM Interface variable
IBeeper m_Beeper=null;
// THREAD SUPPORT:
// m_javabeep is the Thread object for the applet
//--------------------------------------------------------------------------
Thread m_javabeep = null;
// 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
//--------------------------------------------------------------------------
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;
// STANDALONE APPLICATION SUPPORT:
// m_fStandAlone will be set to true if applet is run standalone
//--------------------------------------------------------------------------
boolean m_fStandAlone = false;
// 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.
//--------------------------------------------------------------------------
public static void main(String args[])
{
// Create Toplevel Window to contain applet javabeep
//----------------------------------------------------------------------
javabeepframe frame = new javabeepframe("javabeep");
// 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 + 320,
frame.insets().top + frame.insets().bottom + 240);
// 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.
//----------------------------------------------------------------------
javabeep applet_javabeep = new javabeep();
frame.add("Center", applet_javabeep);
applet_javabeep.m_fStandAlone = true;
applet_javabeep.init();
applet_javabeep.start();
frame.show();
}
// javabeep Class Constructor
//--------------------------------------------------------------------------
public javabeep()
{
// 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: javabeep\r\n" +
"Author: Microsoft Developer Support\r\n" +
"Created with Microsoft Visual J++ Version 1.0";
}
// 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()
{
// 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.
//----------------------------------------------------------------------
resize(320, 240);
// TODO: Place additional initialization code here
}
// 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
}
// ANIMATION SUPPORT
// The imageUpdate() method is called repeatedly by the AWT while
// images are being constructed. The flags parameter provides information
// about the status of images under construction. The AppletWizard's
// initial implementation of this method checks whether the ALLBITS flag is
// set. When the ALLBITS is set, this method knows that an image is
// completely loaded. When all the images are completely loaded, this
// method sets the m_fAllLoaded variable to true so that animation can begin.
//--------------------------------------------------------------------------
public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
{
// Nothing to do if images are all loaded
//----------------------------------------------------------------------
if (m_fAllLoaded)
return false;
// Want all bits to be available before painting
//----------------------------------------------------------------------
if ((flags & ALLBITS) == 0)
return true;
// All bits are available, so increment loaded count of fully
// loaded images, starting animation if all images are loaded
//----------------------------------------------------------------------
if (++m_nCurrImage == NUM_IMAGES)
{
m_nCurrImage = 0;
m_fAllLoaded = true;
}
return false;
}
// ANIMATION SUPPORT:
// Draws the next image, if all images are currently loaded
//--------------------------------------------------------------------------
private void displayImage(Graphics g)
{
if (!m_fAllLoaded)
return;
// Draw Image in center of applet
//----------------------------------------------------------------------
g.drawImage(m_Images[m_nCurrImage],
(size().width - m_nImgWidth) / 2,
(size().height - m_nImgHeight) / 2, null);
// Draw a click me label
FontMetrics fm = getFontMetrics(getFont());
String message = new String("Click me with the mouse!");
g.drawString(message,
(size().width-fm.stringWidth(message))/2,
3*(size().height)/4);
}
// javabeep Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
// 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);
// TODO: Place additional applet Paint code here
}
// 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()
{
if (m_javabeep == null)
{
m_javabeep = new Thread(this);
m_javabeep.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()
{
if (m_javabeep != null)
{
m_javabeep.stop();
m_javabeep = null;
}
// TODO: Place additional applet stop code here
}
// 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.
//--------------------------------------------------------------------------
public void run()
{
repaint();
m_Graphics = getGraphics();
m_nCurrImage = 0;
m_Images = new Image[NUM_IMAGES];
// Load in all the images
//----------------------------------------------------------------------
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++)
{
// Build path to next image
//------------------------------------------------------------------
strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";
if (m_fStandAlone)
m_Images[i-1] = Toolkit.getDefaultToolkit().getImage(strImage);
else
m_Images[i-1] = getImage(getDocumentBase(), strImage);
// Get width and height of one image.
// Assuming all images are same width and height
//------------------------------------------------------------------
if (m_nImgWidth == 0)
{
try
{
// The getWidth() and getHeight() methods of the Image class
// return -1 if the dimensions are not yet known. The
// following code keeps calling getWidth() and getHeight()
// until they return actual values.
// NOTE: This is only executed once in this loop, since we
// are assuming all images are the same width and
// height. However, since we do not want to duplicate
// the above image load code, the code resides in the
// loop.
//----------------------------------------------------------
while ((m_nImgWidth = m_Images[i-1].getWidth(null)) < 0)
Thread.sleep(1);
while ((m_nImgHeight = m_Images[i-1].getHeight(null)) < 0)
Thread.sleep(1);
}
catch (InterruptedException e)
{
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
}
}
// Force image to fully load
//------------------------------------------------------------------
m_Graphics.drawImage(m_Images[i-1], -1000, -1000, this);
}
// Wait until all images are fully loaded
//----------------------------------------------------------------------
while (!m_fAllLoaded)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
}
}
repaint();
while (true)
{
try
{
// Draw next image in animation
//--------------------------------------------------------------
displayImage(m_Graphics);
m_nCurrImage++;
if (m_nCurrImage == NUM_IMAGES)
m_nCurrImage = 0;
// TODO: Add additional thread-specific code here
Thread.sleep(50);
}
catch (InterruptedException e)
{
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
stop();
}
}
}
// 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.
//--------------------------------------------------------------------------
public boolean mouseDown(Event evt, int x, int y)
{
String BeeperString = new String();
// Check if the Beeper object exists, and create it if necessary
if(m_Beeper==null)
m_Beeper = (IBeeper) new Beeper();
// Call Beeper object 'Beep' method. Note that the Beep object has
// been written such that on the sixth call to Beep(), the Beep object
// will return an OLE error code which gets thrown as a Java exception.
// This sample catches the exception, Releases the Beeper object,
// creates a new Beeper object, and continues
try
{
m_Beeper.Beep();
}
catch(com.ms.com.ComException e)
{
// Release the Beeper object by setting m_Beeper=null
m_Beeper=null;
// Create a new Beeper object
m_Beeper = (IBeeper) new Beeper();
m_Beeper.Beep();
}
// Call the Beepers getCount and getItem methods
for(int i=1;i<=m_Beeper.getCount();i++)
{
// Build a message string from the strings that getItem returns
BeeperString+=m_Beeper.getItem(i)+" ";
}
// Display the string
m_Graphics.drawString(BeeperString,x,y);
return true;
}
// 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.
//--------------------------------------------------------------------------
public boolean mouseUp(Event evt, int x, int y)
{
// TODO: Place applet mouseUp code here
return true;
}
// TODO: Place additional applet code here
}