I have created a very simple Tic Tac Toe game using NetBeans 6.8. Swing is used in order to create the grid. I can build and run the game with no problem.

What I now want to do, is have this uploaded onto a website (along with other applications I will make). I am struggling on how to do this though. At uni, we was taught to code applications but wasn't taught how to get them out to the public.

I did try uploading the jar file and running it as an applet but that didn't work.


Could anybody offer some help here? Or is this just not possible?


I may be popping out in a bit so sorry if I don't reply to any suggestions for a while.

Applets are a whole other ball game. Check out this though: http://java.sun.com/docs/books/tutorial/deployment/webstart/

I'm using OSX so this isn't an option for me as far as I can see.

I have found out that I need to convert the code to an applet with a few simple changes but I still don't seem to be able to get it to work in the web browser.

My Code:

package myPackage;

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

public class TicTacToe extends JApplet implements ActionListener {

    //Variables
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton buttons[] = new JButton[9];
    private String letter = "";
    private int count = 0;
    private boolean win = false;

    private int[][] winCombinations = new int[][] {
	{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
	{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
	{0, 4, 8}, {2, 4, 6}             //diagonal wins
    };


    public TicTacToe() {

        //Create window
        window.setSize(300,300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(3,3));

        //Add buttons & action listeners
        for(int i=0; i<=8; i++){
            buttons[i] = new JButton();
            window.add(buttons[i]);
            buttons[i].addActionListener(this);
        }

        //Make window visible
        window.setVisible(true);

    }

    public void actionPerformed(ActionEvent a) {

        count++;

        //See who's turn it is
        if(count % 2 == 0){
            letter = "O";
        } else {
            letter = "X";
        }

        //Display X's or O's when pressed
        JButton pressedButton = (JButton)a.getSource();
        pressedButton.setText(letter);
        pressedButton.setEnabled(false);

        //Determine who won
	//horizontal wins
	for(int i=0; i<=7; i++){
	if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
		buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
		buttons[winCombinations[i][0]].getText() != ""){
		win = true;
	}
        }

	//Show a popup message if someone wins or the game is tie
	if(win == true){
		JOptionPane.showMessageDialog(null, letter + " WINS!");
	} else if(count == 9 && win == false){
		JOptionPane.showMessageDialog(null, "Tie Game!");
	}

    }

    public void init(){

        new TicTacToe();
    }

 }

So I have that and I am trying to call it but i'm not exactly sure how I go about this. This is what I have but it just displays an error message which says "click for details" but then there is no details.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>

        <applet code="TicTacToe.class" height="300" width="300"></applet> 
        
    </body>
</html>

Does this mean anything?

Java Plug-in 1.6.0_17
Using JRE version 1.6.0_17-b04-248-10M3025 Java HotSpot(TM) Client VM
User home directory = /Users/Scott

It's what I see when I click for details when the error message shows. I've tried a few other applets that I have downloaded off the net and its the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.