0

Can anyone explain me why shouldn't i use paint method to draw directly inside a JFrame window, and i should use paintComponent method with a JPanel inside the JFrame ?

Thanks in advance.

Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
  • @ChristopheD Not quite, good suggestion for further reading. In that case, they were asking about the difference between painting approaches for a Swing component as apposed to using the window/top level container over a Swing component - just saying ;) – MadProgrammer Jun 03 '14 at 23:50
  • @MadProgrammer: Oops, a bit too quick with the duplicate search this time I'm afraid. Thanks for pointing this out. – ChristopheD Jun 03 '14 at 23:52
  • 1
    -1, You were given a link to the Swing tutorial in your last question. Did you read the tutorial? It also contains a section on `Custom Painting` which does a high level overview of painting in Swing. The tutorial also contains a link to a more in depth discusson on painting. Read the two tutorials and then ask a specific question if there is something you don't understand. – camickr Jun 04 '14 at 00:18
  • @camickr if all of us have time to read all those oracle tutorial, i would never ask a question here, your answer and the way you speak is not kind and not respectful, sorry. – Naruto Biju Mode Jun 04 '14 at 00:25
  • 3
    @NarutoBijuMode *"if all of us have time to read all those oracle tutorial, i would never ask a question here"* That's not true. Your first place is the tutorials, the second place is the forums, which you use to ask for clarification on points you didn't understand. If you're not willing to invest the time or effort into teaching yourself, why should anybody else, we all have time constraints as well – MadProgrammer Jun 04 '14 at 00:30
  • @MadProgrammer I know to understand something you should read the articles and tutorials given by the source. What i complained about is the way to treat people here, we are humans no one in born knowing all knowledges, and if you don't want to help or you don't have the answer why you bother them, it's like if you ask a teacher a question and he tells you go read the book, there are people here who doesn't fully understand english (like me) and there are others who haven't the same intelligence to understand like others. – Naruto Biju Mode Jun 04 '14 at 00:41
  • 1
    @NarutoBijuMode Camickr is simply trying to point out to you that polluting the forum with questions that can be answered in some part by doing some research first is not what SO is meant for, sorry if you find this annoying or offensive, that's not our intention, our intention is to make you a better, self supporting developer. Ask for an answer won't teach you when the answer your get is not appropriate (and this happens way to often here). Having the ability to research a topic, even a little, before asking for clarification on a topic will put you in a better position to see good answers – MadProgrammer Jun 04 '14 at 00:47
  • @MadProgrammer You are great man, you know how to explain and how to express your idea with respect which is important in life. Thanks really for your help. – Naruto Biju Mode Jun 04 '14 at 00:51

1 Answers1

4

Three main reasons...

  1. Top level containers aren't double buffered, which cause flickering when the frame is repainted, yes, you can implement you're own double buffering, but...
  2. Painting inside a frame does not take into consideration the frames borders, meaning that it's possible to paint under them. Frame borders are also platform/look and feel specific, meaning that there sizes change
  3. In the case of Swing windows, there are components that already exist on the window, meaning that they paint over (or be painted over), in most cases, both (because of the optimised painting engine in Swing), making it difficult to produce a reasonable result

For point #2, frame decorations are painted WITHIN the "window" bounds, not outside them

Take a look at...

for more details

Make the time to read through Painting in AWT and Swing and Performing Custom Painting for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I'm beginner to java and swing, i didn't understand the first reason, for the second is there any way to prove that the draw is under the frame borders because for me it seems impossible on windows, for the third can you give me some example for more clarification. Thanks. – Naruto Biju Mode Jun 04 '14 at 00:09
  • 1
    @NarutoBijuMode Frame decorations are painted WITHIN the bounds of the Window, not outside them, this means that the position and size of the window is always correct (and doesn't need to be modified to adjust for the decorations size). Double buffering is a technique used to improve the performance of painting, rather then trying to paint directly to the screen device which has it's own overheads, you paint an offscreen buffer and do a single update by drawing the buffer to the screen. It reduces flickering when updating the screen – MadProgrammer Jun 04 '14 at 00:28
  • I think that the second is the best reason to not draw inside frame. Thanks for the examples :) – Naruto Biju Mode Jun 04 '14 at 00:34