aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help wiht basic Graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help wiht basic Graphics" Watch "Help wiht basic Graphics" New topic
Author

Help wiht basic Graphics

Harold Lime
Ranch Hand

Joined: Jul 20, 2009
Posts: 38
Hi everyone,

I've not done any programming in a while and am tinkering with some graphics to keep my little grey cells working.

I'm stuck on a couple of things though, so any help on this would be appreciated.



It's a bit quick and dirty but it displays a background image (a football pitch), then in the loop draws a series of circles.
I want g.clearRect() to wipe away the previous circle (to give the illusion of movement) but it also wipes that part of the background. I need something to call paintComponent() but I don't have a graphics object. Is there a simple way of doing this?

Also, when I resize the window the circles disappear - which method needs calling to refresh them and where/how do I call it?

all help greatly received.

Harry
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I haven't tried to figure out exactly how your program works, but have you tried to first paint a ball to the color of the pitch (background) , using the old location, then change the draw color and the location and paint a new circle?

There are several ways of approaching this type of programming exercise. I see value in trying several different approaches, so you can actually see why some work and some won't.

p.s. ok I misread your post, if your background image is more than just a flat color, then my approach probably won't work. You will have to look at a way to redraw the entire pitch each time the ball moves.

consider having a separate class to represent your pitch, with it's own paint routine that includes drawing the pitch and the ball based on it's position. Then each time you call pitch.repaint() that should work

p.s. your separate pitch class would probably extend JPanel
Harold Lime
Ranch Hand

Joined: Jul 20, 2009
Posts: 38
Thanks

I'm going to have to get used to this thinking malarkey.

I've got it working, all except the disappearing cirlces when the frame re-sizes, a bit of research tells me that paintAll() may need calling - I'll look into that and see what I can find.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Welcome to JavaRanch

You don't play the zither do you?

We usually discuss Graphics and GUIs on the Swing forum, so I shall move you there.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
Never use the getGraphics(...) method to get a Graphics object. All custom painting needs to be done in the paintComponent(...) method. This means the the paintComponent() method needs to repaint everything every time it is called. So it needs to repaint the background image as well as the circles.

You can check out my Custom Painting Approaches for a couple of examples as well as a link to the Swing tutorial on Custom Painting for more information.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rob Camick wrote:Never use the getGraphics(...) method to get a Graphics object. All custom painting needs to be done in the paintComponent(...) method. This means the the paintComponent() method needs to repaint everything every time it is called. So it needs to repaint the background image as well as the circles.

You can check out my Custom Painting Approaches for a couple of examples as well as a link to the Swing tutorial on Custom Painting for more information.


Hey Rob, with the bolded remark you touch upon something I've never been too clear on, which is the difference between paint and paintComponent. Why do you say you must use paintComponent ? That would be akin to saying you must never use the paint method, would it not? considering that without one or the other there is no custom painting as far as I know.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
That would be akin to saying you must never use the paint method,


Well, I guess one should never say never. But for beginners that is a simple rule to follow.

Much of the confusion arises because when using old AWT components you did override paint(). But things changed for Swing and you should now be override the paintComponent() method. The Swing tutorial on custom painting explains how the paint() and paintComponent() methods are related and used in Swing.

So I still maintain that unless you know what you are doing and you know what effect you are trying to achieve you should always override paintComponent().
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

The difference is the painting order.

paint calls a few other painting methods, most imporantly paintComponent, paintBorder and paintChildren - in that order. So if you override paint, you may end up painting over any border and/or children (haven't tested that actually). By overriding paintComponent, you're really sure that the border and children are painted after the custom painting, and will overlap that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
OK, I see your point. I respect the fact that for beginners you want to keep things simple. If you think that it's simpler (easier to understand) to have just one method, then fair enough. But if it just makes them wonder why one way is better than another, then it ain't quite so simple, especially for enquiring minds.

Good explanation Mr Prime. Not having encountered the situations you described, I was kind of wondering why this (and other) A better than B statements get made.

Best regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

I've just tried it, and I was right about painting over children.

Consider the following:
Try it with and without line 17. If it weren't for the blinking cursor, the text field would remain hidden until you start typing in it. Apparently the panel will not be repainted then but the JTextField will be. Similarly for the button; it will only become visible if it repaints itself due to an event (hovering, clicking, etc).
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
But if it just makes them wonder why one way is better than another, then it ain't quite so simple, especially for enquiring minds


If is not a matter of one way is better than another. Swing was designed to do custom painting for a component in the paintComponent() method, plain and simple. However, because Swing if far more customizable, you can also get creative and add different affects. This should only be done after you kow and understand the basics of painting.

That is why I always recommend reading the Swing tutorial on "Custom Painting". It can explain it better than I can and I don't have to wasted time duplicating what it says. If you truly have an enquirying mind you will read the tutorial. If you really don't care why, then you just follow the simple rule.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help wiht basic Graphics
 
Similar Threads
Graphics 2D and Components
setLocationRelativeTo(null) - problems with setBackground
alpha filter of an element
KeyListener() is sometimes deaf
JPEGImageEncoder problem