CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2005
    Posts
    3

    Applet Freezing on Refresh

    Hi,
    I am completely new Java Applets.
    I was writing a Othello Game Search for my school project. Then I decided to write an GUI for it using Java Applets. I use Eclipse Java Environment. And the the viewer was set to Sun Applet Viewer. Everything about the applet works accordingly. But in the applet viewer, if I click "Restart/Reload" the applet will freeze. I tried this on a browser. If I quit the site and come back later, the applet freezes. If I click refresh, the applet freezes.

    I did some googling and I saw some things about the stop() and destroy() browser / applet viewer calls. Is that what my problem is? If so, how do I write these destroy() and stop() functions?

    Thanks.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Applet Freezing on Refresh

    Hello!
    Can you perhaps include the source code ¿

    BTW: your username makes me extremely hungry!

  3. #3
    Join Date
    Nov 2005
    Posts
    3

    Re: Applet Freezing on Refresh

    package reversi;

    import java.util.Random;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;

    public class ReversiGame extends Applet implements ActionListener {
    public static int turn = 1; // alpha starts the game
    public static ReversiBoard myBoard; // main reversi board
    public static GameState presentState;
    public GameState [] nextStates;
    public static int movesLeft = 60;
    public CoordinatePair bMove = new CoordinatePair(-1,-1);
    public static int stuckCounter = 0;
    public static int branches = 0;
    public static int nodes = 1;
    static JFrame frame = new JFrame("PReversi");
    static JButton [][] myButton = new JButton [8][8] ;
    static JLabel bLabel = new JLabel();
    static JLabel wLabel = new JLabel();
    static JLabel tLabel = new JLabel();


    /*
    * Main Reversi Game
    */
    public void init(){
    myBoard = new ReversiBoard();
    presentState = new GameState(myBoard, turn);
    int xs, ys;
    createAndShowGUI();
    }

    /*
    * By using parallel arrays
    * The code SHOULD return a best move even if ALL moves have same
    * utility, if that's the case, first move checked is returned.
    */

    public static void createAndShowGUI(){
    Container fContent = frame.getContentPane();
    frame.setSize(350,450);
    frame.setResizable(false);

    // fContent Setup
    fContent.setBackground(new Color(148,176,215));
    fContent.setLayout(new BoxLayout(fContent, BoxLayout.PAGE_AXIS));

    // Menu Buttons
    Container menu = new Container();
    JLabel title = new JLabel("PReversi 1.0*************");
    menu.setLayout(new BoxLayout(menu, BoxLayout.LINE_AXIS));
    JButton newGame = new JButton("New Game");
    newGame.addActionListener(new ActionListener( ) {
    public void actionPerformed(ActionEvent e) {
    myBoard = new ReversiBoard();
    presentState = new GameState(myBoard, turn);
    presentState.getMoves();
    reRender();
    reRender();
    }
    });
    menu.add(title);
    menu.add(newGame);
    fContent.add(menu);


    // the Board
    Container tBoard = new Container();
    tBoard.setLayout(new GridLayout(0,8));
    fContent.add(tBoard);

    // Create a Label
    JButton mButton;
    for(int i = 1; i < 9; i++){
    for(int j = 1; j < 9; j++){
    if(presentState.rb.board[i-1][j-1]==1){
    mButton = new JButton("O");
    }
    else if(presentState.rb.board[i-1][j-1]==-1){
    mButton = new JButton("X");
    }
    else{
    mButton = new JButton("");
    }
    myButton[i-1][j-1] = mButton;
    mButton.setActionCommand(""+i+j);
    mButton.addActionListener(new ActionListener( ) {
    public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    int num = Integer.parseInt(command);
    int x = num / 10;
    int y = num % 10;
    // Perform Move
    performMove(x,y);
    reRender();
    // System.out.println("x = " + x + ", y = " + y);
    }
    });

    mButton.setBackground(Color.white);
    tBoard.add(mButton);
    }
    }

    presentState.getMoves();
    reRender();

    fContent.add(wLabel);
    fContent.add(bLabel);
    wLabel.setText("O Pieces : 2");
    bLabel.setText("X Pieces : 2");

    tLabel.setText("Turn: ");
    frame.setVisible(true);
    frame.requestFocus();
    }

    public static boolean performMove(int x, int y){
    if(presentState.moves.size()!=0){
    if(presentState.validMove(x, y, 1)){
    myBoard.doMove(x,y,1);
    stuckCounter=0;
    presentState = new GameState(myBoard,-1);
    computerMove();
    return true;
    }
    else{
    stuckCounter++;
    }
    }
    else{
    stuckCounter++;
    presentState = new GameState(myBoard,-1);
    computerMove();
    }
    return false;
    }

    public static void computerMove(){
    int xs, ys;
    CoordinatePair bMove = new CoordinatePair(-1,-1);
    // Computer Move
    presentState.getMoves();
    presentState.makeNextStates();
    presentState.makeNext2States();

    // Computer Choose Move
    if(presentState.moves.size()!=0){
    bMove = getBetaMove(presentState);
    xs = bMove.x;
    ys = bMove.y;
    myBoard.doMove(xs,ys,-1);
    stuckCounter=0;
    }
    else{
    stuckCounter++;
    }

    presentState = new GameState(myBoard,1);
    presentState.getMoves();
    }

    public static void reRender(){
    for(int i = 1; i < 9; i++){
    for(int j = 1; j < 9; j++){
    myButton[i-1][j-1].setBackground(Color.white);
    if(myBoard.board[i-1][j-1]==1){
    myButton[i-1][j-1].setText("O");
    myButton[i-1][j-1].setBackground(new Color(0,255,0));
    }
    else if(myBoard.board[i-1][j-1]==-1){
    myButton[i-1][j-1].setText("X");
    myButton[i-1][j-1].setBackground(new Color(255,0,0));
    }
    else{
    myButton[i-1][j-1].setText("");
    for(int k = 0; k < presentState.moves.size(); k++){
    if(presentState.moves.elementAt(k).x == i && presentState.moves.elementAt(k).y == j){
    myButton[i-1][j-1].setBackground(new Color(195,195,195));
    }
    }
    }
    }
    }

    wLabel.setText("O Pieces : " + myBoard.alphaPieces());
    bLabel.setText("X Pieces : " + myBoard.betaPieces());
    }

    public void actionPerformed(ActionEvent e){
    System.out.println(e.getActionCommand());
    }

    public static CoordinatePair getMiniMax(GameState s){
    // Array of nextStates
    int [] nMin = new int [s.nextStates.length];
    int currentMax = -1000;
    int bestIndex = 0;
    CoordinatePair bestMove = new CoordinatePair(-1,-1);
    GameState [] minStates;
    int u;
    for(int i = 0; i < s.nextStates.length;i++){
    minStates = s.nextStates[i].nextStates;
    nMin[i] = 1000;
    branches++;
    nodes++;
    for(int j = 0; j < minStates.length; j++){
    // calls computeUtility here
    u = minStates[j].computeUtility(turn);
    if( u < nMin[i] ){
    nMin[i] = u;
    bestIndex = j;
    }
    if( j != 0){ // Check for Prun-ability
    if ( u < currentMax){
    break;
    }
    }
    branches++;
    }
    if(nMin[i] > currentMax){
    // Best subtree max found
    currentMax = nMin[i];
    bestMove.x = s.moves.elementAt(i).x;
    bestMove.y = s.moves.elementAt(i).y;
    }

    }
    return bestMove;
    }

    /*
    * Allows a player to search the first available found move
    */
    public CoordinatePair getFirstMove(GameState s){
    CoordinatePair firstMove = new CoordinatePair(-1,-1);
    firstMove.x = s.moves.elementAt(0).x;
    firstMove.y = s.moves.elementAt(0).y;
    return firstMove;
    }

    /*
    * Allows a player to play by randomly chosen moves
    */
    public CoordinatePair getRandomMove(GameState s){
    CoordinatePair randomMove = new CoordinatePair(-1,-1);
    Random rMove = new Random();
    int ceiling = s.moves.size();
    int result = rMove.nextInt(ceiling);
    randomMove.x = s.moves.elementAt(result).x;
    randomMove.y = s.moves.elementAt(result).y;
    return randomMove;

    }

    /*
    * Alpha-Beta Code fix, this was later added in to meet the criteria
    * of alpha-beta gameplaying.
    */
    public static CoordinatePair getBetaMove(GameState s){
    int [] nMin = new int [s.nextStates.length];
    int min = 1000;
    int utility = 0;
    CoordinatePair bestMove = new CoordinatePair(-1,-1);
    for(int i = 0; i < s.nextStates.length;i++){
    utility = s.nextStates[i].computeUtility(turn);
    if(utility < min){
    min = utility;
    bestMove.x = s.moves.elementAt(i).x;
    bestMove.y = s.moves.elementAt(i).y;
    }
    }
    return bestMove;
    }

    public void stop(){
    frame.removeAll();
    System.exit(0);
    }

    public void destroy(){
    frame.removeAll();
    System.exit(0);
    }


    }

  4. #4
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: Applet Freezing on Refresh

    Use JApplet instead of Applet. Don't mix top level awt components with swing components. Just use swing.

    Other than that, I would drop the overrides for destroy, stop, etc. Those are to be overridden to handle any clean up because those methods are called by the browser like when you hit the stop button or the refresh button.

    Also, dump the static references, that seems like a holdover from a netbeans frame skeleton (you converted this from a frame class). Study this design pattern that netbeans creates as it is useful for general testing of components. The idea behind this is that you would implement your component/application on a JPanel instead of directly extending a JApplet or JFrame. Then in the JPanel main method (every class can have a main method), you create a temp JFrame, create an instance of the JPanel, and pass that ContentPane to the JFrame for it to use (JFrame.setContentPane()). The create and show gui method is used to ensure that the thread is able to recieve system events basically. But the point is that if you create your widgets from JPanel then it is trivial to add them to a JFrame or JApplet.
    "The Chicken and Rice MRE is not a personal lubricant."

  5. #5
    Join Date
    Nov 2005
    Posts
    3

    Re: Applet Freezing on Refresh

    This used to be an ascii engine.
    I implemented the Applet afterwards, and when I did, it Eclipse started bugging me with the "call to non-static" references. So I started adding the static references.

    Am I to understand that Swing is different than AWT?

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured