This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

What exactly is this graphics object?

 
Greenhorn
Posts: 27
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a simple game where a ball moves around the screen according to key movements. I've used basic physics equations to calculate the position according to the summed forces of the Environment (my object to extend JPanel). The paint() is called on a timer which repaints every 10 milliseconds. That's all in working order. At the moment, I've added a laser to the ball which rotates in accordance to the MouseMotionListener, which utilizes the same painting technique in my paint method (AffineTransform rotate method is set to zero typically and then changed when mouse is moved). That is also in working order. My problem is with the actual laser shot. I've added a MouseListener to initiate the drawing of the shot from the barrel (a simple line2D object). Unfortunately, calling repaint() here doesn't always beat the repaint() being called in the Timer. It's about a 50% chance that the laser actually paints when clicked. So what I need to know is what exactly this Graphics object is? I understand that Java calls it whenever the component is updated or repaint() is called, but what is it associated with? I would like to be able to create a second paint method (without creating another panel) that I can call manually. Can I do this with getGraphics()? What would I call getGraphics() on?

TOO LONG DIDN"T READ: What is a Graphics object exactly? How can I create my own so I can feed it into a second paint() method? I'm looking to make a second method so different calls to repaint() don't interrupt each other, as they are being called from different contexts. (Would I call getGraphics() on my JPanel and simply feed it into a second method "paint2" or something?)
Sorry, if it's kind of long, let me know if I need to clarify.

EDIT: Semi-resolved issue. Posting shorter, more focused question.
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/painting/

To see custom painting in action, check out these wiki links
https://coderanch.com/how-to/java/BackgroundImageOnJPanel
https://coderanch.com/how-to/java/PlotTest
https://coderanch.com/how-to/java/PlotSineCurve

Also, please refrain from using special sized fonts/colors. I have edited them out from your post to make it more easy on the eyes
 
No more fooling around. Read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!