Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help wiht basic Graphics

 
Harold Lime
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic