PDA

View Full Version : One Problem with Javax.Swing



Saberwing
01-22-2010, 04:54 AM
I dont understand why my first line is an error, Javax.Swing.

It's late in the night and I am stressed because this is really irritating me.

Prehaps I am over looking someing realy simple or the problem is related to other parts of my Code?

Here is the whole code just incase the latter is true

BTW it's a connect four (applet template) game :thumbsup:


import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class ConnectFourGUI2
{
private JFrame frame;
private JLabel[] [] slots;
private int currentPlayer;
public ConnectFourGUI ()
{
frame = new JFrame ("Connect Four");
JPanel panel = (JPanel) frame.getContentPane ();
panel.setLayout (new GridLayout (6, 7));
slots = new JLabel [7] [6];
for (int row = 5 ; row >= 0 ; row--)
{
for (int column = 0 ; column < 7 ; column++)
{
slots [column] [row] = new JLabel ();
slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18));
slots [column] [row].setHorizontalAlignment (SwingConstants.CENTER);
slots [column] [row].setBorder (new LineBorder (Color.green));
panel.add (slots [column] [row]);
}
}
frame.setContentPane (panel);
frame.setSize (700, 600);
frame.setVisible (true);
currentPlayer = 1;
}


public void addListener (ConnectFourListener listener)
{
for (int row = 0 ; row < 6 ; row++)
{
for (int column = 0 ; column < 7 ; column++)
{
slots [column] [row].addMouseListener (listener);
}
}
}


public int getColumn (JLabel label)
{
int returnColumn = -1;
for (int row = 0 ; row < 6 ; row++)
{
for (int column = 0 ; column < 7 ; column++)
{
if (slots [column] [row] == label)
{
returnColumn = column;
}
}
}
return returnColumn;
}


public void set (int column, int row)
{
// slots[column][row].setText("*" + currentPlayer + "*");
if (currentPlayer == 1)
{
slots [column] [row].setIcon (new ImageIcon (C/Users/Quinton/Desktop/Connect4/Slot.jpeg));
}
else
{
slots [column] [row].setIcon (new ImageIcon (C/Users/Quinton/Desktop/Connect4/Slot.jpeg));
}
currentPlayer = (currentPlayer % 2) + 1;
}


public static void main (String[] args)
{
ConnectFour game = new ConnectFour ();
ConnectFourGUI gui = new ConnectFourGUI ();
ConnectFourListener listener = new ConnectFourListener (game, gui);
}
}

import javax.swing.*;
import java.awt.event.*;
public class ConnectFourListener implements MouseListener
{
ConnectFourGUI gui;
ConnectFour game;
public ConnectFourListener (ConnectFour game, ConnectFourGUI gui)
{
this.game = game;
this.gui = gui;
gui.addListener (this);
}


public void mouseClicked (MouseEvent event)
{
JLabel label = (JLabel) event.getComponent ();
int column = gui.getColumn (label);
int row = game.drop (column);
if (row != -1)
{
gui.set (column, row);
}
}


public void mousePressed (MouseEvent event)
{
}


public void mouseReleased (MouseEvent event)
{
}


public void mouseEntered (MouseEvent event)
{
}


public void mouseExited (MouseEvent event)
{
}
}





/**
* The ConnectFour class.
*
* This class represents a Connect Four
* game, which allows two players to drop
* checkers into a grid until one achieves
* four checkers in a straight line.
*/
public class ConnectFour
{
// the grid used for storing the game layout.
private int[] [] grid;
// the player whose turn it is.
private int currentPlayer;
/**
* The ConnectFour constructor.
*
* Creates and initializes the grid for the
* Connect Four game.
*/
public ConnectFour ()
{
// create the grid
grid = new int [7] [6];
// initialize the grid
for (int row = 0 ; row < 6 ; row++)
{
for (int column = 0 ; column < 7 ; column++)
{
// set the position to a default value
grid [column] [row] = 0;
}
}
// set the first move to Player 1
currentPlayer = 1;
}


/**
* The drop method.
*
* Drop a checker into the specified column,
* and return the row that the checker lands on.
*/
int drop (int column)
{
if (hasWon ())
{
return -1;
}
int row = 0;
for (; row < 6 && grid [column] [row] != 0 ; row++)
{
}
;
if (row == 6)
{
// if the row is 6, it went through all 6 rows
// of the grid, and couldn't find an empty one.
// Therefore, return false to indicate that this
// drop operation failed.
return -1;
}
// fill the row of that column with a checker.
grid [column] [row] = currentPlayer;
// alternate the players
currentPlayer = (currentPlayer % 2) + 1;
return row;
}


/**
* The toString method
*
* Returns a String representation of this
* Connect Four (TM) game.
*/
public String toString ()
{
String returnString = "";
for (int row = 5 ; row >= 0 ; row--)
{
for (int column = 0 ; column < 7 ; column++)
{
returnString = returnString + grid [column] [row];
}
returnString = returnString + "\n";
}
return returnString;
}


/**
* The hasWon method.
*
* This method returns true if one of the
* players has won the game.
*/
public boolean hasWon ()
{
boolean status = false;
// check for a horizontal win
for (int row = 0 ; row < 6 ; row++)
{
for (int column = 0 ; column < 4 ; column++)
{
if (grid [column] [row] != 0 &&
grid [column] [row] == grid [column + 1] [row] &&
grid [column] [row] == grid [column + 2] [row] &&
grid [column] [row] == grid [column + 3] [row])
{
status = true;
}
}
}
// check for a vertical win
for (int row = 0 ; row < 3 ; row++)
{
for (int column = 0 ; column < 7 ; column++)
{
if (grid [column] [row] != 0 &&
grid [column] [row] == grid [column] [row + 1] &&
grid [column] [row] == grid [column] [row + 2] &&
grid [column] [row] == grid [column] [row + 3])
{
status = true;
}
}
}
// check for a diagonal win (positive slope)
for (int row = 0 ; row < 3 ; row++)
{
for (int column = 0 ; column < 4 ; column++)
{
if (grid [column] [row] != 0 &&
grid [column] [row] == grid [column + 1] [row + 1] &&
grid [column] [row] == grid [column + 2] [row + 2] &&
grid [column] [row] == grid [column + 3] [row + 3])
{
status = true;
}
}
}
// check for a diagonal win (negative slope)
for (int row = 3 ; row < 6 ; row++)
{
for (int column = 0 ; column < 4 ; column++)
{
if (grid [column] [row] != 0 &&
grid [column] [row] == grid [column + 1] [row - 1] &&
grid [column] [row] == grid [column + 2] [row - 2] &&
grid [column] [row] == grid [column + 3] [row - 3])
{
status = true;
}
}
}
return status;
}
}



Thanks in advanced :)

godofreality
01-22-2010, 05:09 AM
you have posted this in the wrong forum you are looking for help with java code and this is a javascript forum the 2 are not the same thing and the only thing they have in common is the java portion in thier name i would recommend asking an mod or an admin to move this topic to the correct forum where the help you need would be better provided

Philip M
01-22-2010, 09:09 AM
And wrap your code in [code] tags, not [quote] tags.


BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.