Your Ad Here Your Ad Here
Results 1 to 4 of 4

Thread: Java Applet

  1. #1
    Poster
    Join Date
    Jan 2004
    Posts
    3,110
    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

    Code:
    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
      
      for(int i=0; i<numRows; i++)
      {
      	for(int j=0; j<numColumns; j++)
      	{
        mineGUI[i][j] = new EJButton(i, j); // Fills array with EJButtons
        mineGUI[i][j].setPreferredSize(new Dimension(30, 30)); // Sets buttons to 30x30 pixels
        mineGUI[i][j].addMouseListener(this); // Adds mouse listener to EJButtons
        mineGUI[i][j].setFlagged(false); // Sets the newly created EJButtons to be unflagged
        p1.add(mineGUI[i][j]); // Add newly created button to JPanel
      	}
      }
      
      menubar1 = new JMenuBar();
        	this.setJMenuBar(menubar1);
        	menu1 = new JMenu("Game");
        	beginner = new JMenuItem("Beginner");
        	beginner.addActionListener(this);
        	menu1.add(beginner);
        	amateur = new JMenuItem("Amateur");
        	amateur.addActionListener(this);
        	menu1.add(amateur);
        	expert = new JMenuItem("Expert");
        	expert.addActionListener(this);
        	menu1.add(expert);
        	menubar1.add(menu1);
      
         random1 = new mineRandomiser(numRows, numColumns, numMines); // Instance of the mineRandomiser class, passing in number of rows, columns and mines
      random1.mineRandom(); // Randomises mine position in the mineRandomiser class
      
      p2 = new JPanel();
      p2.setLayout(new GridLayout(1, 1));
      
      newGame = new JButton("New Game");
      newGame.addActionListener(this);
      quitButton = new JButton("Quit");
      quitButton.addActionListener(this);
      
            p2.add(newGame);
            p2.add(quitButton);
            
            p3 = new JPanel();
            p3.setLayout(new GridLayout(2, 1));
            
            numberFlags = new JLabel();
            numberFlags.setText("Flags: " +noFlags);
            
            revealGame = new JButton("Reveal Game");
            revealGame.addActionListener(this);
            
            gameResult = new JLabel("Win/Lose");
            
            p3.add(numberFlags);
            p3.add(revealGame);
            p3.add(gameResult);
      
      this.getContentPane().add("North", p3);
      this.getContentPane().add("Center", p1);
      this.getContentPane().add("South", p2);
      this.pack();
    	}
    	
    	public static void main(String args[])
        {
        	mineBoard board1 = new mineBoard("Board", 10, 10, 10); // Creates instance of GUI, with title "Board", 10 rows, 10 columns and 10 mines
        	board1.setLocation(100, 100);  // Sets the board position (100x100 pixels in from top left hand corner
        	board1.setVisible(true); // Shows the board
        	
        }
    
        public void mouseClicked(MouseEvent event)
        {
            EJButton button = (EJButton)event.getSource();
            int x = button.getxCo(); // Sets variable x to be the x co-ordinate of the clicked button
            int y = button.getyCo(); // Sets variable y to be y co-ordinate of clicked button
            
            if(event.getButton() == MouseEvent.BUTTON1) // If the user clicked with the left mouse button
            	leftClicked(x, y); // Pass x and y to the left clicked method
            else if(event.getButton() == MouseEvent.BUTTON3) // If the user clicked with right mouse button
            	rightClicked(x, y); // Pass y to right clicked method  	
        }
        public void mouseReleased(MouseEvent event){}
        public void mousePressed(MouseEvent event){}
        public void mouseExited(MouseEvent event){}
        public void mouseEntered(MouseEvent event){}
        
        public void leftClicked(int iPos3, int jPos3)
        {
        	if(!mineGUI[iPos3][jPos3].isFlagged()) // If the clicked button is not flagged
        	{
          mineGUI[iPos3][jPos3].setBorder(loweredbevel); // Set the clicked button to look pressed down
          if(random1.isMine(iPos3, jPos3)) // If the clicked button is a mine
          {
              revealMines(); // Show the position of all the mines
              for(int i=0; i<numRows; i++)
        	for(int j=0; j<numColumns; j++)
        	{
          mineGUI[i][j].setEnabled(false); // Set all buttons to be disabled
          mineGUI[i][j].removeMouseListener(this); // Remove mouselisteners from all buttons
          revealGame.removeActionListener(this); // Remove actionlistener from "Reveal Game" button
        	}
            	}
            	else // If the button clicked is not flagged
            	{
            	// Set the text on the button to be the number of mines around the clicked button
            	mineGUI[iPos3][jPos3].setText(String.valueOf(random1.countMines(iPos3, jPos3, numRows, numColumns)));
            	mineGUI[iPos3][jPos3].removeMouseListener(this); // Remove mouselistener from clicked button
            	mineGUI[iPos3][jPos3].setEnabled(false); // Set clicked button disabled
                }
            }
        }
       
        public void rightClicked(int iPos4, int jPos4)
        {
        	if(mineGUI[iPos4][jPos4].isFlagged) // If the clicked button is flagged
        	{
          mineGUI[iPos4][jPos4].setFlagged(false); // Set the clicked button to be not flagged
          mineGUI[iPos4][jPos4].setText(""); // Remove the "F" (denoting "Flag" from button
          noFlags++; // Increment the number of flags remaining by one
          numberFlags.setText("Flags remaining: " + noFlags); // Show the user the updated number of flags remaining
        	}
        	else
        	{
          mineGUI[iPos4][jPos4].setFlagged(true); // Set the clicked button to be flagged
          mineGUI[iPos4][jPos4].setText("F"); // Set clicked button text to "F" (signifying "Flagged")
          noFlags--; // Decrement number of flags remaining by one
          numberFlags.setText("Flags remaining: " + noFlags); // Show user num of flags left
            }
        }
        
    	public void actionPerformed(ActionEvent event)
    	{
      if(event.getSource() == newGame) // If the user clicks on the "New Game" button
      {
      	this.dispose(); // Get rid of the current instance of the GUI
      	random1 = new mineRandomiser(numRows, numColumns, numMines); // New instance of mineRandomiser class
      	random1.mineRandom(); // Randomise mine positions
      	mineBoard board1 = new mineBoard("Board", 10, 10, 100);
          board1.setLocation(100, 100);
          board1.setVisible(true);
      }
      else if(event.getSource() == quitButton) // If the user clicks on the "Quit" button
      {
      	this.dispose(); // Get rid of current instance of GUI
      	System.exit(0); // Exit the game
      }
      else if(event.getSource() == revealGame && noFlags == 0) // If user clicks on "Reveal game" button and there are no flags remaining
      {
      	if(reveal()) // If the user has placed all the flags correctly
        gameResult.setText("Congratulations"); // Tell them they've won
      	else // If the user hasn't set the flags correctly
        gameResult.setText("Bad luck, try again"); // Tell them they've lost
        
      	for(int i=0; i<numRows; i++)
        	for(int j=0; j<numColumns; j++)
        	{
          mineGUI[i][j].setEnabled(false); // Set all buttons disabled
          mineGUI[i][j].removeMouseListener(this); // Remove mouse listeners from all buttons
          revealGame.removeActionListener(this); // Remove actionlistener from "Reveal game" button
        	}	
      }
      else if(event.getSource() == beginner) // If the user clicks on "Beginner" game
      {
      	this.dispose(); // Get rid of current instance of GUI
      	random1 = new mineRandomiser(10, 10, 100); // New instance of MineRandomiser class
      	random1.mineRandom(); // Ramdomise mine position
      	mineBoard board1 = new mineBoard("Beginner Board", 10, 10, 100); // New instance of GUI
          board1.setLocation(75, 75);
          board1.setVisible(true);
        	}
    
        	else if(event.getSource() == amateur)
        	{
          this.dispose(); // As above (with different number of rows, columns, mines and a different title)
      	random1 = new mineRandomiser(15, 15, 50);
      	random1.mineRandom();
      	mineBoard board1 = new mineBoard("Amateur Board", 15, 15, 50);
          board1.setLocation(75, 75);
          board1.setVisible(true);
        	}
        	else if(event.getSource() == expert)
        	{
          this.dispose(); // As above...
      	random1 = new mineRandomiser(20, 20, 200);
      	random1.mineRandom();
      	mineBoard board1 = new mineBoard("Expert Board", 20, 20, 200);
          board1.setLocation(75, 75);
          board1.setVisible(true);
        	}
        }
    	
    	public boolean reveal()
    	{
      boolean winner = true; // Create a new boolean value called winner and set it true
      
      for(int i = 0; i < numRows; i++)
      {
      	for(int j = 0; j < numColumns; j++)
      	{
        // If button is not flagged and is a mine
        if(((EJButton) mineGUI[i][j]).isFlagged() && !random1.isMine(i, j))
        	winner = false; // Set boolean winner to false (i.e. game is lost)
      	}      	
      }
        
      return winner;
    	}
    	
    	public void revealMines()
    	{
      for(int i = 0; i < numRows; i++)
      {
      	for(int j = 0; j < numColumns; j++)
      	{
        if(random1.isMine(i, j))
        	mineGUI[i][j].setText("M"); // Print "M" on all mines
      	}
      }
        }	
    }
    Code:
    public class mineRandomiser
    {
    	int numRows;
    	int numColumns;
    	int numMines;
    	int [][] mineField;
    
    	public mineRandomiser()
    	{
      numRows = 0;
      numColumns = 0;
      numMines = 0;
    	}
    	
    	public mineRandomiser(int nR, int nC, int nM)
    	{
      numRows = nR;
      numColumns = nC;
      numMines = nM;
      mineField = new int [nR][nC];
        }
        
    	public void mineRandom()
    	{	
      for(int h = 0; h < numMines; h++) // Continue placing mines until the desired number has been reached
      {
          int i = 0, j = 0;
          i = (int) (Math.random()*numRows); // Generate random i (between 0 and end of row)
      	j = (int) (Math.random()*numColumns); // Generate random j (between 0 and end of column)
      	
      	while(mineField[i][j]==1) // While the randomly generated square is a mine
      	{
        i = (int) (Math.random()*numRows); // Generate a new square
           j = (int) (Math.random()*numColumns);
      	}
      	
      	mineField[i][j] = 1; // Set the random square to be 1 (i.e. make it a mine)
        System.out.println("("+i+", "+j+") is a mine"); // Print to system the position of mine
      }
    	}
    	
    	public boolean isMine(int iPos, int jPos)
    	{
      boolean mine = false;
      
      if(mineField[iPos][jPos]==1)
      	mine = true;
      	
      return mine;	
    	}
    	
    	public int countMines(int iClick, int jClick, int nRow, int nCol)
    	{  
      int numMines2 = 0;
      
      for(int iStart = iClick - 1; iStart <= iClick + 1; iStart++)
      {
      	for(int jStart = jClick - 1; jStart <= jClick + 1; jStart++)
      	{
        if(iStart >= 0 && iStart < nRow && jStart >= 0 && jStart < nCol)
        {
        	if(mineField[iStart][jStart] == 1)
        	{
          numMines2++;
        	}
        }
      	}
        
      }
      return numMines2;  
    	}
    }
    Code:
    import javax.swing.*;
    
    public class EJButton extends JButton
    {
    	protected int xCo, yCo;  
    	protected boolean isFlagged;
    	
    	public EJButton()
    	{
      xCo = 0;
      yCo = 0;
    	}
    	
    	public EJButton(int i, int j)
    	{
      xCo = i;
      yCo = j;
    	}
    	
    	public int getxCo()
    	{
      return xCo;
    	}
    	
    	public int getyCo()
    	{
      return yCo;
    	}
    	
    	public void setFlagged(boolean flagged)
    	{
      isFlagged = flagged;
    	}
    	
    	public boolean isFlagged()
    	{
      return isFlagged;
    	}
    }
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think "Ok, this is the limit".
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  2. Internet, Programming and Graphics   -   #2
    Ex-member
    Join Date
    Jan 2003
    Posts
    5,529
    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).

  3. Internet, Programming and Graphics   -   #3
    Poster
    Join Date
    Jan 2004
    Posts
    3,110
    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
    On a given day or given circumstance, you think you have a limit.
    And you then go for this limit and you touch this limit and you think "Ok, this is the limit".
    As soon as you touch this limit, something happens and you suddenly can go a little bit further.
    With your mind power, your determination, your instinct and the experience as well, you can fly very high.

    - Ayrton Senna, R.I.P.

  4. Internet, Programming and Graphics   -   #4
    Ex-member
    Join Date
    Jan 2003
    Posts
    5,529
    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&#39;s in Swing, anyone viewing it will need a v1.3 or later version of the JRE, meaning that they&#39;ll either need the full Java installation or the Java Plug-in (the M&#036; VM &#39;supports&#39; v1.1 only).
    Step-by-step guide for a n00b? Preferably with lots of colourful pictures [/b][/quote]
    Understanding Java Applets in general

    Understanding Swing Applets

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •