• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Multi-threading Java Graphics2D Instances

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all,

I'm new to Java Graphics, and am developing an app that displays ships on top of an aerial view of a harbour.  I followed some online tutorials and successfully used GeneralPath to draw ships within a class which extended JPanel.  As we live and program in an object oriented world, I then decided to create a ShipShape class and delegate drawing of each ship to that class by way of sending a Graphics2D reference from the JPanel class to the ShipShape class constructor.  This too was successful.  Then I decided to take the next step and make each ShipShape instance run in its own thread, so each could exist independently of another.  Each ship instance would also incorporate some simple animation, and so this also influenced my decision to use threads, so any animation could occur within a loop inside a run() method.  Unfortunately, converting ShipShape to a Runnable class has not been successful - instances of ShipShape no longer draw to the screen inside the JPanel class.

I'm wondering if the Graphics2D reference passed to the ShipShape constructor gets broken now that ShipShape is a separate thread.  I've attached a cut down version of my three classes below.  Any help in solving this mystery would be greatly appreciated.  Looking forward, I imagine fully grasping multi-threading in Java graphics must be a vital skill.  Surely in any Java game the individual, animated, Java characters would exist within their own thread, yes?

Thanks & regards,

Chris.





 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
admins: This appears to be an exact duplicate of a post created in the Swing/AWT forum.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!