Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Custom Repaint not working at all

 
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:

 
Marshal
Posts: 69019
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Rancher
Posts: 3161
29
  • 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.
 
That is a really big piece of pie for such a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic