Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Graphics problems

Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello People,
I'm trying to write a game that invloves placing a piece on a "board" and then doing stuff to it, like moving it about etc.. I would like to have a grahic that is independent of the board, but I don't know how to do this. At the moment, I have a piece that I place on the board, but I've done it with a polygon, which is part of the board, by definition. My code looks something like this (off the top of my head..)
public class board extends JPanel
public board()
//constructor stuff..
public void paintComponent(Graphics g)

int [] x = {x1, x2, x3, x4}
int [] y = {y1, y2, y3, y4}
Polygon p = new Polygon(x, y, 4);
//more code that does other stuff..
I basically want something that I can place anywhere I like, and then move or rotate it, etc.. I reckon it would be better to have something independent of the JPanel so I could then put it in a different class and manipulate it independently if I want to.
Any help would, of course, be greatly appreciated!
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way that you could do this is by making a paintComponent(Graphics) method inside the class that represents your game piece. Then, inside the paintComponent(Graphics) method of your board class iterate through your pieces and pass the Graphics reference to that piece's paintComponent() method.

Here's a short (fake code - does not compile) example :

[ July 31, 2002: Message edited by: Nathan Pruett ]
malcolm bailey
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
SOrry I've not replied, I've been away from programming for a wee while, but I've had a look at what you suggested and it's working well, thanks very much!!
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic