multithread.java

来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 425 行

JAVA
425
字号

//******************************************************************************
// MultiThread.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import MultiThreadFrame;

//==============================================================================
// Main Class for applet MultiThread
//
//==============================================================================
public class MultiThread extends Applet implements Runnable
{
	// THREAD SUPPORT:
	//		m_MultiThread	is the Thread object for the applet
	//--------------------------------------------------------------------------
	int x=0,y=0;
	int a=0,b=0;
	int MouseX=0,MouseY=0;
	int ang=0;	
    int waittime=0;
    String starttext="start";
	String endtext="end";
    Button endbutton,startbutton;
	private Thread	 m_MultiThread = null;
    private Thread	 me_MultiThread = 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
	//--------------------------------------------------------------------------
	private 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 MultiThread
		//----------------------------------------------------------------------
		MultiThreadFrame frame = new MultiThreadFrame("MultiThread");
		MultiThread applet_MultiThread = new MultiThread();
		frame.add("Center", applet_MultiThread);

		// 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.
		//----------------------------------------------------------------------

		applet_MultiThread.m_fStandAlone = true;
		applet_MultiThread.init();
		applet_MultiThread.start();
                             frame.show();
	}

	// MultiThread Class Constructor
	//--------------------------------------------------------------------------
	public MultiThread()
	{
		// 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: MultiThread\r\n" +
		       "Author: user18\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	// 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()
	{
	    setLayout(new BorderLayout());
		waittime=Integer.parseInt(getParameter("waittime"));

/*		if(starttext==null)
		{
		    starttext="start";
		}
*/		startbutton=new Button(starttext);
                add("South", startbutton);
		startbutton.addMouseListener(this);
/*		if(endtext==null)
		{
		    endtext="end";
		}
*/		endbutton=new Button(endtext);
		add("North", endbutton);
		endbutton.addMouseListener(this);
	}

	public void destroy()
	{

	}


	private void displayImage(Graphics g)
	{   
	
		if (!m_fAllLoaded)
			return;

		// Draw Image in center of applet
		//----------------------------------------------------------------------
		if(Thread.currentThread()==m_MultiThread)

              g.drawImage(m_Images[m_nCurrImage],MouseX,MouseY,null);
//				   (size().width - m_nImgWidth)   / 2,
//				   (size().height - m_nImgHeight) / 2, null);
		else
		{
			    g.clearRect(x,y,m_nImgWidth,m_nImgHeight);
				x=   (size().width - m_nImgWidth)   / 2 +(int)(200*Math.cos(Math.PI*ang/180));
				y=   (size().height - m_nImgHeight) /2+(int)(200*Math.sin(Math.PI*ang/180));
					 g.drawImage(m_Images[m_nCurrImage],x,y,null);
				ang++;
				if(ang > 360)
				   ang=0;
				   a=(size().width - m_nImgWidth)   / 2 +(int)(100*Math.cos(Math.PI*ang/180));
				  // a=(x-50)*(int)(200*Math.cos(Math.PI*ang/180));
				   b=  (size().height - m_nImgHeight) /2+(int)(100*Math.sin(Math.PI*ang/180));
				 //  b=(y-50)*(int)(200*Math.sin(Math.PI*ang/180));
                   g.drawLine(a,b,(a+1),(b+1));
		}
	}

	// MultiThread 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);
		}
		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_MultiThread == null)
		{
			m_MultiThread = new Thread(this);
			m_MultiThread.start();
		}
		if (me_MultiThread == null)
		{
			me_MultiThread = new Thread(this);
			me_MultiThread.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_MultiThread != null)
		{
			m_MultiThread.stop();
			m_MultiThread = null;
		}
		if (me_MultiThread != null)
		{
			me_MultiThread.stop();
			me_MultiThread = 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 boolean action(Event event, Object arg)
	{   
		if(arg.equals("play"))
		{
			my_start();
		}
		else if(arg.equals("stop"))
		{
			my_stop();
		}
		return true;
	}
	
	public void run()
	{
		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];

    		// Load in all the images
    		//------------------------------------------------------------------
    		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++)
    		{
    			// 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);

                tracker.addImage(m_Images[i-1], 0);
    		}

    		// Wait until all images are fully loaded
    		//------------------------------------------------------------------
			try
			{
				tracker.waitForAll();
				m_fAllLoaded = !tracker.isErrorAny();
			}
			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
			}
			
			if (!m_fAllLoaded)
			{
			    stop();
			    m_Graphics.drawString("Error loading images!", 10, 40);
			    return;
			}
			

			// Assuming all images are same width and height.
			//--------------------------------------------------------------
		    m_nImgWidth  = m_Images[0].getWidth(this);
		    m_nImgHeight = m_Images[0].getHeight(this);
        }		
		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
			if(Thread.currentThread()==m_MultiThread)
        
				Thread.sleep(waittime);
			else
			    Thread.sleep(150);
			}
			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)
	{
		// TODO: Place applet mouseDown code here
		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;
	}

	// 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.
	//--------------------------------------------------------------------------
	public boolean mouseDrag(Event evt, int x, int y)
	{
		// TODO: Place applet mouseDrag code here
		Graphics g=getGraphics();
		g.clearRect( MouseX,MouseY, m_nImgWidth, m_nImgHeight);
		MouseX=x;
		MouseY=y;
		return true;

	}


	public void mouseClicked(MouseEvent evt)
	{     

	       if(evt.getSource()==endbutton)  
		    {  
			    stop();
	            m_MultiThread=null;
		    }
		   else if(evt.getSource()==startbutton)
		        start();
	}
	
	
	// 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.
	//--------------------------------------------------------------------------
	//public boolean action(Event evt,int x,int y)
	//{
	//}
	public boolean mouseMove(Event evt, int x, int y)
	{
		// TODO: Place applet mouseMove code here
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseEnter() method is called if the mouse cursor enters the
	// applet's portion of the screen.
	//--------------------------------------------------------------------------
	public boolean mouseEnter(Event evt, int x, int y)
	{
		// TODO: Place applet mouseEnter code here
		return true;
	}

	// MOUSE SUPPORT:
	//		The mouseExit() method is called if the mouse cursor leaves the
	// applet's portion of the screen.
 	//--------------------------------------------------------------------------
	public boolean mouseExit(Event evt, int x, int y)
	{
		// TODO: Place applet mouseExit code here
		return true;
	}


	// TODO: Place additional applet code here

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?