GigaNews
Threads in some sections have NO links for Guests, Register before you can view those threads.




PDA

View Full Version : Java Applet


4th gen
05-03-2004, 06:59 AM
Was just wondering how hard it'd be to make an applet out of this game? It's three classes (GUI, Extended JButton and randomiser class). Thanks in advance for any help/advice :)

It's pretty messy, sorry :(


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class mineBoard extends JFrame implements MouseListener, ActionListener
{
Border loweredbevel;    
mineRandomiser random1;
int numRows = 10;    
int numColumns = 10;
int numMines = 100;
EJButton [][] mineGUI;
int noFlags = numMines;
   protected JLabel numberFlags, numberFlags2, gameResult;
   JMenuBar menubar1;
   JMenuItem beginner, amateur, expert;
   JMenu menu1;
protected JPanel p1, p2, p3;
protected JButton newGame, quitButton, revealGame;

   public mineBoard(String title, int numberRows, int numberColumns, int numberMines)
{  
 numRows = numberRows; // Initialises the number of rows of buttons according to skill level selected
 numColumns = numberColumns; // Initalises number of columns according to skill level
 numMines = numberMines; // Initialises number of mines according to skill level
 
 this.setTitle(title); // Sets title of the board according to skill level
 p1 = new JPanel(); // New panel for displaying EJButtons
 p1.setLayout(new GridLayout(numRows, numColumns)); // Sets the layout of the Panel in a grid layout according to dimensions of grid

 mineGUI = new EJButton[numRows][numColumns]; // Creates an instance of the mineGUI with dimensions specified through skill level selected
 
&nbsp;for(int i=0; i<numRows; i++)
&nbsp;{
&nbsp; for(int j=0; j<numColumns; j++)
&nbsp; {
&nbsp; &nbsp;mineGUI[i][j] = new EJButton(i, j); // Fills array with EJButtons
&nbsp; &nbsp;mineGUI[i][j].setPreferredSize(new Dimension(30, 30)); // Sets buttons to 30x30 pixels
&nbsp; &nbsp;mineGUI[i][j].addMouseListener(this); // Adds mouse listener to EJButtons
&nbsp; &nbsp;mineGUI[i][j].setFlagged(false); // Sets the newly created EJButtons to be unflagged
&nbsp; &nbsp;p1.add(mineGUI[i][j]); // Add newly created button to JPanel
&nbsp; }
&nbsp;}
&nbsp;
&nbsp;menubar1 = new JMenuBar();
&nbsp; &nbsp; this.setJMenuBar(menubar1);
&nbsp; &nbsp; menu1 = new JMenu("Game");
&nbsp; &nbsp; beginner = new JMenuItem("Beginner");
&nbsp; &nbsp; beginner.addActionListener(this);
&nbsp; &nbsp; menu1.add(beginner);
&nbsp; &nbsp; amateur = new JMenuItem("Amateur");
&nbsp; &nbsp; amateur.addActionListener(this);
&nbsp; &nbsp; menu1.add(amateur);
&nbsp; &nbsp; expert = new JMenuItem("Expert");
&nbsp; &nbsp; expert.addActionListener(this);
&nbsp; &nbsp; menu1.add(expert);
&nbsp; &nbsp; menubar1.add(menu1);
&nbsp;
&nbsp; &nbsp; random1 = new mineRandomiser(numRows, numColumns, numMines); // Instance of the mineRandomiser class, passing in number of rows, columns and mines
&nbsp;random1.mineRandom(); // Randomises mine position in the mineRandomiser class
&nbsp;
&nbsp;p2 = new JPanel();
&nbsp;p2.setLayout(new GridLayout(1, 1));
&nbsp;
&nbsp;newGame = new JButton("New Game");
&nbsp;newGame.addActionListener(this);
&nbsp;quitButton = new JButton("Quit");
&nbsp;quitButton.addActionListener(this);
&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;p2.add(newGame);
&nbsp; &nbsp; &nbsp; &nbsp;p2.add(quitButton);
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;p3 = new JPanel();
&nbsp; &nbsp; &nbsp; &nbsp;p3.setLayout(new GridLayout(2, 1));
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;numberFlags = new JLabel();
&nbsp; &nbsp; &nbsp; &nbsp;numberFlags.setText("Flags: " +noFlags);
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;revealGame = new JButton("Reveal Game");
&nbsp; &nbsp; &nbsp; &nbsp;revealGame.addActionListener(this);
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;gameResult = new JLabel("Win/Lose");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;p3.add(numberFlags);
&nbsp; &nbsp; &nbsp; &nbsp;p3.add(revealGame);
&nbsp; &nbsp; &nbsp; &nbsp;p3.add(gameResult);
&nbsp;
&nbsp;this.getContentPane().add("North", p3);
&nbsp;this.getContentPane().add("Center", p1);
&nbsp;this.getContentPane().add("South", p2);
&nbsp;this.pack();
}

public static void main(String args[])
&nbsp; &nbsp;{
&nbsp; &nbsp; mineBoard board1 = new mineBoard("Board", 10, 10, 10); // Creates instance of GUI, with title "Board", 10 rows, 10 columns and 10 mines
&nbsp; &nbsp; board1.setLocation(100, 100); &nbsp;// Sets the board position (100x100 pixels in from top left hand corner
&nbsp; &nbsp; board1.setVisible(true); // Shows the board
&nbsp; &nbsp;
&nbsp; &nbsp;}

&nbsp; &nbsp;public void mouseClicked(MouseEvent event)
&nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;EJButton button = (EJButton)event.getSource();
&nbsp; &nbsp; &nbsp; &nbsp;int x = button.getxCo(); // Sets variable x to be the x co-ordinate of the clicked button
&nbsp; &nbsp; &nbsp; &nbsp;int y = button.getyCo(); // Sets variable y to be y co-ordinate of clicked button
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;if(event.getButton() == MouseEvent.BUTTON1) // If the user clicked with the left mouse button
&nbsp; &nbsp; &nbsp; &nbsp; leftClicked(x, y); // Pass x and y to the left clicked method
&nbsp; &nbsp; &nbsp; &nbsp;else if(event.getButton() == MouseEvent.BUTTON3) // If the user clicked with right mouse button
&nbsp; &nbsp; &nbsp; &nbsp; rightClicked(x, y); // Pass y to right clicked method &nbsp;
&nbsp; &nbsp;}
&nbsp; &nbsp;public void mouseReleased(MouseEvent event){}
&nbsp; &nbsp;public void mousePressed(MouseEvent event){}
&nbsp; &nbsp;public void mouseExited(MouseEvent event){}
&nbsp; &nbsp;public void mouseEntered(MouseEvent event){}
&nbsp; &nbsp;
&nbsp; &nbsp;public void leftClicked(int iPos3, int jPos3)
&nbsp; &nbsp;{
&nbsp; &nbsp; if(!mineGUI[iPos3][jPos3].isFlagged()) // If the clicked button is not flagged
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;mineGUI[iPos3][jPos3].setBorder(loweredbevel); // Set the clicked button to look pressed down
&nbsp; &nbsp; &nbsp;if(random1.isMine(iPos3, jPos3)) // If the clicked button is a mine
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;revealMines(); // Show the position of all the mines
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0; i<numRows; i++)
&nbsp; &nbsp; for(int j=0; j<numColumns; j++)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;mineGUI[i][j].setEnabled(false); // Set all buttons to be disabled
&nbsp; &nbsp; &nbsp;mineGUI[i][j].removeMouseListener(this); // Remove mouselisteners from all buttons
&nbsp; &nbsp; &nbsp;revealGame.removeActionListener(this); // Remove actionlistener from "Reveal Game" button
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else // If the button clicked is not flagged
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; // Set the text on the button to be the number of mines around the clicked button
&nbsp; &nbsp; &nbsp; &nbsp; mineGUI[iPos3][jPos3].setText(String.valueOf(random1.countMines(iPos3, jPos3, numRows, numColumns)));
&nbsp; &nbsp; &nbsp; &nbsp; mineGUI[iPos3][jPos3].removeMouseListener(this); // Remove mouselistener from clicked button
&nbsp; &nbsp; &nbsp; &nbsp; mineGUI[iPos3][jPos3].setEnabled(false); // Set clicked button disabled
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}
&nbsp;
&nbsp; &nbsp;public void rightClicked(int iPos4, int jPos4)
&nbsp; &nbsp;{
&nbsp; &nbsp; if(mineGUI[iPos4][jPos4].isFlagged) // If the clicked button is flagged
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;mineGUI[iPos4][jPos4].setFlagged(false); // Set the clicked button to be not flagged
&nbsp; &nbsp; &nbsp;mineGUI[iPos4][jPos4].setText(""); // Remove the "F" (denoting "Flag" from button
&nbsp; &nbsp; &nbsp;noFlags++; // Increment the number of flags remaining by one
&nbsp; &nbsp; &nbsp;numberFlags.setText("Flags remaining: " + noFlags); // Show the user the updated number of flags remaining
&nbsp; &nbsp; }
&nbsp; &nbsp; else
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;mineGUI[iPos4][jPos4].setFlagged(true); // Set the clicked button to be flagged
&nbsp; &nbsp; &nbsp;mineGUI[iPos4][jPos4].setText("F"); // Set clicked button text to "F" (signifying "Flagged")
&nbsp; &nbsp; &nbsp;noFlags--; // Decrement number of flags remaining by one
&nbsp; &nbsp; &nbsp;numberFlags.setText("Flags remaining: " + noFlags); // Show user num of flags left
&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp;}
&nbsp; &nbsp;
public void actionPerformed(ActionEvent event)
{
&nbsp;if(event.getSource() == newGame) // If the user clicks on the "New Game" button
&nbsp;{
&nbsp; this.dispose(); // Get rid of the current instance of the GUI
&nbsp; random1 = new mineRandomiser(numRows, numColumns, numMines); // New instance of mineRandomiser class
&nbsp; random1.mineRandom(); // Randomise mine positions
&nbsp; mineBoard board1 = new mineBoard("Board", 10, 10, 100);
&nbsp; &nbsp; &nbsp;board1.setLocation(100, 100);
&nbsp; &nbsp; &nbsp;board1.setVisible(true);
&nbsp;}
&nbsp;else if(event.getSource() == quitButton) // If the user clicks on the "Quit" button
&nbsp;{
&nbsp; this.dispose(); // Get rid of current instance of GUI
&nbsp; System.exit(0); // Exit the game
&nbsp;}
&nbsp;else if(event.getSource() == revealGame && noFlags == 0) // If user clicks on "Reveal game" button and there are no flags remaining
&nbsp;{
&nbsp; if(reveal()) // If the user has placed all the flags correctly
&nbsp; &nbsp;gameResult.setText("Congratulations"); // Tell them they've won
&nbsp; else // If the user hasn't set the flags correctly
&nbsp; &nbsp;gameResult.setText("Bad luck, try again"); // Tell them they've lost
&nbsp; &nbsp;
&nbsp; for(int i=0; i<numRows; i++)
&nbsp; &nbsp; for(int j=0; j<numColumns; j++)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;mineGUI[i][j].setEnabled(false); // Set all buttons disabled
&nbsp; &nbsp; &nbsp;mineGUI[i][j].removeMouseListener(this); // Remove mouse listeners from all buttons
&nbsp; &nbsp; &nbsp;revealGame.removeActionListener(this); // Remove actionlistener from "Reveal game" button
&nbsp; &nbsp; }
&nbsp;}
&nbsp;else if(event.getSource() == beginner) // If the user clicks on "Beginner" game
&nbsp;{
&nbsp; this.dispose(); // Get rid of current instance of GUI
&nbsp; random1 = new mineRandomiser(10, 10, 100); // New instance of MineRandomiser class
&nbsp; random1.mineRandom(); // Ramdomise mine position
&nbsp; mineBoard board1 = new mineBoard("Beginner Board", 10, 10, 100); // New instance of GUI
&nbsp; &nbsp; &nbsp;board1.setLocation(75, 75);
&nbsp; &nbsp; &nbsp;board1.setVisible(true);
&nbsp; &nbsp; }

&nbsp; &nbsp; else if(event.getSource() == amateur)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;this.dispose(); // As above (with different number of rows, columns, mines and a different title)
&nbsp; random1 = new mineRandomiser(15, 15, 50);
&nbsp; random1.mineRandom();
&nbsp; mineBoard board1 = new mineBoard("Amateur Board", 15, 15, 50);
&nbsp; &nbsp; &nbsp;board1.setLocation(75, 75);
&nbsp; &nbsp; &nbsp;board1.setVisible(true);
&nbsp; &nbsp; }
&nbsp; &nbsp; else if(event.getSource() == expert)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;this.dispose(); // As above...
&nbsp; random1 = new mineRandomiser(20, 20, 200);
&nbsp; random1.mineRandom();
&nbsp; mineBoard board1 = new mineBoard("Expert Board", 20, 20, 200);
&nbsp; &nbsp; &nbsp;board1.setLocation(75, 75);
&nbsp; &nbsp; &nbsp;board1.setVisible(true);
&nbsp; &nbsp; }
&nbsp; &nbsp;}

public boolean reveal()
{
&nbsp;boolean winner = true; // Create a new boolean value called winner and set it true
&nbsp;
&nbsp;for(int i = 0; i < numRows; i++)
&nbsp;{
&nbsp; for(int j = 0; j < numColumns; j++)
&nbsp; {
&nbsp; &nbsp;// If button is not flagged and is a mine
&nbsp; &nbsp;if(((EJButton) mineGUI[i][j]).isFlagged() && !random1.isMine(i, j))
&nbsp; &nbsp; winner = false; // Set boolean winner to false (i.e. game is lost)
&nbsp; } &nbsp; &nbsp; &nbsp;
&nbsp;}
&nbsp; &nbsp;
&nbsp;return winner;
}

public void revealMines()
{
&nbsp;for(int i = 0; i < numRows; i++)
&nbsp;{
&nbsp; for(int j = 0; j < numColumns; j++)
&nbsp; {
&nbsp; &nbsp;if(random1.isMine(i, j))
&nbsp; &nbsp; mineGUI[i][j].setText("M"); // Print "M" on all mines
&nbsp; }
&nbsp;}
&nbsp; &nbsp;}
}



public class mineRandomiser
{
int numRows;
int numColumns;
int numMines;
int [][] mineField;

public mineRandomiser()
{
&nbsp;numRows = 0;
&nbsp;numColumns = 0;
&nbsp;numMines = 0;
}

public mineRandomiser(int nR, int nC, int nM)
{
&nbsp;numRows = nR;
&nbsp;numColumns = nC;
&nbsp;numMines = nM;
&nbsp;mineField = new int [nR][nC];
&nbsp; &nbsp;}
&nbsp; &nbsp;
public void mineRandom()
{
&nbsp;for(int h = 0; h < numMines; h++) // Continue placing mines until the desired number has been reached
&nbsp;{
&nbsp; &nbsp; &nbsp;int i = 0, j = 0;
&nbsp; &nbsp; &nbsp;i = (int) (Math.random()*numRows); // Generate random i (between 0 and end of row)
&nbsp; j = (int) (Math.random()*numColumns); // Generate random j (between 0 and end of column)
&nbsp;
&nbsp; while(mineField[i][j]==1) // While the randomly generated square is a mine
&nbsp; {
&nbsp; &nbsp;i = (int) (Math.random()*numRows); // Generate a new square
&nbsp; &nbsp; &nbsp; j = (int) (Math.random()*numColumns);
&nbsp; }
&nbsp;
&nbsp; mineField[i][j] = 1; // Set the random square to be 1 (i.e. make it a mine)
&nbsp; &nbsp;System.out.println("("+i+", "+j+") is a mine"); // Print to system the position of mine
&nbsp;}
}

public boolean isMine(int iPos, int jPos)
{
&nbsp;boolean mine = false;
&nbsp;
&nbsp;if(mineField[iPos][jPos]==1)
&nbsp; mine = true;
&nbsp;
&nbsp;return mine;
}

public int countMines(int iClick, int jClick, int nRow, int nCol)
{ &nbsp;
&nbsp;int numMines2 = 0;
&nbsp;
&nbsp;for(int iStart = iClick - 1; iStart <= iClick + 1; iStart++)
&nbsp;{
&nbsp; for(int jStart = jClick - 1; jStart <= jClick + 1; jStart++)
&nbsp; {
&nbsp; &nbsp;if(iStart >= 0 && iStart < nRow && jStart >= 0 && jStart < nCol)
&nbsp; &nbsp;{
&nbsp; &nbsp; if(mineField[iStart][jStart] == 1)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp;numMines2++;
&nbsp; &nbsp; }
&nbsp; &nbsp;}
&nbsp; }
&nbsp; &nbsp;
&nbsp;}
&nbsp;return numMines2; &nbsp;
}
}



import javax.swing.*;

public class EJButton extends JButton
{
protected int xCo, yCo; &nbsp;
protected boolean isFlagged;

public EJButton()
{
&nbsp;xCo = 0;
&nbsp;yCo = 0;
}

public EJButton(int i, int j)
{
&nbsp;xCo = i;
&nbsp;yCo = j;
}

public int getxCo()
{
&nbsp;return xCo;
}

public int getyCo()
{
&nbsp;return yCo;
}

public void setFlagged(boolean flagged)
{
&nbsp;isFlagged = flagged;
}

public boolean isFlagged()
{
&nbsp;return isFlagged;
}
}

Lamsey
05-03-2004, 11:11 AM
Not too difficult but as it's in Swing, anyone viewing it will need a v1.3 or later version of the JRE, meaning that they'll either need the full Java installation or the Java Plug-in (the M$ VM 'supports' v1.1 only).

4th gen
05-03-2004, 11:14 AM
Originally posted by Lamsey@3 May 2004 - 10:11
Not too difficult but as it's in Swing, anyone viewing it will need a v1.3 or later version of the JRE, meaning that they'll either need the full Java installation or the Java Plug-in (the M$ VM 'supports' v1.1 only).
Step-by-step guide for a n00b? Preferably with lots of colourful pictures :rolleyes:

Lamsey
05-03-2004, 11:16 AM
Originally posted by 4th gen+3 May 2004 - 11:14--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (4th gen @ 3 May 2004 - 11:14)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-Lamsey@3 May 2004 - 10:11
Not too difficult but as it's in Swing, anyone viewing it will need a v1.3 or later version of the JRE, meaning that they'll either need the full Java installation or the Java Plug-in (the M$ VM 'supports' v1.1 only).
Step-by-step guide for a n00b? Preferably with lots of colourful pictures :rolleyes: [/b][/quote]
Understanding Java Applets in general (http://java.sun.com/docs/books/tutorial/applet/index.html)

Understanding Swing Applets (http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html)