• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

The Art & Science of Java Chapter 4 Exercise 14 Checkers

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

I'm going through CS106A(Programming Methodology) course from iTunes where in the The Art & Science of Java book associated with the course, there is an exercise called Checkers.

Below is that exercise and as you can see, I've completed the program but not as polished as I hope.

I am using Eclipse and had no Programming Knowledge prior to this course.

-------------------------------------

How do get rid of the circles or blank checkers from the white squares?

Also how do I make the checkers smaller and centred in each black squares?

Thank you guys.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using doubles for the number of rows? That is definitely an integer number. Why have you got N_Rows (which should be spelt in UPPER_CASE throughout) and columns as well? The board is square, so you always have rows and columns the same.
How are you drawing the board? Are you using the paintComponent method? In which case you would call super.paintComponent(g); as the first line of that method.
Why have you got all that GUI work in the run() method? That ought to be in the constructor of the GUI class, or a method called private void initGUI() called from the constructor.

Don’t know exactly why you are getting that problem, but check those queries and see how far you get.
 
Greenhorn
Posts: 2
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The proper code is:


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding this line



I tried running this code in Eclipse and I ran a println on this statement and received '0' as its output. I know this can't be correct. What is this variable supposed to be calculating. The code above doesn't have any comments. Thanks.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Mazz wrote:I have a question regarding this line



I tried running this code in Eclipse and I ran a println on this statement and received '0' as its output. I know this can't be correct. What is this variable supposed to be calculating. The code above doesn't have any comments. Thanks.


Welcome to the Ranch.

To be honest I don't know. Looking at how it is used I guess it's supposed to be the total board width and the OP was assuming the panel was going to be wider than it was high and so the board would fill the height but would need to be centered on the width. Having said that the calculation doesn't appear to be calculating that so maybe I'm barking up the wrong tree. Without having the rest of the code so I can run it, it's hard to give further advice, sorry.

Maybe someone else here knows the question and can give a better answer.

 
Jeremy Mazz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying, the rest of the code is two posts up from my original post. I was referring to Goran Dragutinovic code.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Mazz wrote:Thanks for replying, the rest of the code is two posts up from my original post. I was referring to Goran Dragutinovic code.



I figure that line puts the checker board in the center of the panel. hence the "getWidth()/2 -" part.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Mazz wrote:Thanks for replying, the rest of the code is two posts up from my original post. I was referring to Goran Dragutinovic code.


Yes, I saw that. That's how I worked out it was probably being used to center the panel along the X axis. But that code is not an SSCCE and so can't be run.
 
Get me the mayor's office! I need to tell him about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic