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