File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes What exactly is this graphics object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What exactly is this graphics object?" Watch "What exactly is this graphics object?" New topic

What exactly is this graphics object?

Caleb Kemper

Joined: Feb 08, 2012
Posts: 27

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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

Recommended reading

To see custom painting in action, check out these wiki links

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

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: What exactly is this graphics object?
It's not a secret anymore!