ku95 0 Light Poster
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);
				}
			}
		}
	}

 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.