Java converting from JApplet to JFrame

I need to convert a TicTacToe game from JApplet to JFrame

Page 1 of 1

2 Replies - 6596 Views - Last Post: 22 February 2009 - 07:37 PM Rate Topic: -----

#1 Jrsarver  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 22-February 09

Java converting from JApplet to JFrame

Posted 22 February 2009 - 02:20 PM

I have code for a TicTacToe game using JApplet, that I need to convert to JFrame. I don't know how to use the Math.random to generate random x's and o's on the board without user input. Obviously the MouseListener is not needed for this. Following is my code. Sorry if it's too lengthy.

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


  public class TictacToe extends JApplet {
  // sets first as  X player
  private char whoseTurn = 'X';

  // Create  cells
  private Cell[][] cells =  new Cell[3][3];

  // Create and initialize a status label
  private JLabel jlblStatus = new JLabel("X's turn to play");

   public TictacToe() {
    // sets up grid
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        p.add(cells[i][j] = new Cell());

   //sets borders
    p.setBorder(new LineBorder(Color.red, 1));
    jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
    add(p, BorderLayout.CENTER);
    add(jlblStatus, BorderLayout.SOUTH);
  }


  public class Cell extends JPanel {
    // Token used for this cell
    private char token = ' ';

    public Cell() {
      setBorder(new LineBorder(Color.black, 1)); // border of cell
      addMouseListener(new MouseListener());  // alllows mouse to be used
    }

    /** Return token */
    public char getToken() {
      return token;
    }

    /** Set a new token */
    public void setToken(char c) {
      token = c;
      repaint();
    }

    /** colors cell */
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);

      if (token == 'X') {
        g.drawLine(10, 10, getWidth() - 10, getHeight() - 20);
        g.drawLine(getWidth() - 10, 10, 10, getHeight() - 20);
      }
      else if (token == 'O') {
        g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
      }
    }

    private class MouseListener extends MouseAdapter {
      /** tells if  mouse clicked on a cell */
      public void mouseClicked(MouseEvent e) {
        // If  empty game is not over
        if (token == ' ' && whoseTurn != ' ') {
          setToken(whoseTurn); // Set token in the cell

          //  game status
          if (isWon(whoseTurn)) {
            jlblStatus.setText(whoseTurn + " won! The game is over");
            whoseTurn = ' '; // Game is over
          }
          else if (isFull()) {
            jlblStatus.setText("Draw! The game is over");
            whoseTurn = ' '; // Game is over
          }
          else {
            whoseTurn = (whoseTurn == 'X') ? 'O': 'X'; // switches the turn
            jlblStatus.setText(whoseTurn + "'s turn");  // shows whose turn
          }
        }
      }
    }
  }

  /** checks to see if cell is empty */
  public boolean isFull() {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (cells[i][j].getToken() == ' ')
          return false;

    return true;
  }

  /** Determine who wins */
  public boolean isWon(char token) {
    for (int i = 0; i < 3; i++)
      if ((cells[i][0].getToken() == token)
          && (cells[i][1].getToken() == token)
          && (cells[i][2].getToken() == token)) {
        return true;
      }

    for (int j = 0; j < 3; j++)
      if ((cells[0][j].getToken() ==  token)
          && (cells[1][j].getToken() == token)
          && (cells[2][j].getToken() == token)) {
        return true;
      }

    if ((cells[0][0].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][2].getToken() == token)) {
      return true;
    }

    if ((cells[0][2].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][0].getToken() == token)) {
      return true;
    }

    return false;
 }
  
public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("TicTacToe");

    // Create new applet
    TictacToe applet = new TictacToe();

    // Add the new instance to the frame
    frame.add(applet, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 22 February 2009 - 02:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java converting from JApplet to JFrame

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Java converting from JApplet to JFrame

Posted 22 February 2009 - 02:28 PM

Here is the tutorial on it....

http://www.dreaminco...wtopic28410.htm

Cheers
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java converting from JApplet to JFrame

Posted 22 February 2009 - 07:37 PM

View PostJrsarver, on 22 Feb, 2009 - 01:20 PM, said:

I have code for a TicTacToe game using JApplet, that I need to convert to JFrame. I don't know how to use the Math.random to generate random x's and o's on the board without user input. Obviously the MouseListener is not needed for this. Following is my code. Sorry if it's too lengthy.

Math.random has nothing to do here neither the mouseListener they are the same for Applet or Application
follow markhazlett9 recommendations and it should work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1