1 Replies - 2129 Views - Last Post: 31 January 2013 - 07:17 AM Rate Topic: -----

#1 HausOf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-January 13

Eclipse: Start: Applet not initialized - help?

Posted 31 January 2013 - 07:03 AM

I'm new to programming with Java and using eclipse.

I have recently started to work with a Tic Tac Toe game in Java via Eclipse, however, when I attempt to run the code in an applet via the Eclipse programme, a white window loads up with the message: "Start: Applet not initialized" located in the bottom left of the screen.

Since I'm new to using both Eclipse and Java, I'm having trouble troubleshooting what the problem is, and I was wondering if someone could possibly point me in the right direction of the error?


Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;


public class TicTacToe extends JApplet implements ActionListener {
	
	JButton squares[];
	JButton newGameButton;
	JLabel score;
	JLabel wins;
	JLabel loses;
	JLabel ties;
	
	int emptySquaresLeft=9;
	int gamesWon=0;
	int gamesLost=0;
	int gamesTied=0;
	
	public static void main(String[] args){
		
		//.. Create and initialize the applet
		
		JApplet theApplet = new TicTacToe();
		theApplet.init(); //Needed if overridden in applet
		theApplet.start(); //Needed if overridden in applet
		
		//.. Create a window (JFrame) and make applet the content pane
		
		JFrame window = new JFrame ("Tic Tac Toe Applet and Application");
		window.setContentPane(theApplet);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Arrange the components
		window.setSize(300,300);
		window.setVisible(true); //Make the window visible
	}
	
	// ====================== Applet Constructor
	
	public TicTacToe() {
		add(new JLabel("This is both an Applet and Application"));
	}
	
	/**
	 * init mrthod is the applets constructor
	 */
	
	public void init(){
		//Get the applet's content pane - 
		//all window components go there
		
		Container appletContent = this.getContentPane();
		resize(300,300);
		
		//Set the pplet's layout manager, font and background color.
		appletContent.setLayout(new BorderLayout());
		appletContent.setBackground(Color.CYAN);
		
		//Create the button new Game and register it
		//with the action listener
		newGameButton=new JButton("New Game");
		newGameButton.addActionListener(this);
		wins = new JLabel("Won: " +gamesWon);
		loses=new JLabel("Lost: " +gamesLost);
		ties=new JLabel("Tied: "+gamesTied);
		
		JPanel topPanel = newJPanel();
		topPanel.setLayout(new GridLayout());
		topPanel.add(wins);
		topPanel.add(newGameButton);
		newGameButton.setHorizontalAlignment(JButton.CENTER);
		newGameButton.setSize (3,3);
		topPanel.add(loses);

		
		appletContent.add(topPanel, "North");
		
		JPanel centerPanel=new JPanel();
		centerPanel.setLayout(new GridLayout(3,3)); //FIX CODE
		appletContent.add(centerPanel, "Center");
		
		score=new JLabel("Your Turn!");
		appletContent.add(score, "South");
		
		// Create an array to hold references to 9 buttons
		squares=new JButton[9];
		
		//Instantiate the buttons, store the references
		// to them in the array, register them with the
		//listeners, paint them in orange and add to panel
		
		for(int i=0; i<9; i++){
			squares[i]=new JButton();
			//INSERT CODE HERE!!
			squares[i].setBackground(Color.ORANGE);
			centerPanel.add(squares[i]);
			
			// Create the frame and set its content pane
			
			JFrame frame = new JFrame("TicTacToe");
		}
	}
	
	/**
	 * This method will process all action events
	 * @param Action Event object
	 */
	
	public void actionPerformed(ActionEvent e){
		
		JButton theButton = (JButton) e.getSource();
		//Is this a New Game Button?
		
		if (theButton ==newGameButton){
			for(int i=0; i<9; i++){
				squares[i].setEnabled(true);
				squares[i].setText("");
				squares[i].setBackground(Color.ORANGE);
			}
			
			emptySquaresLeft=9;
			score.setText("Your Turn!");
			newGameButton.setEnabled(false);
			return; // EXIT THE METHOD HERE
		}
		
		String winner = "";
		//Is this one of the squares?
		
		for(int i=0; i<9; i++){
			if (theButton == squares[i]){
				squares[i].setText("X");
				//squares[i].setEnabled(false);
				squares[i].setEnabled(true);
				
				winner = lookForWinner;
				
				if(!"".equals(winner)){
					endTheGame();
					
				}else {
					computerMove();
					winner = lookForWinner();
					if (!"".equals(winner)){
						endTheGame();
					}
				}
				break;
			}
		} //END LOOP
		
		if (winner.equals("X")){
			score.setText("You Won!");
			gamesWon++;
			wins.setText(" Won: " +gamesWon);
		}else if (winner.equals("O")){
			score.setText("You Lost!");
			gamesLost++;
			loses.setText(" Lost: "+gamesLost);
		}else if (winner.equals("T")){
			gamesTied++;
			score.setText("It's a tie!");
		}
	} //END ACTIONPERFORMED
	
	/**
	 * This methid is called after every move to see
	 * if we have a winner.
	 * It checks every row, column and diagonal to find out
	 * three squares with the same label (other than blank)
	 * @return "X", "O", "T" for tie or "" for no winner
	 */
	
	String lookForWinner(){
		
		String theWinner = "";
		emptySquaresLeft--;
		
		if (emptySquaresLeft==0){
			
			return "T"; //It's a tie
		}
		
		//Check the row 1 - array elements 0,1,2
		
		if(!squares[0].getText().equals("") &&
				squares[0].getText().equals(squares[1].getText()) &&
				squares[0].getText().equals(squares[2].getText()) ) {
			
			theWinner = squares[0].getText();
			highlightWinner(0,1,2);
			
			//Check the row 2 - array elements 3,4,5
		}else if(!squares[3].getText().equals("") &&
				squares[3].getText().equals(squares[4].getText()) &&
				squares[3].getText().equals(squares[5].getText()) ) {
			
			theWinner = squares[3].getText();
			highlightWinner(3,4,5);
			
			//Check the row 3 - array elements 6,7,8
	}else if(!squares[6].getText().equals("") &&
			squares[6].getText().equals(squares[7].getText()) &&
			squares[6].getText().equals(squares[8].getText()) ) {
		
		theWinner = squares[6].getText();
		highlightWinner(6,7,8);
		
		//Check the column 1 - 0,3,6
	}else if(!squares[0].getText().equals("") &&
			squares[0].getText().equals(squares[3].getText()) &&
			squares[0].getText().equals(squares[6].getText()) ) {
		
		theWinner = squares[0].getText();
		highlightWinner(0,3,6);
		
		// Check the column 2 - array elements 1,4,7
	}else if(!squares[1].getText().equals("") &&
			squares[1].getText().equals(squares[4].getText()) &&
			squares[1].getText().equals(squares[7].getText()) ) {
		
		theWinner = squares[1].getText();
		highlightWinner(1,4,7);
		
		// Check the column 3 - 2,5,8
	}else if(!squares[2].getText().equals("") &&
			squares[2].getText().equals(squares[5].getText()) &&
			squares[2].getText().equals(squares[8].getText()) ) {
		
		theWinner = squares[2].getText();
		highlightWinner(2,5,8);
		
		//Check the first diagonal - array elements 0,4,8
	}else if(!squares[0].getText().equals("") &&
			squares[0].getText().equals(squares[4].getText()) &&
			squares[0].getText().equals(squares[8].getText()) ) {
		
		theWinner = squares[0].getText();
		highlightWinner(0,4,8);
		
		//Check the second diagonal - array elements 2,4,6
	}else if(!squares[2].getText().equals("") &&
			squares[2].getText().equals(squares[4].getText()) &&
			squares[2].getText().equals(squares[6].getText()) ) {
		
		theWinner = squares[2].getText();
		highlightWinner(2,4,6);
	}
		return theWinner;
}
	
	/**
	 * This method applies a set of rules to find
	 * the best computer's move. If a good move
	 * can't be found, it picks a random square.
*/
	
	void computerMove() {
		
		int selectedSquare;
		//Computer first tries to find an empty
		//square next to the two squares with "O" to win
		
		selectedSquare = findEmptySquare("O");
		
		//If can't find two "O", at least try to stop the
		//Opponent from making 3 in a row by placing
		//"O" next to 2 "X".
		
		if (selectedSquare == -1){
			selectedSquare = findEmptySquare("X");
		}
		//if the selectedSquare if still -1, at least
		//try to occupy the central square
		
		//FIX THIS!!
		if ( (selectedSquare == 0)&&(squares[4].getText().equals(""))){
			selectedSquare=4;
		}
		
		//No luck with the central either...
		//just get a random square
		if (selectedSquare == -1){
			selectedSquare = getRandomSquare();
		}
		
		squares[selectedSquare].setText("O");
		squares[selectedSquare].setEnabled(true);
	}
	
	/**
	*This method checks every row, column and diagonal
	*to see if there are two squares with the same label
	*and an empty square
	*@param give X - for the user, and O for the computer
	*@return the number of the empty square to use.
	*or the negative 1 coule not find 2 square
	*with the same label
	*/
	
	int findEmptySquare(String player){
		
		int weight[] = new int[9];
		
		for (int i=0; i<9; i++){
			if (squares[i].getText().equals("O"))
				weight[i] = -1;
			else if (squares[i].getText().equals("X"))
				weight[i] = 1;
				else
					weight[i] = 0;
		}
		
		int twoWeights = player.equals("O") ? -2 : 2;
		
		//See if row 1 has the same 2 squares and a blank
		if (weight[0] + weight[1] + weight[2] == twoWeights){
			if (weight[0] == 0)
				return 0;
			else if (weight[1] == 0)
				return 1;
			else
				return 2;
		}
		
		//See if row 2 has the same 2 squares and a blank
		if (weight[3] + weight[4] + weight[5] == twoWeights){
			if (weight[3] == 0)
				return 3;
			else if (weight[4] == 0)
				return 4;
			else
				return 5;
		}
		
		//See if row 3 has the same 2 squares and a blank
		if (weight[6] + weight[7] + weight[8] == twoWeights){
			if (weight[6] == 0)
				return 6;
			else if (weight[7] == 0)
				return 7;
			else
				return 8;
		}
		
		//See of column 2 has the same 2 squares and a blank
		
		if (weight[0] + weight[3] + weight[6] == twoWeights){
			if (weight[0] == 0)
				return 0;
			else if (weight[3] == 0)
				return 3;
			else
				return 6;
		}
		
		//See if column 2 has the same 2 squares and a blank
		if (weight[1] + weight[4] + weight[7] == twoWeights){
			if (weight[1] == 0)
				return 2;
			else if (weight[4] == 0)
				return 4;
			else
				return 7;
		}
		
		//See if column 3 has the same 2 squares and a blank
		if (weight[2] + weight[5] + weight[8] == twoWeights){
			if (weight[2] == 0)
				return 2;
			else if (weight[5] == 0)
				return 5;
			else
				return 8;
		}
		
		//See if diagonal 1 has the same 2 squares and a blank
		if (weight[0] + weight[4] + weight[8] == twoWeights){
			if (weight[0] == 0)
				return 0;
			else if (weight[4] == 0)
				return 4;
			else
				return 8;
		}
		
		//See if diagonal 2 has the same 2 squares and a blank
		if (weight[2] + weight[4] + weight[6] == twoWeights){
			if (weight[2] == 0)
				return 2;
			else if (weight[4] == 0)
				return 4;
			else
				return 6;
		}
		
		//Do not have the same two neightbours
		return -1;
	} //End of findEmptySpace
	
	/**
	*This method selects any empty square
	*@return a randomly selected square number
	*/
	
	int getRandomSquare(){
		boolean gotEmptySquare = false;
		int selectedSquare = -1;
		
		do {
			selectedSquare = (int) (Math.random()*9);
			if(squares[selectedSquare].getText().equals("")){
				gotEmptySquare = true; //LOOPING ENDS HERE!
			}
		}while(!gotEmptySquare);
		return selectedSquare;
	} //END RANDOM SQUARE SELECTION
	
	/**
	*This method highlights the winning line
	*@param first square to highlight
	*@param second square to highlight
	*@param third square to highlight
	*/
	
	void highlightWinner(int win1, int win2, int win3){
		squares[win1].setBackground(Color.CYAN);
		squares[win2].setBackground(Color.CYAN);
		squares[win3].setBackground(Color.CYAN);
	}
	
	/**
	*Disables squares and enable New Game Button
	*/
	
	void endTheGame(){
		for (int i=0; i<9; i++){
			squares[i].setEnabled(false);
		}
		//Enable New Game Button
		// newGameButton.setEnabled(true);
		
	}
}


Thanks in advance for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Eclipse: Start: Applet not initialized - help?

#2 HausOf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-January 13

Re: Eclipse: Start: Applet not initialized - help?

Posted 31 January 2013 - 07:17 AM

I have managed to fix this error!

newJPanel needed to be new JPanel - missed the space by accident.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1