Results 1 to 7 of 7
  1. #1
    gasper91 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Simple question on Embedding Java applets

    Hey,

    I'm trying to embed a java applet I made that is a connect four game made with one class. I understand that you need

    <APPLET CODE="ConnectFour.class" WIDTH="650" HEIGHT="400">
    </APPLET>

    and then inside the tags you need PARAM tags with parameters. What exactly do I need to put in for the parameters. Does this mean my private fields or what? I'm not sure what I'm doing here so if I'm lacking information than just tell me what you need and I'll do my best to let you know.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    See The APPLET Tag for more information on the applet tag and parameters.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    What exactly do I need to put in for the parameters
    The parameters can be read by the applet. See the applet method: getParameter??

    Leave the .class off the code= attrbute. The value is supposed to be the class name not a file name.

  4. #4
    gasper91 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Hmm, I'm still not completely getting it. Is it possible that my connect four game shouldn't take any parameters? Here is the code:

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.*;


    public class ConnectFour extends StudentTestableGraphicsProgram
    {
    //~ Instance/static variables .............................................
    //Game board dimensions
    private static final int GAME_BOARD_WIDTH = 400;
    private static final int GAME_BOARD_HEIGHT = 350;

    //static size of the ovals used in the array
    private static final int OVAL = 55;

    //Array Dimensions
    private static final int ROW = 6;
    private static final int COLUMN = 7;

    //Sets up the player's colors
    private static final Color ONE_COLOR = Color.RED;
    private static final Color TWO_COLOR = Color.BLUE;

    //Creates a new array used for the game baord
    private GOval[][] board = new GOval[ROW][COLUMN];

    //Title
    private GLabel connectFourTitle;

    //During the game it is either player 1's turn,
    //player 2's turn, or game over. So three
    //booleans are created.
    private boolean player1Turn;
    private boolean player2Turn;
    private boolean gameOver;

    //Stores colors that are used to
    //decide on a game winner
    private Color color1;
    private Color colorWin;
    private Color colorWin2;
    private Color colorWin3;
    private Color colorWin4;

    //Keeps a count to decide if there is a
    //game winner.
    private int sameColor;

    //Status label changes as players turns switch
    //and if the game is over.
    private GLabel statusLabel;

    //Initializes reset button
    private GRect resetRect;
    private GLabel resetLabel;
    //~ Methods .................................................. .............

    // ----------------------------------------------------------
    /**
    * Sets up the initial state of the application.
    * This method implements the various methods created.
    *
    */
    public void init()
    {
    addMouseListeners();

    //Basis initializations.
    player1Turn = true;
    player2Turn = false;
    gameOver = false;

    //Displays game board and title.
    gameBoard();
    gameTitle();

    //Initializes the game status.
    statusLabel = new GLabel("Red's turn", 475, 250);
    statusLabel.setFont("Arial-bold-34");
    statusLabel.setColor(ONE_COLOR);
    add(statusLabel);

    //Sets up reset button
    resetRect = new GRect(480, 300, 138, 85);
    resetRect.setColor(Color.BLACK);
    resetRect.setFilled(true);
    resetRect.setFillColor(Color.GREEN);
    add(resetRect);

    resetLabel = new GLabel("RESET", 485, 350);
    resetLabel.setColor(Color.BLACK);
    resetLabel.setFont("Arial-bold-38");
    add(resetLabel);

    }

    // ----------------------------------------------------------
    /**
    * Places a game piece in the appropriate spot.
    * @param e An object representing the mouse click event being handled
    */
    public void mouseClicked(MouseEvent e)
    {
    //Only the column needs to be read when placing
    //a game piece
    int columnClick = (e.getX() - 82) / OVAL;

    //Used for RESET button
    int x = e.getX();
    int y = e.getY();
    GObject resetClicked = getElementAt(x, y);

    //Calls resetGame method to clear board.
    if ((resetClicked == resetRect) || (resetClicked == resetLabel))
    {
    resetGame();
    }

    //Determines where a game piece you be placed,
    //calls the method to check for a win, and
    //determines what the color the game piece should
    //be.
    if (player1Turn)
    {
    for (int row = 5; row >= 0; row--)
    {
    GOval drop = board[row][columnClick];
    color1 = drop.getFillColor();

    if (color1.equals(Color.WHITE))
    {
    drop.setFillColor(ONE_COLOR);
    player1Turn = false;
    player2Turn = true;
    statusLabel.setLabel("Blue's Turn");
    statusLabel.setColor(TWO_COLOR);
    if (checkWin(ONE_COLOR))
    {
    gameOver = true;
    statusLabel.setLabel("Red wins");
    statusLabel.setColor(ONE_COLOR);
    break;
    }
    break;
    }
    }
    }
    else if (player2Turn)
    {
    for (int row = 5; row >= 0; row--)
    {
    GOval drop = board[row][columnClick];
    color1 = drop.getFillColor();
    if (color1.equals(Color.WHITE))
    {
    drop.setFillColor(TWO_COLOR);
    player1Turn = true;
    player2Turn = false;
    statusLabel.setLabel("Red's Turn");
    statusLabel.setColor(ONE_COLOR);
    if (checkWin(TWO_COLOR))
    {
    gameOver = true;
    statusLabel.setLabel("Blue wins");
    statusLabel.setColor(TWO_COLOR);
    break;
    }
    break;
    }
    }
    }

    //Makes it so it is "no ones turn" if the game
    //is over that way no more moves can be made.
    if (gameOver)
    {
    player1Turn = false;
    player2Turn = false;
    }
    }

    /**
    * This method sets up the game board.
    * @return board returns the game board
    *
    */
    public GOval[][] gameBoard()
    {
    //Large yellow rectangle.
    GRect rect = new GRect(75, 75, GAME_BOARD_WIDTH, GAME_BOARD_HEIGHT);
    rect.setFilled(true);
    rect.setColor(Color.BLACK);
    rect.setFillColor(Color.YELLOW);
    add(rect);

    //Creates an array of white ovals to symbolize the slots.
    for (int row = 0; row < ROW; row++)
    {
    for (int column = 0; column < COLUMN; column++)
    {
    int x = column * OVAL + 75;
    int y = row * OVAL + 75;

    GOval oval = new GOval(x + 7, y + 7, OVAL - 7, OVAL - 7);
    oval.setFilled(true);
    oval.setColor(Color.BLACK);
    oval.setFillColor(Color.WHITE);
    add(oval);

    board[row][column] = oval;
    }
    }

    return board;

    }
    /**
    * Places a game title label.
    * @return connectFour returns label.
    */
    public GLabel gameTitle()
    {
    connectFourTitle = new GLabel("CONNECT FOUR", 140, 60);
    connectFourTitle.setFont("Arial-bold-34");
    add(connectFourTitle);

    return connectFourTitle;
    }

    /**
    * Checks to see if a player won.
    * @param color enters a color to be checked for.
    * @return true false winner or no winner.
    */
    public boolean checkWin(Color color)
    {
    //Check for vertical win.
    for (int row = 0; row < ROW; row++)
    {
    sameColor = 0;
    for (int col = 0; col < COLUMN; col++)
    {
    colorWin = board[row][col].getFillColor();
    if (colorWin.equals(color))
    {
    sameColor++;
    if (sameColor == 4)
    {
    return true;
    }
    }
    else
    {
    sameColor = 0;
    }
    }
    }

    //Check for horizontal win.
    for (int col = 0; col < COLUMN; col++)
    {
    sameColor = 0;
    for (int row = 0; row < ROW; row++)
    {
    colorWin = board[row][col].getFillColor();
    if (colorWin.equals(color))
    {
    sameColor++;
    if (sameColor == 4)
    {
    return true;
    }
    }
    else
    {
    sameColor = 0;
    }
    }
    }
    //Check diagonal win with downward slope.
    for (int row = 0; row < 3; row++)
    {
    for (int col = 0; col < 4; col++)
    {
    colorWin = board[row][col].getFillColor();
    colorWin2 = board[row + 1][col + 1].getFillColor();
    colorWin3 = board[row + 2][col + 2].getFillColor();
    colorWin4 = board[row + 3][col + 3].getFillColor();
    if (colorWin.equals(color) && colorWin2.equals(color) &&
    colorWin3.equals(color) && colorWin4.equals(color))
    {
    return true;
    }
    }
    }
    //Check diagnoal win with positive slope.
    for (int row = 3; row < 6; row++)
    {
    for (int col = 0; col < 4; col++)
    {
    colorWin = board[row][col].getFillColor();
    colorWin2 = board[row - 1][col + 1].getFillColor();
    colorWin3 = board[row - 2][col + 2].getFillColor();
    colorWin4 = board[row - 3][col + 3].getFillColor();
    if (colorWin.equals(color) && colorWin2.equals(color) &&
    colorWin3.equals(color) && colorWin4.equals(color))
    {
    return true;
    }
    }
    }

    return false;

    }

    /**
    * Resets the game.
    */
    public void resetGame()
    {
    gameBoard();
    statusLabel.setLabel("Red's Turn");
    statusLabel.setColor(Color.RED);
    gameOver = false;
    player1Turn = true;
    }

    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    Is it possible that my connect four game shouldn't take any parameters
    yes. Parameters allow you to tailor your program for a particular environment or execution. If there is nothing you need to tell your code about where it is or what it is to do, you don't need parameters.

  6. #6
    gasper91 is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    java.lang.NoClassDefFoundError: acm/program/StudentTestableGraphicsProgram
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: acm.program.StudentTestableGraphicsProgram
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more



    This is what I'm getting, the java thing pops up when I try to run the applet online, but after the loading bar starts I get this error.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default

    NoClassDefFoundError: acm/program/StudentTestableGraphicsProgram
    The java program can not find the class referenced in the message.
    You need to add the jar file that the class is in to the classpath for the java command:

    java -classpath THEJARFILEHERE.jar;. YOURCLASSNAMEHERE

    That was for programs.

    For applets, add the jar file to the <APPLET tag's archive= attribute

Similar Threads

  1. Question about a simple Java programing assignment.
    By ron_j_m in forum New To Java
    Replies: 12
    Last Post: 04-27-2011, 12:03 AM
  2. Replies: 2
    Last Post: 03-18-2010, 12:39 PM
  3. Replies: 8
    Last Post: 03-14-2010, 07:53 PM
  4. Applets and serial ports question
    By kenshinofkin in forum Java Applets
    Replies: 1
    Last Post: 02-26-2009, 11:30 PM
  5. Replies: 7
    Last Post: 10-06-2008, 04:01 PM

Tags for this Thread

Posting Permissions

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