Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please add comments to this code with // and explain the code where necessary this is just some of the code public class

Can you please add comments to this code with // and explain the code where necessary this is just some of the code

public class Connect4 { private JFrame frame;

public Connect4() { frame = new JFrame("DrawGrid"); frame.setSize(600, 400); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setPreferredSize(frame.getSize()); frame.add(new MultiDraw(frame.getSize())); frame.pack(); frame.setVisible(true); }

public static void main(String... argv) { new Connect4(); }

public static class MultiDraw extends JPanel implements MouseListener { int startY = 10; int startX = 10; int turn = 2; int cellSize = 60; int rows = 6; int cols = 7; boolean winner=false; String ccolor = ""; Color[][] grid = new Color[rows][cols]; public MultiDraw(Dimension dimension) { setSize(dimension); setPreferredSize(dimension); addMouseListener(this); //Initializing the Array int x = 0; for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length; col++) { Color c; if(x%2==0) { grid[row][col] = Color.white; } else { grid[row][col] = Color.white; } x++; } } }

@Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Dimension d = getSize(); g2.setColor(new Color(0, 0, 255)); g2.fillRect(0,0,d.width,d.height); startX = 0; startY = 0;

//Creating the second grid for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length; col++) { g2.setColor(grid[row][col]); g2.fillRect(startX,startY,cellSize,cellSize); g2.setColor(Color.black); g2.drawRect(startX,startY,cellSize,cellSize); startX += cellSize; } startY += cellSize; startX = 0; }

g2.setColor(new Color(255, 255, 255)); if(winner==false) { if(turn%2==0) g2.drawString("Red's Turn",450,20); else g2.drawString("Yellow's Turn",450,20); } else { g2.drawString("WINNER - "+ ccolor,450,20); } }

public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if(winner==false) { if(x<(cellSize*grid[0].length) && y<(cellSize*grid.length)) { int clickedRow = y/cellSize; int clickedCol = x/cellSize;

clickedRow = dropP(clickedCol);

if(clickedRow!=-1) { if(turn%2==0) { grid[clickedRow][clickedCol]= Color.red; ccolor = "RED"; } else { grid[clickedRow][clickedCol]= Color.yellow; ccolor = "Yellow"; } turn++; if(checkForWinner(clickedCol,clickedRow, grid[clickedRow][clickedCol])) { winner=true; } } } repaint(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

What is disclosure of cost method investment?

Answered: 1 week ago