Connect4View

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class Connect4View extends JFrame
{
	DrawPanel canvas;
	Connect4Model model;
	ControlPanel myControlPanel, JRadioButton;

	public static void main(String[] args)
	{
		Connect4View w = new Connect4View();
		w.setVisible(true);
	}

	public Connect4View()
	{
		setTitle("Connect4 solution");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,220);
		setLocation(300,300);

		int number_of_columns = 7;
		int number_of_rows = 9;

		model = new Connect4Model(number_of_columns,number_of_rows);
		canvas = new DrawPanel(number_of_columns,number_of_rows);
		myControlPanel = new ControlPanel();


		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);
		add(myControlPanel, BorderLayout.SOUTH);
 	}
	class ControlPanel extends JPanel implements ActionListener
	{
		JButton myButton;
		JRadioButton twoPlayerButton;
		JRadioButton autoRedButton;
		JRadioButton autoYellowButton;

		public ControlPanel()
		{
			myButton = new JButton("reset!");
			add(myButton);
			myButton.addActionListener(this);

			twoPlayerButton = new JRadioButton("Two Player Game");
			twoPlayerButton.addActionListener(this);
			add(twoPlayerButton);
			twoPlayerButton.setSelected(true);


			autoRedButton = new JRadioButton("Auto Red Button");
			autoRedButton.addActionListener(this);
			add(autoRedButton);
			autoRedButton.setSelected(true);

			autoYellowButton = new JRadioButton("Auto Yellow Button");
			autoYellowButton.addActionListener(this);
			add(autoYellowButton);
			autoYellowButton.setSelected(true);


			ButtonGroup group = new ButtonGroup();
			group.add(twoPlayerButton);

			ButtonGroup group1 = new ButtonGroup();
			group.add(autoRedButton);

			ButtonGroup group2 = new ButtonGroup();
			group.add(autoYellowButton);
		// and also add the other Radio Buttons to the group
		}



		public void actionPerformed(ActionEvent event)
		{
		 if(event.getSource()==twoPlayerButton)
		   {
		    System.out.println("twoPlayer");
		   }
		   else if(event.getSource()==autoRedButton)
		   {
		    System.out.println("RedButton");
		   }
		   else if(event.getSource()==autoYellowButton)
		   {
		    System.out.println("Yellow Button");
		   }
		   else if(event.getSource()==myButton)
		   {
		    //
   }
			System.out.println("event is " + event.getActionCommand());

			model.reset();
			canvas.repaint();
			getParent().repaint();
		}
	}
	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());
			model.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 < model.getNumCols(); thisCol ++)
			{
				int num_of_counters = model.getNumCounters(thisCol);
				System.out.println("col " + thisCol + " has " + num_of_counters);
				for (int counter=0; counter < num_of_counters; counter ++)
				{
					int colour = model.getCounter(thisCol, counter);
					if(colour==Connect4Column.RED_COUNTER)
						g2.setColor(Color.red);
					else if(colour==Connect4Column.YELLOW_COUNTER)
						g2.setColor(Color.yellow);

					Rectangle r = getRect(thisCol, numRows-counter-1);
					if (r != null)
						g2.fillOval(r.x, r.y, r.width, r.height);
				}
			}
		}
	}

 }

Connect4Model

public class Connect4Model
{
	Connect4Column [] columns; // an array of columns
	int NUM_COLUMNS; // how many columns we have in the game
	int NUM_ROWS;    // how many rows we have in the game
	int playerToGoNext = Connect4Column.RED_COUNTER; //can be red or yellow

	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);
		}
	}

	int getNumCols() // simply return the number of columns
	{
		return NUM_COLUMNS;
	}

	int getNumRows() // simply return the number of rows
	{
		return NUM_ROWS;
	}

	int getNextPlayer() // returns who gets to go next
	{
		return playerToGoNext;
	}
	void reset()
	{
		for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol ++)
		{
			for (int thisRow = 0; thisRow < NUM_ROWS; thisRow ++)
			{
				columns[thisCol].numCounters = 0;
				columns[thisCol].counters[thisRow] = 0;
			}
		}
	}

	boolean go(int thisColumn) // try to put a counter at this col
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return false;

		System.out.println("thisCol is " + thisColumn);
		if(columns[thisColumn].addCounter(playerToGoNext)==true)
		{
			if(playerToGoNext==Connect4Column.YELLOW_COUNTER)
				playerToGoNext=Connect4Column.RED_COUNTER;
			else
				playerToGoNext=Connect4Column.YELLOW_COUNTER;
			return true;
		}
		return false;
	}

	int getCounter(int thisColumn, int thisRow) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getCounter(thisRow);
	}
	int getNumCounters(int thisColumn) //
	{
		if(thisColumn<0 || thisColumn>=NUM_COLUMNS)
			return 0;
		return columns[thisColumn].getNumCounters();
	}

}

Here I have placed my code. Im trying to store the state of the auto play in the Connect4Model class. I've worked out that I have to write get and set methods so that the other classes can access information. This is highly confusing and I was wondering if there is a possible solutions to this.

JRadioButton is a one bit two state memory.

JRadioButton autoRedButton;
JRadioButton autoYellowButton;

to check state simply write

boolean stateOfAutoRedButton =autoRedButton.isSelected();

public class Connect4View extends JFrame {

    DrawPanel canvas;
    Connect4Model model;
    ControlPanel myControlPanel;

    public Connect4View() {
        setTitle("Connect4 solution");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 220);
        setLocation(300, 300);

        int number_of_columns = 7;
        int number_of_rows = 9;

        model = new Connect4Model(number_of_columns, number_of_rows);
        canvas = new DrawPanel(model, number_of_columns, number_of_rows);//A
        myControlPanel = new ControlPanel(model, canvas);//B

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        add(canvas, BorderLayout.CENTER);
        add(myControlPanel, BorderLayout.SOUTH);
    }
    //C
    public boolean isAutoRedButtonSelected() {
        return myControlPanel.isAutoRedButtonSelected();
    }
}
public class ControlPanel extends JPanel implements ActionListener {
    //F
    private JButton myButton;
    private JRadioButton twoPlayerButton;
    private JRadioButton autoRedButton;
    private JRadioButton autoYellowButton;
    //
    private final Connect4Model model;
    private final DrawPanel canvas;
    //D
    public ControlPanel(Connect4Model model, DrawPanel canvas) {
        this.model = model;
        this.canvas = canvas;
        myButton = new JButton("reset!");
        //...
    }

    public void actionPerformed(ActionEvent event) {
        //...
        model.reset();
        canvas.repaint();
        getParent().repaint();
    }
    //E
    public boolean isAutoRedButtonSelected() {
        return autoRedButton.isSelected();
    }
}

Place your classes in separate files
To do this change constructors (A,B,D) , add fields.
Make fields private (F). Use set/get/is methods (C,E,G).

public class Connect4Model {
        //...
    void reset() {
        for (int thisCol = 0; thisCol < NUM_COLUMNS; thisCol++) {
            for (int thisRow = 0; thisRow < NUM_ROWS; thisRow++) {
                //columns[thisCol].numCounters = 0;
                columns[thisCol].resetNumCounter();
                //columns[thisCol].counters[thisRow] = 0;
                //G TODO
            }
        }
    }
}

I get what you mean what you have done, but whats bugging me is the fact that I have to store the state of the autoplay in the Connect4Model class and I have to decide how this is to be stored, and write get and set methods so that the other classes can access this information. If I'm to write the get and set methods, won't I then have to call them? Also I tried your methods and funny enough it was giving me cannot find symbol errors when clearly the variables have been declared

Edited 6 Years Ago by ku95: n/a

in class Connect4Model declare field and method

private boolean redButton;

    void setRedButton(boolean state) {
        redButton = state;
    }

class ControlPanel:
method actionPerformed write

//...
          else if (event.getSource() == autoRedButton) {
            System.out.println("RedButton");
            model.setRedButton(isAutoRedButtonSelected()); //H
        }

where constructor is

//D
    public ControlPanel(Connect4Model model, DrawPanel canvas) {
        this.model = model;
        this.canvas = canvas;
        myButton = new JButton("reset!");
        //...
    }
This article has been dead for over six months. Start a new discussion instead.