Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Repaint not working at all  RSS feed

 
Karan Mohnani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create an X/O game and in that i am not able to call drawString() when user clicks on the required position. I am able to display grid and take mouse co-ordinated where user clicks but i am not able to call repaint() or call update() "X" and "O" on that. Please help.Thanks

Here is code snippet:

 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't override update(...). There is no need to do that in Swing.



Don't use two Lists for the painting location. Instead you can use an ArrayList that stores a Point object, which contains both x/y values:





Don't use the MouseInfo class. That will return the mouse location relative to the screen. You want the mouse location relative to the panel, since you are doing your painting on the panel, not the screen.

Instead you can use e.getPoint() and add the point to the points ArrayList



Don't invoke getGraphics or invoke update(...) directly. Swing will do all this for you when a component needs to be repainted.



The drawString(...) method paints the text above and to the right of the point specified. So you will need to adjust the x/y values so the character is painted in the center of your grid. You can use the Graphics.getFontMetrics() to get information about the size of the text.

Don't know if that is everything, but it is a start.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!