This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

board not Drawing...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason, when I am calling "InitializeBoard(g)" and
reDrawBoard(g), It's not painting to the JPanel...

is there something I am missing?

code is below...








thanks,


Justin
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can't copy/paste/compile/run your code due to the missing classes,
particularly Square.

as your problem is to do with the display, create a sample program revolving
around just adding the one square.

Get rid of everything unrelated to the problem of showing a single square
when the program opens i.e. get rid of the listeners, the methods update/find/redraw etc

include a class Square, again get rid of everything from the square class
if it is not related to its display.

you should now be able to post a very short 'working' program that displays the problem
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I fixed the problem...


in my battleship class, in the paintComponent() I have the
following code:



I'm guessing that there is something wrong with me
calling "board = new Board()" when I repaint...

so I just got rid of the "InitializeBoard()" method,
and put that code in the constructor...

and it worked fine.

and when I click on a square, I called a "findSquare" routine
which looks for a square on the board that contains the same
X,Y coords captured by the mouseClicked() in BattleShip class.

then i UpdateBoard, and Color that Square white.

I just have one question regarding my ship objects...

ok:

1. my ship class extends JPanel
- so I can use setEnabled, setToolTipText, etc...
- also I can call this.getX(),this.getY()
- and I can add a Mouse/Action/Key event directly to the
component

ok, here is the question...

My board is drawn using drawRect and such...
what would be a good way to tell if a square on my board
contains a ship object?

should I grab the Point when clicked, and then check that point by
saying:



thanks

Justin
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> should I grab the Point when clicked, and then check that point by
saying:

I'd say it would be the other way around
instead of ship.contains(), it would be board.contains(ship)

try this, click at several places in the panel, then click the ship

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic