Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lucidquiet

[java] drawing -- the paintComponent() method.

This topic is 5384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was trying to do some drawing and ran into some weird stuff... maybe someone might be able to shed some light on the problem. I was trying to do some drawing -- and I read the Java 2D book and it says to override the paint() method in AWT, but to override the paintComponent() method in swing. So I do this and then realize that paintComponent() is in JComponent, and JFrame isn''t a JComponent. So what the? It doesn''t matter that much anyway, because I change my mind and make the whole thing an MDI with JDesktopPane and some JInternalFrames, and get some weird stuff... 1. Like the JInternalFrame paints several times when created, and clears stuff I''m trying to draw on it. 2. Depending on when I make them visible there are two different occurrences, the JInternalFrame does appear. What I mean is -- I made the Desktop pane made it visible, then made the internal frame and made it visible and everything worked fine. But if I made them both, and set sizes and location for both, then made them visible (the desktop pane first.) The internal frame doesn''t show up. 3. And hears an interesting one. I make both desktop and internal frames and then make the desktop frame visible, not the internal frame. ( I''ve overwritten both paint/paintComponent methods for the frames. ) When I resize the desktop frame a sort of outline of the internal frame appears, but its undragable, unclosable, untouchable really, and doesn''t have the usual colors -- like I said and outline (ghost) really. 4. Not to mention the desktop doesn''t repaint the background when stretched. I''m just wondering if this rings anyone''s bell, and maybe there is something I''m missing. Some sliver of information that I haven''t found yet that would help. If not I''m just gonna look at the API code for paintComponent(). It should have the answers. I would post the code, but its spread over several files. Interfaces and generic frame stuff. L- " ''No one has control -- control is just a fantasy. And being human is difficult.'' "

Share this post


Link to post
Share on other sites
Advertisement
You rarely, if ever, want to directly paint Frames. Throw a JPanel in there and paint on that. If you really do want to draw directly to the frame then a BufferStrategy is probably the way to go (which JInternalFrame et al have nothing to do with, that''s strictly Frame and JFrame).

What exactly are you doing to the paint() and paintComponent() methods? Are you calling super.paint(), super.paintComponent()? If not, you''ve completely taken over the painting process. The super versions will ensure the standard parts of the frame are properly painted and dirty areas cleaned up.

Swing/AWT is a quirky system at times, it takes some getting used to. Basically whenever you say paint() or any method similiar to paint() (like update(), paintAll(), etc), you''re requesting a paint take place, not forcing one. Swing will get around to fulfilling your request whenever it pleases, if it ever does. Usually it honors your request right away, but not always. When you and Swing don''t see eye to eye, you often have to figure out how to "fool" it a little bit.

Also, when painting in Swing goes wrong, try things that will clearly force a repaint. Like minimize then maximize the app. If the result of that is what you''re expecting, then for whatever reason Swing''s just ignoring your paint requests. If the result is still a mess, then something else more fundamental is wrong.

Share this post


Link to post
Share on other sites
Well, that worked...the JPanel I mean. Should have thought of that. Oh, well, live and learn.

However I''m haveing some troubles with my KeyListener now. I added one after I made the frames visible, and I''m not sure if that has anything to do with it. I haven''t gotten a chance to really look at the code again, but it was working not long ago. I''m just curious if there are problems with changing out KeyListeners, programatically. For whatever reason.

Thanks,
L-

" ''No one has control -- control is just a fantasy. And being human is difficult.'' "

Share this post


Link to post
Share on other sites
If you need more control over rendering to a frame and you''re developing an application (not applet) for Java 1.4 or higher, you can take advantage of Full Screen Exclusive mode. Read the tutorial on the Sun website.

Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,
fasting, good works and so on. Up until Doom, no one seemed to have thought about the
double-barrel shotgun. Eat leaden death, demon...
-- Terry Pratchett

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!