Hi basically I'm current doing a connect 4 program that consist of 3 separate programs Connect4Model, Connect 4 Column and Connect4View... I'm trying to compile and run my connect4View program but for some what reason I'm unable to do so. Below I have supplied the code for the 3 programs. Connect4Model and Connect4Column work perfectly fine.
Connect4Model
public class Connect4Model
{
private Connect4Column columns[];
private int NUM_COLUMNS;
private int NUM_ROWS;
private int playerToGoNext = Connect4Column.RED_COUNTER;
public Connect4Model(int numCols, int numRows)
{
NUM_COLUMNS = numCols;
NUM_ROWS = numRows;
columns = new Connect4Column[NUM_COLUMNS];
for (int i = 0; i < NUM_COLUMNS; i++)
{
columns[i] = new Connect4Column(NUM_ROWS);
}
}
public int getNumCols()
{
return NUM_COLUMNS;
}
public int getNumRows()
{
return NUM_ROWS;
}
public int getNextPlayer()
{
return playerToGoNext;
}
boolean go(int thisColumn)
{
if(thisColumn < 0 || thisColumn >= NUM_COLUMNS)
{
return false;
}
System.out.println("thisCol is " + thisColumn);
if(columns[thisColumn].addCounter(playerToGoNext))
{
if(playerToGoNext == Connect4Column.YELLOW_COUNTER)
{
playerToGoNext = Connect4Column.RED_COUNTER;
}
else
{
playerToGoNext = Connect4Column.YELLOW_COUNTER;
}
return true;
}
else
{
return false;
}
}
public int getCounter(int thisColumn, int thisRow)
{
return columns[thisColumn].getCounter(thisRow);
}
public int getNumCounters(int thisColumn)
{
return columns[thisColumn].getNumCounters();
}
}
Connect4Column
public class Connect4Column
{
private int counters[];
private int numCounters;
private int MAX_NUM_COUNTERS;
static int YELLOW_COUNTER = 1;
static int RED_COUNTER = 2;
public Connect4Column(int max_counters)
{
MAX_NUM_COUNTERS = max_counters;
counters = new int[MAX_NUM_COUNTERS];
numCounters = 0;
}
public boolean addCounter(int thisCounter)
{
if ((numCounters < MAX_NUM_COUNTERS)&&((thisCounter == 1)||(thisCounter == 2)))
{
counters[numCounters] = thisCounter;
numCounters++;
return true;
}
else
{
return false;
}
}
public int getNumCounters()
{
return numCounters;
}
public int getCounter(int thisRow)
{
if ((thisRow >= 0)&&(thisRow <= numCounters))
{
return counters[thisRow];
}
else
{
return 0;
}
}
}
Connect4View <<< The one that im having problems with
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Connect4View extends JFrame
{
DrawPanel canvas;
Connect4Model myConnect4;
public static void main(String[] args)
{
Connect4View w = new Connect4View();
w.setVisible(true);
}
public Connect4View()
{
setTitle("Week3: Connect 4");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,220);
setLocation(300,300);
int number_of_columns = 7;
int max_number_of_beads = 9;
myConnect4 = new Connect4Model(number_of_columns,max_number_of_beads);
canvas = new DrawPanel(number_of_columns,max_number_of_beads);
add(canvas);
}
class DrawPanel extends JPanel implements MouseListener
{
public int numCols;
public int numRows;
public DrawPanel(int nc, int nr)
{
numCols = nc;
numRows = nr;
addMouseListener(this);
}
int getCol(int x)
{
return x*numCols/getWidth();
}
int getRow(int y)
{
return y*numRows/getHeight();
}
public void mouseReleased(MouseEvent event)
{
}
public void mousePressed(MouseEvent event)
{
}
public void mouseClicked(MouseEvent event)
{
int thisCol = getCol(event.getX());
myConnect4.go(thisCol);
repaint();
}
public void mouseEntered(MouseEvent event)
{
}
public void mouseExited(MouseEvent event)
{
}
Rectangle getRect(int thisCol, int thisRow)
{
// if input is out of range, return "null"
if(thisCol <0 || thisRow < 0)
return null;
if(thisCol>=numCols || thisRow>=numRows)
return null;
// otherwise, make and return the Rectangle
int w = getWidth()/numCols;
int h = getHeight()/numRows;
int x = thisCol*w;
int y = thisRow*h;
Rectangle myRect = new Rectangle(x,y,w,h);
return myRect;
}
public void paint(Graphics g)
{
g.setColor(Color.gray);
g.fillRect(0,0,getWidth(), getHeight());
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D)g;
// we'll use Graphics2D for it's "draw" method -
// neater than the Graphics "drawRect" suppled
// (which you could also use)
for (int i = 0;i<numCols;i++)
for(int j = 0;j<numRows;j++)
g2.draw(getRect(i,j));
for (int thisCol = 0; thisCol < numCols; thisCol++)
{
int numCounters = myConnect4.getNumCounters(thisCol);
{
for (int c = 0; c < numCounters; c++)
{
int Rows = numRows - (1 + c);
Rectangle t = new Rectangle(getRect(thisCol,Rows));
int color = myConnect4.getCounter(thisCol,c);
//System.out.println(" "+thisCol+" "+c+" "+color);
g2.fillOval(t.x,t.y,t.width,t.height);
}
}
}
}
}
If there is any possible solutions to this problem that would be most grateful. I'm not necessarily asking for help its just advice as to where I have gone wrong.