I want to join 2 dots of the same colour that is 2 red dots or 2 blue dots ....but after applying my code...when i run the program it does not join the 2 same colour dots...even though it does not display any error.

So basically i am trying to create vertical lines and horizontal lines when two dots of the same colour are adjacent.

I HAVE PROVIDED THE PROBLEM WITHIN COMMENTS.

import java.awt.*;
import java.awt.event.*;
import java.awt.Rectangle.*;

public class dotsgame
{
   public static void main( String[ ] args)
   {
      new Dots4Win();
   }
}

class Dots4Win extends Frame
{
   private int counter = 0 ;

   private Rectangle[][] dots = new Rectangle[5][4] ;

   /* 0 denotes black; 1 denotes red;  2 denotes blue */
   private int[][] status = new int[5][4] ;

   Dots4Win()
   {
      setTitle("                                     D O T S         G A M E" );
      setLocation( new Point( 400, 250 ) ) ;
      setSize( 500, 410 );
      setResizable( true ) ;  
      setBackground(new java.awt.Color(236, 233, 220));
      
  for ( int i = 0; i < 5; i++ )
      {
         for ( int j = 0; j < 4; j++ )
         {
            dots[i][j] = new Rectangle( 63 + 85*i, 70 + 90*j, 11, 11 ) ;

         }
      }

      for ( int i = 0; i < 5; i++ )
      {
         for ( int j = 0; j < 4; j++ )
         {
            status[i][j] = 0 ;
         }
      }

      addMouseListener( new MouseCatcher() ) ;        // register a mouse listener
      addWindowListener( new WindowCatcher( ) );      // register a window listener

      setVisible( true );
   }

   public void paint( Graphics gc )
   {
      for ( int i = 0; i < 5; i++ )
      {
         for ( int j = 0; j < 4; j++ )
         {
            if ( status[i][j] == 0 )
            {
               gc.setColor( Color.black ) ;
            }
            else if ( status[i][j] == 1 )
            {
               gc.setColor(Color.red ) ;
            }
            else
            {
               gc.setColor( Color.blue) ;
            }
            gc.fillOval( dots[i][j].x, dots[i][j].y, dots[i][j].width, dots[i][j].height ) ;

         }
         
      }
      
   }

   
  
   
   // I HAVE TRIED THIS CODE BUT UNABLE TO GET VERTICAL LINES WHEN TWO SIMILAR COLOUR DOTS APPEAR THE CODE DOES NOT SHOW ANY ERROR,YET UNABLE TO GET DESIRED RESULT.....PLZZZ HELP
   //-------------------------------------------------------------------------------------------------------------------------------------

   public void paintVertically(Graphics gc) {
       int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                if (status[i][j] == status[i0][j0]) { //the same status
                    if (status[i][j] == 1) {
                        gc.setColor(Color.red);
                    } else {
                        gc.setColor(Color.blue);
                    }
                    if (status[i][j] != 0) {
                        gc.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y);
                    }
                }
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }
public void paintHorizontally(Graphics gc) {
       int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                if (status[j][i] == status[j0][i0]) { //the same status
                    if (status[j][i] == 1) {
                        gc.setColor(Color.red);
                    } else {
                        gc.setColor(Color.blue);
                    }
                    if (status[j][i] != 0) {
                        gc.drawLine(dots[j0][i0].x, dots[j0][i0].y, dots[j][i].x, dots[j][i].y);
                    }
                }
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

   // IN TRYING TO CREATE HORIZONTAL LINES I HAVE SWAPED j with i...bu the lines are not appearing though the program runs without any error.
   //-------------------------------------------------------------------------------------------------------------------------------------




class MouseCatcher extends MouseAdapter
   {
      public void mousePressed(MouseEvent evt )
      {
         Point ptMouse = evt.getPoint() ;

         for ( int i = 0; i < 5; i++ )
         {
            for ( int j = 0; j < 4; j++ )
            {
               if ( dots[i][j].contains( ptMouse ) && (status[i][j] == 0) )   // check you're on a black dot
               {
                  if ( counter % 2 == 0 )
                  {
                     status[i][j] = 1 ;
                  }
                  else
                  {
                     status[i][j] = 2 ;
                  }
                  counter++ ;
                  repaint();
               }
            }
         }
      }
   }

   class WindowCatcher extends WindowAdapter
   {
      public void windowClosing( WindowEvent evt )
      {
         evt.getWindow( ).dispose( );                // releases this window's resources
         System.exit( 0 );
      }
   }
}

Recommended Answers

All 5 Replies

Here are two your methods simplified for testing purposes (without conditions)

public void paintVertically(Graphics gc) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                gc.setColor(Color.blue);
                gc.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y);
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

    public void paintHorizontally(Graphics gc) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                gc.setColor(Color.red);
                gc.drawLine(dots[j0][i0].x, dots[j0][i0].y, dots[j][i].x, dots[j][i].y);
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

//
Is a simpler method without using of additional variables i0,j0
need to reduce the scope of one of the loop and add the number one to one of the indices

Here are two your methods simplified for testing purposes (without conditions)

public void paintVertically(Graphics gc) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                gc.setColor(Color.blue);
                gc.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y);
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

    public void paintHorizontally(Graphics gc) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                gc.setColor(Color.red);
                gc.drawLine(dots[j0][i0].x, dots[j0][i0].y, dots[j][i].x, dots[j][i].y);
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

//
Is a simpler method without using of additional variables i0,j0
need to reduce the scope of one of the loop and add the number one to one of the indices

I have replaced my code with the code that you provided and tried to run the program...yet it is not showing any improvement and is at the same stage..that is no horizontal or vertical lines...hence when 2 similar color dots appear...no line is created

Is it my fault? No.
Make this simple example
new Rectangle[2][2]
points are [0,0] [0,1]
[1,0] [1,1] where 0,1 are indexes to your Rectangle[][] dots
first row --> two points,one horizontal line
second row --> two points,one horizontal line
first col --> two points,one vertical line
second col --> two points,one vertical line

for drawing horizontally use
gc.drawLine(dots[j].x, dots[j].y, dots[j].x, dots[j].y);
in this case your limit of i-loop should be one less then normal: i< sizei-1

for drawing vertically use
gc.drawLine(dots[j].x, dots[j].y, dots[j].x, dots[j + 1].y);
in this case your limit of j-loop should be one less then normal: j< sizej-1

Is it my fault? No.
Make this simple example
new Rectangle[2][2]
points are [0,0] [0,1]
[1,0] [1,1] where 0,1 are indexes to your Rectangle[][] dots
first row --> two points,one horizontal line
second row --> two points,one horizontal line
first col --> two points,one vertical line
second col --> two points,one vertical line

for drawing horizontally use
gc.drawLine(dots[j].x, dots[j].y, dots[j].x, dots[j].y);
in this case your limit of i-loop should be one less then normal: i< sizei-1

for drawing vertically use
gc.drawLine(dots[j].x, dots[j].y, dots[j].x, dots[j + 1].y);
in this case your limit of j-loop should be one less then normal: j< sizej-1

public void paintVertically(Graphics gc){
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                gc.setColor(Color.blue);
                gc.drawLine(dots[i][j].x, dots[i][j].y, dots[i][j].x, dots[i][j + 1].y);

                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

    public void paintHorizontally(Graphics gc) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                gc.setColor(Color.red);
                gc.drawLine(dots[i][j].x, dots[i][j].y, dots[i + 1][j].x, dots[i][j].y);

                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

i have used the above code as suggested by you...but when you do 2 points in each row and 2 points in each column hence 4 points...you are never able to get two same colour dots on the same row or column.......then i changed it to 5 points each row and 5 points each column and it is NOT JOINING.....

THEN I TRIED

gc.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[j].x, dots[j].y); ------>FOR VERTICAL

gc.drawLine(dots[j0][i0].x, dots[j0][i0].y, dots[j].x, dots[j].y);----->FOR HORIZONTAL

still it is at the same stage....so i am kinda confused...and i never said its your fault...its just that i have a deadline....and i am totally lost....just need some help

Aaaa...
To view H & L lines invoke

paintVertically(gc);
        paintHorizontally(gc);

inside paint method.

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.