• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

board not Drawing...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic