The method paint(Graphics)
in components lightweight (Swing) performs these three things, in that order:
Paints the content of the component itself when calling the method paintComponent(Graphics)
.
Paints subcomponents when calling the method paintChildren(Graphics)
.
Paint the edge of the component when calling the method paintBorder(Graphics)
.
The method paint(Graphics)
also does a few more things to determine which area is to be painted. It also handles the case you are printing the component (to the printer), then calling the methods print*(Graphics)
instead of paint*(Graphics)
. See all this on corresponding source code.
How you will hardly want to alter this standard functioning of paint(Graphics)
(there is little reason for this), it would hardly make sense to write such a method. So, write over the method paintComponent(Graphics)
that is who makes the design of the component of fact.
The class Graphics2D
is a subclass of Graphics
. She has several more methods, just take a quick look at the her javadoc to realize this. AWT will always pass an instance of Graphics2D
for your method paintComponent(Graphics)
, so it’s safe to do the cast. If I’m not mistaken, the reason for this is historical, and the ideal would be that the extra methods had been added to the class Graphics
instead of in a subclass.
See more about this on oracle tutorial, although the information there is presented in a disorganized way.
I also recommend looking at the my specialization TCC which ended in 2007, where I brought this up.
I think there are a lot of questions for just one question. I recommend that I left only the 2 and 3 that are directly related.
– user28595
In short, swing is an evolution of awt but he ends up inheriting many things from the awt, like the Paint method, which is responsible for the design of the components in the awt, but in swing, the mother class of all components was rewritten (Component in awt and Jcomponent in swing), and the paintcomponent’s drawing of the canvas was deleted. Because every class in the API inherits from Jcomponent, and this method is its core, it turns out it’s the best option for rewriting when we need to redesign a component.
– user28595
The second question is partially answered in the previous comment, adding to the fact that Graphics2d is an evolution of Graphics, prepared to provide better tools for geometric drawings and shapes, colors and other details in the construction of elements. It provides a larger set of possibilities for drawing than Graphics.
– user28595
@So when using swing elements (like a Jpanel for example) it is recommended to use paintComponent?
– Diego H. Seto
Yes, because all that J has in front of it is the swing API, and it inherits from Jcomponent, which is the class that serves as a sort of base for all classes of swing components. If I’m not mistaken, the class itself
paint
started calling thepaintComponent
, then it makes more sense to overwrite the last one so as not to break the general drawing than if doing.– user28595
I do not know if my explanation was good, so I did not answer, but if you know English, this link is your complete answer to question 1: https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html The other answers your question 2: https://docs.oracle.com/javase/tutorial/2d/overview/index.html
– user28595
I understand your explanation, thank you very much :D
– Diego H. Seto