Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

draw a chessboard

Thread view: 
dfsd - 09 Dec 2004 16:19 GMT
hi there,

i really need your help , i haven't got a clue how to draw a chessboard in
java.  can u help me?

the question is:

Write a method whose heading is
static void drawBoard(Graphics g, int x,int y, int edge, int size, Color k1,
Color k2)

Which draws a chess board whose top left hand corner is at x,y.
The size of each liitle square is edge
The number of squares per row is size
and the colours of the squares are k1 and k2.
The whole board has a black border.

i think i have to use something like,:  for(int i=1; i<8; i+=2 ) {

but i dont know how.

Please help, i would be sooo grateful

thanks
Chris Smith - 09 Dec 2004 18:23 GMT
> i really need your help , i haven't got a clue how to draw a chessboard in
> java.  can u help me?

A little, perhaps.  Are you familiar with the Java Standard API
Documentation?  It's available online at
http://java.sun.com/j2se/1.5.0/docs/api/, and contains the information
you need to know about using the Graphics class.  Graphics is in the
java.awt package.

The rest is just math.  If you're confused, you might try posting more
about what you tried (even with source code), and what went wrong when
you tried it.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Fahd Shariff - 09 Dec 2004 22:38 GMT
A simple looping exercise. Note how I use the mod (%) operator to
switch colors alternately.

public void drawBoard(Graphics g, int x,int y, int edge, int size,
Color k1,
Color k2) {

for(int j = 0; j <size ; j++) {
for(int i = 0 ; i < size ; i++) {
g.setColor(i%2==(j%2==0?1:0)?k1:k2);
g.fillRect(x+i*edge, y+j*edge, edge, edge);
}
}
g.setColor(Color.black);
g.drawRect(x,y,edge*size,edge*size) ;
}

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.