| | |
Dot and Lines Game
![]() |
•
•
Join Date: Oct 2007
Posts: 1
Reputation:
Solved Threads: 0
Hi All,
I need a bit of help with a java Dots and Lines game i am developing, Similar to the Dots and Box game. I have laid out the Dots with the two colours blue and yellow which change accordingly.
I now need to draw a horizontal or a vertical line when 2 Dots are the same colour, Can anyone help me out with this.
I'm doing something similar to this:
http://i2.photobucket.com/albums/y6/...4/DotsGame.jpg
Here is the code i Have now:
I need a bit of help with a java Dots and Lines game i am developing, Similar to the Dots and Box game. I have laid out the Dots with the two colours blue and yellow which change accordingly.
I now need to draw a horizontal or a vertical line when 2 Dots are the same colour, Can anyone help me out with this.
I'm doing something similar to this:
http://i2.photobucket.com/albums/y6/...4/DotsGame.jpg
Here is the code i Have now:
Java Syntax (Toggle Plain Text)
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[7][7] ; /* 0 denotes black; 1 denotes red; 2 denotes blue */ private int[][] status = new int[7][7] ; Dots4Win() { setTitle( "Dots Game" ); setLocation( new Point( 200, 100 ) ) ; setSize( 500, 500 ); setResizable( true ) ; // this prevents re-sizing of window for ( int i = 0; i < 7; i++ ) { for ( int j = 0; j < 7; j++ ) { dots[i][j] = new Rectangle( 40 + 70*i, 70 + 40*j, 11, 11 ) ; } } for ( int i = 0; i < 7; i++ ) { for ( int j = 0; j < 7; 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 < 7; i++ ) { for ( int j = 0; j < 7; j++ ) { if ( status[i][j] == 0 ) { gc.setColor( Color.black ) ; } else if ( status[i][j] == 1 ) { gc.setColor(Color.red ) ; } else { gc.setColor( Color.yellow ) ; } gc.fillOval( dots[i][j].x, dots[i][j].y, dots[i][j].width, dots[i][j].height ) ; } } } class MouseCatcher extends MouseAdapter { public void mousePressed(MouseEvent evt ) { Point ptMouse = evt.getPoint() ; for ( int i = 0; i < 7; i++ ) { for ( int j = 0; j < 7; 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 ); } } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
To draw line you need two points. Inside double loop you have access to one of them, current P(i,j). You need additional varibles to remember in the current loops these values, so you can use them on the next loop.
Invoke this inside paint function.
Make same with horizontal lines ( swap places of loops i-j --> j-i ).
Apply additional conditions ( do not draw line for this same point,do not draw diagonals ).
java Syntax (Toggle Plain Text)
void paintVertically(Graphics g) { int i0 = 0; int j0 = 0; for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (status[i][j] == status[i0][j0]) { //the same status if (status[i][j] == 1) { g.setColor(Color.red); } else { g.setColor(Color.yellow); } if (status[i][j] != 0) { g.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y); } } //i0 = i; j0 = j; } i0 = i; } }
Make same with horizontal lines ( swap places of loops i-j --> j-i ).
Apply additional conditions ( do not draw line for this same point,do not draw diagonals ).
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- A python script with input (Python)
- Need general, esoteric advice on C++ in the real world.. (C++)
- smb/// no host "null" (*nix Software)
- dot game (Geeks' Lounge)
- Graphics In Pixel: Part II (C++)
Other Threads in the Java Forum
- Previous Thread: Help-Read line from the text file
- Next Thread: checking my work.
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





