advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JVMine


//******************************************************************************
// -----------------------------------------------------------
// JVMine.java		                 
// -----------------------------------------------------------
//   Main Applet class for Demineur JVMine Applet
// -----------------------------------------------------------
// Author : R. BERTHOU
// E-Mail : rbl@berthou.com
// URL    : http://www.javaside.com
// -----------------------------------------------------------
// Ver  * Author     *  DATE    * Description
// ....................DD/MM/YY...............................
// 1.02 * R. BERTHOU * 28/01/99 * Change mail address
// 1.01 * R. BERTHOU * 28/08/97 * Correct minor bug
// 1.00 * R. BERTHOU * 31/07/97 * Creation 
//******************************************************************************
import java.applet.Applet;
import java.util.Random ;
import java.awt.*;

import java.net.URL;
import java.net.MalformedURLException;

//==============================================================================
// Main Class
//==============================================================================
public class JVMine extends  java.applet.Applet
{
	//--------------------------------------------------------------------------
	private int[]	    bStr ;
	private int			iEtat ;
	private int			iCpt ;

	private	Image		buffer;
	private	Graphics	offScreen;
	private long		startTick ;

	private Image[]		img ;		

	private int aWidth, aHeight;

	private int		m_bgcolor = 0 ;
	public  int		m_mine = 99 ;
	public  int		m_ligne = 16 ;
	public  int		m_col = 30 ;
	private int     iMax ;

	// Font control variables
	private Font wFont;
	private FontMetrics wMetrics;
	private int	 iPas, iX ;

    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: JVMine V 1.02\r\n" +
		       "Author: R. BERTHOU\r\n" +
			   "E-Mail : rbl@berthou.com\r\n" +
			   "URL : http://www.javaside.com/" ;
	}

    //--------------------------------------------------------------------------
	public String[][] getParameterInfo()
	{
		String[][] info =
		{
			{ "bgImg",	"String",	"bgImage" },
			{ "bgcolor", "int",		"bgColor" },
			{ "nbmine",  "int",		"nb de mine" },
			{ "nbcol",	 "int",		"nb de col" },
			{ "nbligne", "int",		"nb de ligne" },
			{ "font",	"String",	"Font Name" },
			{ "fontsize", "int",	"Font Size" },
		};
		return info;		
	}


    //--------------------------------------------------------------------------
	// 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()
	{
		String param;

		try {
			aWidth = size().width;
			aHeight = size().height;
			buffer = createImage (aWidth, aHeight);
			offScreen = buffer.getGraphics ();
		} catch (Exception e) {
			offScreen = null;
		}

		// nbMine : nombre de mines
		//----------------------------------------------------------------------
		param = getParameter("nbmine");
		if (param != null)
			m_mine = Integer.parseInt(param);

		// nbcol : nombre de colones
		//----------------------------------------------------------------------
		param = getParameter("nbcol");
		if (param != null)
			m_col = Integer.parseInt(param);

		// nbligne : nombre de colones
		//----------------------------------------------------------------------
		param = getParameter("nbligne");
		if (param != null)
			m_ligne = Integer.parseInt(param);


		// bgcolor: Couleur du fond
		//----------------------------------------------------------------------
		param = getParameter("bgcolor");
		if (param != null)
			m_bgcolor = Integer.parseInt(param);

		MediaTracker  trk = new MediaTracker(this) ;

		// bImg: Bouton Image
		//----------------------------------------------------------------------
		param = getParameter("bImg");
		img = new Image[13];
		if (param != null) {
			for (int i = 0 ; i < 13 ; i++) {
		    	img[i] = getImage(getCodeBase(), param + (i) + ".gif");
				trk.addImage(img[i], 0) ;
			}
		}
		else
			for (int i = 0 ; i < 13 ; i++) 
		    	img[i] = null ;


		try	  { trk.waitForAll() ; }
		catch (InterruptedException e) {	}

		// Font
		//----------------------------------------------------------------------
		param = getParameter("fontsize");
		int iSize = 12 ;
		if (param != null)
			iSize = Integer.parseInt(param);

		param = getParameter("font");
		if (param == null)
			param = new String("Arial") ;

		wFont = new Font(param, Font.BOLD, iSize);
		if (wFont == null) wFont = getFont() ;

		wMetrics = getFontMetrics(wFont);

		iPas = wMetrics.getHeight() * 2 ;
		newGame() ;
			

  }
  //----------------------------------------------------------------------
  
  // start : 						  random
  //----------------------------------------------------------------------
  public void newGame() {
	    Random a ; 
		int i = 0 ;
		int j = 0 ;
		int iE ;
		a = new Random() ;
		iEtat = 0 ;
		iCpt = m_mine ;
		int k = 0 ;

		iMax = m_ligne*m_col ;
		bStr = new int[iMax] ;

		iX = (aWidth - (m_col * 15)) / 2 ;
		if ( (m_col * 15) > aWidth ) iX = 0 ;

		for (i = 0 ; i < iMax; i++) bStr[i] = 10 ;

		// calculate the tick to wait for
		startTick = -1 ;

		i = 0 ;
		while ( i < m_mine) {
			j = (int)(iMax * a.nextDouble()) ;
			if ( (j < iMax) && (bStr[j] != 19 ) ) {
				iE = j % m_col ;
				bStr[j] = 19 ;
				i++ ;

				if (iE > 0) { // pas la 1er colonne
					k = j-1-m_col ;	// -31
					if ( k >= 0 )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;
					k = j - 1 ;		// -1
					if ( k >= 0 )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;

					k = j+m_col-1 ; // +29
					if ( k < iMax )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;
				}

				k = j - m_col ;
				if ( k >= 0 )  
					if ( bStr[k] != 19 ) bStr[k] += 1 ;
				k = j + m_col ;
				if ( k < iMax )  
					if ( bStr[k] != 19 ) bStr[k] += 1 ;

				if (iE < (m_col-1)) {  // Pas la derniere
					k = j-m_col+1 ;		// -29
					if ( k >= 0 )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;
					k = j + m_col + 1 ; // +31
					if ( k < iMax )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;
					k = j + 1 ;			// +1
					if ( k < iMax )  
						if ( bStr[k] != 19 ) bStr[k] += 1 ;
				}

			}
		}

  		repaint();
  		
  }

  // start : 
  //----------------------------------------------------------------------
  public void start() {
  		repaint();
  }


  // mouseDown : 
  //----------------------------------------------------------------------
  public boolean mouseDown(Event e, int x, int y){
	  int iC = (x-iX) / 15 ;
	  int iL = (y-iPas) / 15 ;
	  boolean rep = false ;


	  if ( (x > ((aWidth/2)-20)) && (x < ((aWidth/2)+40)) &&
		   (y > (iPas/4))        && (y < (iPas*3/4) ) ) {
		  URL clickDest ;
	  	  try{
			clickDest = new URL("http://www.javaside.com/") ;
			getAppletContext().showDocument(clickDest, "rbl");
		  }catch(MalformedURLException mal){
			System.out.println("Bad URL!");
		  }
		  return(true) ;
	  }

	  if (iEtat == 1) {
		  newGame() ;
	      repaint() ;
		  return(true);
	  }

	  if ( (y > iPas) && (x > iX) && (x < aWidth - iX))
	  if ( (e.modifiers & Event.META_MASK) == Event.META_MASK) {
		  if ( bStr[(iL*m_col)+iC] > 9 ) {
			rep = true ;
			if ( bStr[(iL*m_col)+iC] < 20 ) {
				if (iCpt > 0) {
					bStr[(iL*m_col)+iC] += 10 ;
					iCpt-- ;
				}
				else
					showStatus("Erreur trop de mines...");
			}
			else {
					bStr[(iL*m_col)+iC] -= 10 ;
					iCpt++ ;
			}
		}
		else 
			rep = testCase((iL*m_col)+iC ) ;
		
	  }
	  else {
		  if ( bStr[(iL*m_col)+iC] > 9) {
				if (startTick == -1)
		  			startTick = System.currentTimeMillis() ;
				bStr[(iL*m_col)+iC] -= 10 ;
				rep = true ;
				if (bStr[(iL*m_col)+iC] == 9)
					iEtat = 1 ;
				if (bStr[(iL*m_col)+iC] == 0) 
					verifPos( (iL*m_col) + iC) ;
		  }
	  }

	  if (rep)  repaint() ;

	  return(true);
  }
  //----------------------------------------------------------------------

  public void verifPos(int j) {
		int iE = j % m_col ;
		int k ;

		if (iE > 0) {	// Pas la 1ere col
			k = j - m_col -1 ;	// -31
			if ( k >= 0 )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 
			k = j - 1 ;			// -1
			if ( k >= 0 )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 
			k = j + m_col - 1 ; // +29
			if ( k < iMax )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 
		}

		k = j - m_col ;
		if ( k >= 0 )  
			if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
				bStr[k] -= 10 ;
				if (bStr[k] == 0) verifPos( k ) ;
		    } 

		k = j + m_col ;
		if ( k < iMax )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 

		if (iE < (m_col-1)) {	// Pas la derniere
			k = j - m_col+1 ;	// -29
			if ( k >= 0 )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 

			k = j + m_col + 1 ;	// +31
			if ( k < iMax )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 

			k = j + 1 ;			// +1
			if ( k < iMax )  
				if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
					bStr[k] -= 10 ;
					if (bStr[k] == 0) verifPos( k ) ;
			    } 
		}

  }

  public boolean testCase(int j) {
		int iE = j % m_col ;
		int i = 0 ;
		int k ;

		if (iE > 0) {		// Pas la 1ere col
			k = j - m_col - 1 ;   // -31
			if ( k >= 0 )  
				if ( bStr[k] > 19 ) i++ ;
			k = j - 1 ;		// -1
			if ( k >= 0 )  
				if ( bStr[k] > 19 ) i++ ;
			k = j + m_col - 1 ;  // +29
			if ( k < iMax )  
				if ( bStr[k] > 19 ) i++ ;
		}

		k = j - m_col ;
		if ( k >= 0 )  
				if ( bStr[k] > 19 ) i++ ;
		
		k = j + m_col ;
		if ( k < iMax )  
				if ( bStr[k] > 19 ) i++ ;

		if (iE < (m_col-1) ) {
			k = j - m_col + 1 ;  // -29
			if ( k >= 0 )  
				if ( bStr[k] > 19 ) i++ ;
			k = j + m_col + 1 ;  // +31
			if ( k < iMax )  
				if ( bStr[k] > 19 ) i++ ;
			k = j + 1 ;			// +1
			if ( k < iMax )  
				if ( bStr[k] > 19 ) i++ ;
		}

		if (i == bStr[j])  {
			verifPos(j) ;
			return true ;
		}
		else
			return false ;

  }



  // paint :  Called when the applet needs to be painted
  //		  calls the flicker free updating system
  //----------------------------------------------------------------------
  public void paint (Graphics g) {
		update(g);
  } // end of paint
  //----------------------------------------------------------------------


  // update :  Draw the applet without flicker
  //----------------------------------------------------------------------
  public synchronized void update(Graphics g) {
		if (offScreen!=null) {
			paintApplet(offScreen);
			g.drawImage(buffer,0,0,this);
		} else
			paintApplet(g);
  } // end of update
  //----------------------------------------------------------------------

  // paintApplet :  Paint the applet into whatever image
  //----------------------------------------------------------------------
  public void paintApplet(Graphics g) {

	  int i = 0 ;
	  int j = 0 ;
	  int a = 0 ;
	  int k = 0 ;

	  g.setColor( new Color(m_bgcolor) );
	  g.fillRect(0, 0, aWidth, aHeight) ;

	  for (i = 0 ; i < m_ligne; i++) {
	     for (j = 0 ; j < m_col; j++) {
			 a = bStr[(i*m_col)+j] ;
			 if ( (iEtat == 0) && (a == 9) ) iEtat = 1 ; 
			 if (iEtat == 1) {
				 if (a == 19) a = 9 ; 
				 if ((a > 19) && (a != 29) ) {
					 iEtat = 1 ;
					 a = 41 ; 
				 }
			 }
				  
			 if (a > 40) a = 12 ; else
			 if (a > 19) a = 11 ; else
			 if (a > 9) { a = 10 ;	k++; }

			 if (img[a] != null)
				g.drawImage(img[a], iX+(j*15), iPas + (i*15) , this) ;
		 }
	  }


	  long thisTick ;

		// calculate the tick to wait for
	  if (startTick != -1)
		thisTick = (System.currentTimeMillis() - startTick ) / 1000;
	  else
		thisTick = 0 ;

	  g.setColor( new Color(0) );
	  g.fill3DRect(iPas,
	  			   iPas/4,
				   iPas,
				   iPas/2, 
				   true ) ;

	  g.fill3DRect(aWidth-iPas-iPas,
	  			   iPas/4,
				   iPas,
				   iPas/2, 
				   true ) ;

	  g.setFont( wFont );
	  g.setColor( Color.green );

	  g.drawString (" " + iCpt, iPas+5, iPas*2/3 ) ;
	  g.drawString (" " + thisTick, aWidth-iPas-iPas+5, iPas*2/3 ) ;
	  g.setColor( Color.black );
	  if (k == 0) {
		  iEtat = 1 ;
		  g.drawString (" " + iCpt, iPas+5, (iPas*2/3) ) ;
		  g.drawString ("Win", (aWidth/2)-5, (iPas*2/3) ) ;
	  }
	  else
		  if (iEtat == 1)
			 g.drawString ("Lose", (aWidth/2)-5, (iPas*2/3) ) ;
		  else {
			 g.setColor( new Color(0xC0C0C0) );
			 g.fill3DRect( (aWidth / 2)-20, iPas/4, 40, iPas/2, true) ; 
			 g.setColor( new Color(0) );
			 g.drawString ("?", (aWidth/2)-3, (iPas*2/3) ) ;
		  }


  }

}



Back to the JVMine applet page.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'

"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES