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